1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Review Learn about linked lists
CHAPTER 4 QUEUE CSEB324 DATA STRUCTURES & ALGORITHM.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
What is a Queue? A queue is a FIFO “first in, first out” structure.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
 Balancing Symbols 3. Applications
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
Linked List Spring 2013Programming and Data Structure1.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Linked Lists EENG 212 ALGORITHMS And DATA STRUCTURES.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Data Structure & Algorithms
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
CS505 Data Structures and Algorithms
CC 215 Data Structures Queue ADT
C++ Plus Data Structures
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Stack and Queue.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
C++ Plus Data Structures
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Stacks and Queues CSE 373 Data Structures.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
CSE 373 Data Structures Lecture 6
Linked Lists.
CSCS-200 Data Structure and Algorithms
Presentation transcript:

1 Queues and Lists

QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out (LIFO) discipline A queue adopts a first-in/first-out (FIFO) model that more closely resembles a waiting line. 2

Queue ADT abstract typedef > queue; /* a queue is a sequence of elements, FIFO = First In First Out */ abstract int empty ( queue q ); postcondition: empty(s) == 1 if queue is empty, == 0 otherwise; abstract int full ( queue q ); postcondition: full(s) == 1 if queue is full, == 0 otherwise; 3

Queue ADT 4 abstract elements delete ( queue q ); /* dequeue */ precondition: empty(q) == 0; postcondition: first element is removed from queue q; abstract void insert ( queue q, elements e ); /* enqueue */ precondition: full(q) == 0; postcondition: element e is added to the end of queue q;

Implementation of Queue 5 #define elements int typedef struct { int size,front,rear; elements *items; } queue;

Implementation of Queue FrontRear B C FrontRear C D E FrontRear C FrontRear

Implementation of Queue 7 queue create ( int n ) { queue q; q.size = n; q.front = -1; /* empty queue has both front and rear == -1 */ q.rear = -1; /* the array of items is viewed as circular */ q.items = (elements*)calloc(n,sizeof(elements)); return q; } int empty ( queue q ) { return (q.rear == -1 ? 1 : 0); }

Implementation of Queue 8 int full ( queue q ){ return (q.front == (q.rear+1) % q.size ? 1 : 0); } elements delete ( queue *q ){ elements e; assert(empty(*q)==0); /* enforce precondition */ if (q->front == q->rear) q->rear = -1; /* queue will be empty */ e = q->items[q->front]; if (q->rear == -1) q->front = -1; /* queue is empty */ else q->front = (q->front+1) % q->size; return e; } B C front rear G C D E FrontRear

Implementation of Queue 9 void insert ( queue *q, elements e ) { assert(full(*q)==0); /* enforce precondition */ q->rear = (q->rear+1) % q->size; q->items[q->rear] = e; if (q->front == -1) /* queue was empty */ q->front = 0; /* queue of one item */ } void clear ( queue *q ) { free(q->items); q->size = 0; q->front = -1; q->rear = -1; } B C front rear

10 Linked List Group of nodes connected by pointers A node consists of Data Pointer to next node 6538 HeadNull

11 Insertion into a Linked List Insert 9 after HeadNull 9

12 Deletion from a Linked List Delete 3 from the list 6538 HeadNull 9 Free this space

13 Declaration of a node struct node { int info; struct node *next; }; typedef struct node node; 6538 HeadNull

14 Linked List Structure 6538 Head 6538 Null Some address In memory Points back to Internal node

15 Dynamic allocation of a node node *ptr; ptr = (node *)malloc(sizeof(node)); ? ptr free(ptr)

16 Inserting at the Head 1. Allocate a new node 2. Insert new element 3. Make new node point to old head 4. Update head to point to new node 6538 HeadNull Head

17 Inserting at the Head void inserthead(node *head, int a) { node *ptr; ptr->info = a; ptr->next = head; head = ptr; } 6538 Head Null Head inserthead(head,2); Memory Problems

18 Inserting at the Head void inserthead(node *head, int a) { node *ptr; ptr = (node*)malloc(sizeof(node)); ptr->info = a; ptr->next = head; head = ptr; } 6538 Head Null Head inserthead(head,2); Can not modify head

19 Inserting at the Head node *inserthead(node *head, int a) { node *ptr; ptr = (node*)malloc(sizeof(node)); ptr->info = a; ptr->next = head; return(ptr); } 6538 Head Null Head head = inserthead(head,2);

20 Removing at the Head 1. Update head to point to next node in the list 2. Allow garbage collector to reclaim the former first node 6538 Head Null 538 Head Null

21 Removing at the Head void deletehead(node *head) { head = head->next; return; } 6538 Head Null 538 Head Null deletehead(head); Memory Leak

22 Removing at the Head void deletehead(node *head) { node * ptr; ptr = head; head = head->next; free(ptr); return; } 6538 Head Null 538 Head Null deletehead(head); Can not modify head

23 Removing at the Head node* deletehead(node *head) { node * ptr; ptr = head; head = head->next; free(ptr); return(head); } 6538 Head Null 538 Head Null head = deletehead(head);

24 Inserting at the Tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 6538 Head Null 6538 Head Null 1

25 Inserting at the Tail node *inserttail(node *head, int a) { node *ptr; node *ptr2 = head; ptr = (node*)malloc(sizeof(node)); ptr->info = a; ptr->next = NULL; if (head == NULL) return (ptr); else if (head->next == NULL) { head->next = ptr; return (head); } while (head->next != NULL) head = head->next; head->next = ptr; return(ptr2); } 6538 Head Null

26 Print a linked list void printlist (node *head) { while (head !=NULL) { printf("%d ",head->info); head = head->next; } printf("\n"); } 6538 HeadNull printlist(head);

27 Find length of a linked list int length(node *head) { if (head == NULL) return 0; else return 1 + length(head->next); } 6538 HeadNull x=length(head);

Linked List Implementation of Queue typedef struct { int size,front,rear; elements *items; } queue; HeadNull

Linked List Implementation of Queue struct node { elements info; struct node *next; }; typedef struct node node; front Null struct queue { node *front; node *rear; } ; typedef struct queue queue rear

Linked List Implementation of Queue front Null int empty ( queue q ); /* postcondition: empty(q) == 1 if queue q is empty, * == 0 otherwise; */ elements delete ( queue *q ); /* precondition: empty(*q) == 0; * postcondition: first element is removed from queue *q; */ void insert ( queue *q, elements e ); /* precondition: full(*q) == 0; * postcondition: element e is inserted to end of queue *q; */ rear

Linked List Implementation of Queue Null queue *q; q frontrear struct

Linked List Implementation of Queue front Null int empty ( queue q ) { if (q.front == NULL) return (1); else return (0); } rear

Linked List Implementation of Queue front Null void insert ( queue *q, elements e ) { node *l; l = (node*)malloc(sizeof(node)); l->info = e; l->next = NULL; rear if (empty(*q) == 1) q->front = l; else q->rear->next = l; q->rear = l; }

Linked List Implementation of Queue front Null elements delete ( queue *q ) { elements e; node *l; assert(empty(*q)==0); l = q->front; e = l->info; l = l->next; rear free(q->front); q->front = l; return e; }

35 Doubly Linked List Group of nodes connected by pointers A node consists of Data Pointer to next node Pointer to previous node 6538 Head Null

36 Doubly Linked List struct cnode { int info; struct cnode *next; struct cnode *previous; }; typedef struct cnode cnode; 6538 Head Null

37 Inserting at the Head cnode *inserthead(cnode *head, int a) { cnode *ptr; ptr = (cnode*)malloc(sizeof(cnode)); ptr->info = a; ptr->next = head; head->previous = ptr; ptr->previous = NULL; return(ptr); } 538 Head Null 6538 Head Null

38 Delete a given Node void delete(cnode *p, int *px) { cnode *q,r; if (p== NULL) { printf(“void deletion\n”); return; } *px = p->info; q = p->previous; r = p->next; 6538 Null Head Null q->next = r; r->previous = q; free(p); return; } p q r

Circular Lists Linked lists have some limitations Given a pointer p, we can not reach preceding nodes In circular lists, next field of last node points to first node A node consists of Data Pointer to next node 6538 HeadNull 6538 Head

Circular Lists Keep a pointer to the last node How can we add or remove an element from either the front or the rear of the list? How can we check if the list is empty? 6538 list First node Last node

Queues as Circular Lists #define elements int struct node { elements info; struct node *next; }; typedef struct node node; 6538 list First node Last node node *insert_item ( node *l, elements i ); /* adds the element i to the rear of the list */ node *delete_item ( node *l, elements *i ); /* deletes the first element from the list */

Queues as Circular Lists node *insert_item ( node *l, int i ) { node *nl; nl = (node*)malloc(sizeof(node)); nl->info = i; if (l == NULL) nl->next = nl; else 6538 l { nl->next = l->next; l->next = nl; } return nl; }

Queues as Circular Lists node *delete_item ( node *l, int *i ) { assert(l != NULL); if (l->next == l) { *i = l->info; free(l); return NULL; } 6538 l else { node *head = l->next; *i = head->info; l->next = head->next; free(head); return l; }

Print a Circular List void print ( node *l ) { if (l != NULL) { node *p; for (p = l->next; p != l; p = p->next) printf(" %d", p->info); printf(" %d", l->info); } 6538 list

Exercises Write a function to create a copy of a linked list. Write a function to free the nodes of a linked list. Write a function to reverse a singly linked list Write a function to insert a number into a sorted linked list Write a function to remove duplicate elements in a sorted linked list