160 likes | 556 Views
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. Queue: Push. top. Max. Max. top. Lex. Lex. Nika. Nika. Push. Vlad. Vlad. Joe. Joe.
E N D
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
Queue: Push top Max Max top Lex Lex Nika Nika Push Vlad Vlad Joe Joe
Queue: Pop pop Max Max top Lex Lex Nika Nika Vlad Vlad Joe Joe
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;
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.
Implementation front Max A queue can be implemented with the help of a linked list: Lex Nika end Vlad
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
Deque: Push Joe top push_front Joe top Max Max Lex Lex Nika Nika push_end Vlad end Vlad Jane Jane end
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;
Exercise: Operation Priority Figure out the priority of operations in an algebraic expression, e.g. Expression: a*(b+c)-d/e+f Priority: 2 3 1 3 1 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
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
Assignment Read chapter 6, prepare for quiz next class. I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%.