Queues Linear list. One end is called front. Other end is called rear.

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 and Algorithms (60-254)
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)
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.
Data Structure Dr. Mohamed Khafagy.
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 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.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Stacks CSE, POSTECH. 2 2 Stacks Linear list One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Priority Queues, Heaps & Leftist Trees
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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.
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.
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.
Click to edit Master text styles Stacks Data Structure.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
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.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
Queues.
Computing with C# and the .NET Framework
Chapter 18: Stacks and Queues.
Using Queues: Coded Messages
COSC160: Data Structures: Lists and Queues
Stacks 황승원 Fall 2010 CSE, POSTECH.
Stacks Linear list. One end is called top. Other end is called bottom.
Stacks.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CSCE 3110 Data Structures & Algorithm Analysis
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Chapter 19: Stacks and Queues.
Stacks template<class T> class stack { public:
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Stacks and Queues.
Stacks, Queues, and Deques
Stacks.
Stacks template<class T> class stack { public:
Stacks template<class T> class stack { public:
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
پشته ها و صف ها.
Stacks public interface Stack { public boolean empty();
Queues Jyh-Shing Roger Jang (張智星)
Stacks.
Stacks and Queues.
Visit for more Learning Resources
Using a Queue Chapter 8 introduces the queue data type.
Stacks template<class T> class stack { public:
Using a Queue Chapter 8 introduces the queue data type.
Stacks, Queues, and Deques
Queues Linear list. One end is called front. Other end is called rear.
Stacks and Queues.
Queues.
Chapter 4 Stacks and Queues
Data Structures & Programming
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. By comparison, in a stack adds and removes are to/from the same end of he list.

Bus Stop Queue Bus Stop front rear rear rear rear rear

Bus Stop Queue Bus Stop front rear rear rear

Bus Stop Queue Bus Stop front rear rear

Bus Stop Queue Bus Stop front rear rear This is FIFO queue. Some real life queues are not FIFO. Removal from a queue may be done by priority rather than by arrival time order. For example, loading into a plane– first class, frequent fliers, by rows from back of plane rather than by order in which you arrive at the departure gate.

The Abstract Class queue template <class T> class queue { public: virtual ~queue() {} virtual bool empty() const = 0; virtual int size() const = 0; virtual T& front() = 0; virtual T& back() = 0; virtual void pop() = 0; virtual void push(const T& theElement) = 0; };

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 Represent as a grid in which components and already placed wires are denoted by blocked grid positions.

Lee’s Wire Router Label all reachable squares 1 unit from start. start pin end pin Could use rat in a maze animation from Web site instead. Blue squares are blocked squares. Orange squares are available to route a wire. A queue of reachable squares is used. The queue is initially empty, and the start pin square/cell is the examine cell. This cell has a distance value of 0. Unreached unblocked squares adjacent to the examine cell are marked with their distance (this is 1 more than the distance value of the examine cell) and added to the queue. Then a cell is removed from the queue and made the new examine cell. This process is repeated until the end pin is reached or the queue becomes empty. Cells become examine cells in order of their distance from the start pin. Label all reachable squares 1 unit from start.

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

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

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

Lee’s Wire Router start pin 4 3 3 2 2 end pin 1 1 2 2 2 3 4 3 4 4 4 Label all reachable unlabeled squares 5 units from start.

Lee’s Wire Router 5 start pin 4 5 3 3 2 2 end pin 1 1 2 2 2 3 4 3 4 5 4 4 5 5 5 Label all reachable unlabeled squares 6 units from start.

Lee’s Wire Router End pin reached. Traceback. 6 5 6 start pin 4 5 3 3 2 2 end pin 1 1 2 2 2 6 3 4 3 4 5 6 4 4 5 6 5 6 5 6 6 End pin reached. Traceback.

Lee’s Wire Router End pin reached. Traceback. 6 5 6 start pin 4 5 3 3 2 2 end pin 1 1 1 2 2 2 2 6 3 4 3 3 4 4 5 5 6 4 4 5 6 5 6 5 6 6 End pin reached. Traceback.

Derive From arrayList 1 2 3 4 5 6 a b c d e when front is left end of list and rear is right end empty() => queue::empty() O(1) time size() => queue::size(0) front() => get(0) back() => get(size() - 1)

Derive From arrayList 1 2 3 4 5 6 a b c d e pop() => erase(0) 1 2 3 4 5 6 a b c d e pop() => erase(0) O(size) time push(theElement) => insert(size(), theElement) O(1) time

Derive From arrayList 1 2 3 4 5 6 a b c d e when front is right end of list and rear is left end empty() => queue::empty() O(1) time size() => queue::size(0) front() => get(size() - 1) back() => get(0)

Derive From arrayList 1 2 3 4 5 6 a b c d e 1 2 3 4 5 6 a b c d e pop() => erase(size() - 1) O(1) time push(theElement) => insert(0, theElement) O(size) time

Derive From arrayList to perform each opertion in O(1) time (excluding array doubling), we need a customized array representation.

Derive From extendedChain b c d e NULL firstNode lastNode front rear when front is left end of list and rear is right end empty() => extendedChain::empty() O(1) time size() => extendedChain::size()

Derive From ExtendedChain b c d e NULL firstNode lastNode front rear front() => get (0) O(1) time back() => getLast() … new method

Derive From ExtendedChain b c d e NULL firstNode lastNode front rear push(theElement) => push_back(theElement) O(1) time pop() => erase(0)

Derive From extendedChain b a NULL firstNode lastNode rear front when front is right end of list and rear is left end empty() => extendedChain::empty() O(1) time size() => extendedChain::size()

Derive From extendedChain b c d e NULL firstNode lastNode rear front front() => getLast() O(1) time back() => get(0)

Derive From extendedChain b c d e NULL firstNode lastNode rear front push(theElement) => insert(0, theElement) O(1) time pop() => erase(size() - 1) O(size) time

Custom Linked Code Develop a linked class for queue from scratch to get better preformance than obtainable by deriving from extendedChain.

Custom Array Queue Use a 1D array 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] A B C

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

Custom Array Queue Use integer variables theFront and theBack. theFront is one position counterclockwise from first element theBack gives position of last element use front and rear in figures [0] [1] [2] [3] [4] [5] A B C [0] [1] [2] [3] [4] [5] A B C rear rear front front

Push An Element Move rear one clockwise. [0] [1] [2] [3] [4] [5] A B C front rear

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

Pop An Element Move front one clockwise. [0] [1] [2] [3] [4] [5] A B C rear

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

Moving rear Clockwise if (rear = = arrayLength) rear = 0; rear++; [0] [1] [2] [3] [4] [5] A B C front rear rear = (rear + 1) % arrayLength;

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

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

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

Empty That Queue [0] [1] [2] [3] [4] [5] rear front 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.

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

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

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

A Full Tank Please [0] [1] [2] [3] [4] [5] D E front C F B A rear 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 lastOperationIsPush. Following each push set this variable to true. Following each pop set to false. Queue is empty iff (front == rear) && !lastOperationIsPush Queue is full iff (front == rear) && lastOperationIsPush

Ouch!!!!! Remedies (continued). Define an integer variable size. Following each push do size++. Following each pop do size--. Queue is empty iff (size == 0) Queue is full iff (size == arrayLength) Performance is slightly better when first strategy is used. Due to the cost of increments/decrements But we do those operations anyway  size() in O(1)