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
QueueQueue
Queue: PushQueue: Push
Max
Lex
Nika
VladPushJoe
top Max
Lex
Nika
Vlad
Joe
top
Queue: PopQueue: Pop
pop
Lex
Nika
Vlad
Joe
Max
Lex
Nika
Vlad
Joe
top
Max
#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;
Generic queue Class (STL)Generic queue Class (STL)
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.
ApplicationsApplications
A queue can be implemented with the help of a linked list:
ImplementationImplementation
Max
Lex
Nika
Vladend
front
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
DequeDeque
Deque: PushDeque: Push
Max
Lex
Nika
Vlad
push_frontJoe top
endpush_endJane
Max
Lex
Nika
Vlad
top
end
Joe
Jane
#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;
Generic deque Class (STL)Generic deque Class (STL)
Figure out the priority of operations in an algebraic expression, e.g.
Expression: a*(b+c)-d/e+fPriority: 2 3 1 3 1Using 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
Exercise: Operation PriorityExercise: Operation Priority
1) Declare user-defined type:struct OperationPriority{ char Operation; int Priority;};
2) Your queue must store OperationPriority objects 3) Assign priority levels to operations: 2 for *,/; 1 for +,-4) Read expression into string5) Set nestingLevel=06) 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 value7) cout the queue
Operation Priority SolutionOperation Priority Solution
Read chapter 6, prepare for quiz next class.
I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%.
AssignmentAssignment