Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
CS Data Structures ( 資料結構 ) Chapter 3: Stacks and Queues Spring 2012.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Lec 7 Sept 17 Finish discussion of stack infix to postfix conversion Queue queue ADT implementation of insert, delete etc. an application of queue.
Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only.
Queues CSE, POSTECH 2 2 Queues Like a stack, special kind of linear list One end is called front Other end is called rear Additions (insertions or enqueue)
5/4/2015ITK 2751 Queue a FIFO (First In First Out) efbh front rear poll, remove offer peek, element capacity size.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
CHAPTER 7 Queues.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Introduction to Stacks & Queues.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
Queue Chapter 9 Stacks Last in first out (LIFO)Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 3 1. Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Lecture 8 Feb 19 Goals: l applications of stack l Postfix expression evaluation l Convert infix to postfix l possibly start discussing queue.
Lecture 9 Feb 26 Announcement/discussion: mid-term # 1 (March 10?) Goals: Queue – implementation using array Application to BFS (breadth-first search)
Lecture 11 Sept 26, 2011 Goals convert from infix to postfix.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
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.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Data Structures Using C++
Stacks Chapter 8. Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Queue 09/10/081. Queue (Linear Queue) It is a linear data structure consisting of list of items. In queue, data elements are added at one end, called.
Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Data Structures & Algorithms
Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only. FIFO.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Welcome to CSCE 221 – Data Structures and Algorithms
Midterm Review Linear list Stack queue. What is a data structure What is an abstract data type.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
 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.
1 Chapter 5 Stacks. 2 Topics LIFO (last-in-first-out) structure Implementations –Use class LinearList or Chain –from scratch Applications –parentheses.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
CS505 Data Structures and Algorithms
Stacks Stacks.
Stacks.
Queues Queues Queues.
CSCE 3110 Data Structures & Algorithm Analysis
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Queues Linear list. One end is called front. Other end is called rear.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
پشته ها و صف ها.
Queues Linear list. One end is called front. Other end is called rear.
Presentation transcript:

Queues Linear list. One end is called front. Other end is called rear. Additions are done at the rear only. Removals are made from the front only.

Bus Stop Queue Bus Stop front rear

Bus Stop Queue Bus Stop front rear

Bus Stop Queue Bus Stop front rear

Bus Stop Queue Bus Stop front rear

Queue – formula based (b). Delete(x), (c). Add(D)

ADT of Queue

Revisit Of Stack Applications Applications in which the stack cannot be replaced with a queue. –Parentheses matching. –Towers of Hanoi. –Switchbox routing. –Method invocation and return. –Try-catch-throw implementation. Application in which the stack may be replaced with a queue. –Rat in a maze. Results in finding shortest path to exit.

Wire Routing

Lee’s Wire Router start pin end pin Label all reachable squares 1 unit from start.

Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 2 units from start. 11

Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 3 units from start

Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 4 units from start

Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 5 units from start

Lee’s Wire Router start pin end pin Label all reachable unlabeled squares 6 units from start

Lee’s Wire Router start pin end pin End pin reached. Traceback

Lee’s Wire Router start pin end pin 4 End pin reached. Traceback

Custom Array Queue Use a 1D array queue. queue[] Circular view of array. [0] [1] [2][3] [4] [5]

Custom Array Queue Possible configuration with 3 elements. [0] [1] [2][3] [4] [5] AB C

Custom Array Queue Another possible configuration with 3 elements. [0] [1] [2][3] [4] [5] AB C

Custom Array Queue Use integer variables front and rear. –front is one position counterclockwise from first element –rear gives position of last element [0] [1] [2][3] [4] [5] AB C front rear [0] [1] [2][3] [4] [5] AB C front rear

Add An Element [0] [1] [2][3] [4] [5] AB C front rear Move rear one clockwise.

Add An Element Move rear one clockwise. [0] [1] [2][3] [4] [5] AB C front rear Then put into queue[rear]. D

Remove An Element [0] [1] [2][3] [4] [5] AB C front rear Move front one clockwise.

Remove An Element [0] [1] [2][3] [4] [5] AB C front rear Move front one clockwise. Then extract from queue[front].

Moving rear Clockwise [0] [1] [2][3] [4] [5] AB C front rear rear++; if ( rear = = queue.length) rear = 0; rear = (rear + 1) % queue.length;

Empty That Queue [0] [1] [2][3] [4] [5] AB C front rear

Empty That Queue [0] [1] [2][3] [4] [5] B C front rear

Empty That Queue [0] [1] [2][3] [4] [5] C front rear

Empty That Queue When a series of removes causes the queue to become empty, front = rear. When a queue is constructed, it is empty. So initialize front = rear = 0. [0] [1] [2][3] [4] [5] front rear

A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear

A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear D

A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear DE

A Full Tank Please [0] [1] [2][3] [4] [5] AB C front rear DE F When a series of adds causes the queue to become full, front = rear. So we cannot distinguish between a full queue and an empty queue!

Ouch!!!!! Remedies. –Don’t let the queue get full. When the addition of an element will cause the queue to be full, increase array size. This is what the text does. –Define a boolean variable lastOperationIsPut. Following each put set this variable to true. Following each remove set to false. Queue is empty iff (front == rear) && !lastOperationIsPut Queue is full iff (front == rear) && lastOperationIsPut –Performance is slightly better when first strategy is used.

Queue – formula based

Queue – formula based (cont.)

int main(void) { Queue q(100); // add a few elements q.Add(1); cout << "Queue back is " << q.Last() << endl; q.Add(2); cout << "Queue back is " << q.Last() << endl; q.Add(3); cout << "Queue back is " << q.Last() << endl; q.Add(4); cout << "Queue back is " << q.Last() << endl; // The above statement can be written as: // q.Add(1).Add(2).Add(3).Add(4) cout << "Queue should be 1234, front to back" << endl; Test program

// test empty and size if (q.Empty()) cout << "The queue is empty" << endl; else cout << "The queue is not empty" << endl; int x; while (!q.Empty()) { cout << "Queue front is " << q.First() << endl; q.Delete(x); cout << “Deleted front element" << endl; } return 0; }

Output Queue back is 1 Queue back is 2 Queue back is 3 Queue back is 4 Queue should be 1234, front to back The queue is not empty Queue front is 1 Deleted front element Queue front is 2 Deleted front element Queue front is 3 Deleted front element Queue front is 4

Derive From Chain abcde null firstNode lastNode frontrear

Queue – linked list

Queue – linked list (cont.)

int main(void) { LinkedQueue q; // add a few elements q.Add(1); cout << "Queue back is " << q.Last() << endl; q.Add(2); cout << "Queue back is " << q.Last() << endl; q.Add(3); cout << "Queue back is " << q.Last() << endl; q.Add(4); cout << "Queue back is " << q.Last() << endl; // The above statement can be written as: // q.Add(1).Add(2).Add(3).Add(4) cout << "Queue should be 1234, front to back" << endl; Test program

// test empty and size if (q.Empty()) cout << "The queue is empty" << endl; else cout << "The queue is not empty" << endl; int x; while (!q.Empty()) { cout << "Queue front is " << q.First() << endl; q.Delete(x); cout << “Deleted front element" << endl; } return 0; }

Output Queue back is 1 Queue back is 2 Queue back is 3 Queue back is 4 Queue should be 1234, front to back The queue is not empty Queue front is 1 Deleted front element Queue front is 2 Deleted front element Queue front is 3 Deleted front element Queue front is 4