Linked Lists Linked Lists, Queues, and Stacks



Similar documents
Data Structures and Algorithms Stacks and Queues

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Common Data Structures

Chapter 3: Restricted Structures Page 1

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Module 2 Stacks and Queues: Abstract Data Types

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Abstract Data Types. Chapter 2

St S a t ck a ck nd Qu Q eue 1

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Analysis of a Search Algorithm

DATA STRUCTURE - QUEUE

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Cpt S 223. School of EECS, WSU

Stacks. Linear data structures

DATA STRUCTURES USING C

DATA STRUCTURE - STACK

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Data Structures and Data Manipulation

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Universidad Carlos III de Madrid

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

Introduction to Data Structures

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

STACKS,QUEUES, AND LINKED LISTS

CmpSci 187: Programming with Data Structures Spring 2015

Introduction to Data Structures and Algorithms

Data Structures Using C++

Basic Data Structures and Algorithms

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Sequential Data Structures

7.1 Our Current Model

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Data Structures and Algorithms

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

LINKED DATA STRUCTURES

Data Structures and Algorithms C++ Implementation

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Algorithms and Data Structures

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

CS 2412 Data Structures. Chapter 3 Queues

Data Structure [Question Bank]

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Chapter 14 The Binary Search Tree

Data Structures and Algorithms V Otávio Braga

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Timing of a Disk I/O Transfer

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Lecture Notes on Stacks & Queues

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Data Structures UNIT III. Model Question Answer

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Programming with Data Structures

3 Abstract Data Types and Search

D06 PROGRAMMING with JAVA

! stack, queue, priority queue, dictionary, sequence, set e.g., a Stack is a list implements a LIFO policy on additions/deletions.

Linked List as an ADT (cont d.)

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Lecture 2: Data Structures Steven Skiena. skiena

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Lecture 12 Doubly Linked Lists (with Recursion)

Stacks. Data Structures and Data Types. Collections

CS 2412 Data Structures. Chapter 2 Stacks and recursion

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

10CS35: Data Structures Using C

Java Software Structures

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Algorithms and Data Structures Written Exam Proposed SOLUTION

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Ordered Lists and Binary Trees

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Transcription:

Linked Lists Linked Lists, Queues, and Stacks CSE 10: Introduction to C Programming Fall 200 Dynamic data structure Size is not fixed at compile time Each element of a linked list: holds a value points to the next list element Linked List Illustration List Operations head 1. Create list 2. Destroy list. Retrieve value at specified position. Insert value at specified position. Delete value at specified position 6. Get number of elements in list Sample Code List Creation struct listnode ; int value; listnode *next; void initializelist() head = NULL; numitems = 0; static listnode *head; /* points to first list item */ static int numitems; /* number of list elements */

List Deletion void deletelist() listnode *temp = NULL; while (head!= NULL) temp = head; head = head->next; free(temp); numitems = 0; List Retrieval int retrieve (int pos) /* Assumption: pos < list length */ listnode *ptr = head; int i; for (i = 0; i < pos; i++) ptr = ptr->next; return ptr->value; Inserting Values Locating The Insertion Point Two steps: Locate insertion point Insert new list element Step two requires some care! If the list is sorted, this requires a loop We want a pointer to the node BEFORE the insertion point Inserting The New Element Order Matters! points to the node BEFORE the insertion point ->next is our only reference to the rest of the list points to the new list element If we change that first, we lose the rest of the list!

Algorithm: First Try Algorithm: Second Try ->next = ; ->next = ->next; ->next = ->next; 1 ->next = ; 2 1 2 Algorithm Summary Removing An Element Similar to inserting an element 1. Locate insertion point 2. Set next pointer of new element. Insert new element Three steps: Locate the element to be removed Redirect pointers Delete the selected element Finding The Removal Point Pointers for Removal Depending on the type of list, this may require a loop prevptr delptr We need two pointers: delptr points to node to remove prevptr points to node BEFORE delptr

Removal Steps Algorithm: First Try To remove a list element, we need to: prevptr delptr 1 Set the preceding element to point to the following element of the list Delete the former list element As with list insertion, order matters! 2 delptr->next = null; delete delptr; prevptr->next = delptr->next; Algorithm: Second Try Algorithm Summary prevptr delptr 1 prevptr->next = delptr->next; delptr->next = null; delete delptr; 2 1. Locate element to be removed 2. Re-route pointers around element. Perform cleanup. Delete element Insertion Summary Removal Summary Find insertion point Set the new element to point to the rest of the list Insert the new element Find removal point Use two pointers Re-route pointers around element to be removed Clean up and delete the unwanted element

List Variations Queues Doubly-linked list Each node also contains a pointer to the preceding list node Circular linked lists Last node points to first node, not NULL One pointer (to the tail) stores entire list first-in, first-out (FIFO) data structure Real-world examples: grocery checkout, on/off-ramp Programming examples print jobs, processes running on a CPU Queue Operations Straight Queues 1. Create queue 2. Destroy queue. Enqueue (insert element at tail). Dequeue (remove element at head). Get number of elements (as opposed to circular queues) Maintain two pointers one to head of queue (for removal) one to tail of queue (for insertion) Queue Insertion Queue Removal Special case: empty queue set head and tail to point to new element Otherwise: 1. newnode->next = tail->next; /*or NULL*/ 2. tail->next = newnode; Special case: only one element in queue set tail to point to NULL Otherwise: 1. newptr = head; 2. head = head->next;

Circular Queues Stacks Last node points to first node Can represent a circular queue with one pointer (to tail) Head of queue is just tail->next last-in, first-out (LIFO) data structure Real-world example: stack of books Programming examples: activation stack (for function calls) Stack Operations Stack Insertion (Push) 1. Create stack 2. Destroy stack Very easy just insert at the head:. Push (insert object). Pop (remove object). Peek (view object on top of stack) newnode->next = head; head = newnode; 6. Get number of elements Stack Deletion (Pop) Array-Based Implementations Just as easy as insertion oldnode = head; head = head->next; Stacks and queues are also easy to implement using arrays Track indices of stack top, or queue head and tail Problem is that array is still of fixed size