Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queue Queue – First In / First Out (FIFO) data structure that supports two operations: push for adding new element at the end of the queue pop for removing.

Similar presentations


Presentation on theme: "Queue Queue – First In / First Out (FIFO) data structure that supports two operations: push for adding new element at the end of the queue pop for removing."— Presentation transcript:

1 Queue Queue – First In / First Out (FIFO) data structure that supports two operations: push for adding new element at the end of the queue pop for removing new element at the front of the queue

2 Queue: Push top Max Max top Lex Lex Nika Nika Push Vlad Vlad Joe Joe

3 Queue: Pop pop Max Max top Lex Lex Nika Nika Vlad Vlad Joe Joe

4 Generic queue Class (STL)
#include <queue> queue<string> myQueue; size() returns queue size; front() returns the front queue element; push () adds new element to (the end of) the queue; pop() removes element from (the front of) the queue;

5 Applications Various lists: print queues, message queues, service request queues, process / thread queues; O(1) push & pop operations. Round robin: circular queue (front element linked to last.

6 A queue can be implemented with the help of a linked list:
Implementation A queue can be implemented with the help of a linked list: front Max Lex Nika end Vlad

7 Deque DEQueue – Double-Ended Queue = stack + queue hybrid, supports adding and removing elements at the front and at the end. push for adding new element at the end of the queue pop for removing new element at the front of the queue

8 Deque: Push Joe Joe Max Max Lex Lex Nika Nika Vlad Vlad Jane Jane top
push_front Joe top Max Max Lex Lex Nika Nika push_end Vlad end Vlad Jane Jane end

9 Generic deque Class (STL)
#include <deque> deque<string> myDeque; * supports all of the methods of vector and list (including indexes and iterators) size() returns deque size; front() returns the front deque element; back() returns the back deque element; push_front() - adds new element to the front of the deque; push_end() - adds new element to the end of the deque; pop_front() - removes element from the front of the deque; pop_end() - removes element from the back of the deque;

10 Exercise: Operation Priority
Figure out the priority of operations in an algebraic expression, e.g. Expression: a*(b+c)-d/e+f Priority: Using string and queue Read expression from cin into a string Look at each character and if it is +,-,*,/ determine the priority of arithmetic operation populate the queue with operation in its priority, e.g. (*,2) (+,3) (-,1) (/,3) (+,1) Print the queue contents

11 Operation Priority Solution
Declare user-defined type: struct OperationPriority { char Operation; int Priority; }; Your queue must store OperationPriority objects Assign priority levels to operations: 2 for *,/; 1 for +,- Read expression into string Set nestingLevel=0 Look at each character If the character is ‘(‘ then nestingLevel++ If the character is ‘)‘ then nestingLevel-- If the character is ’*’, ’/’ then priority = nestingLevel*2 + 2 If the character is ’+’, ’-’ then priority = nestingLevel*2 + If the character is ’+’, ’-’, ‘*’, ‘/’ then add it to queue including the priority value cout the queue

12 Assignment Read chapter 6, prepare for quiz next class.
I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%.


Download ppt "Queue Queue – First In / First Out (FIFO) data structure that supports two operations: push for adding new element at the end of the queue pop for removing."

Similar presentations


Ads by Google