1 / 12

Queue

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.

guang
Download Presentation

Queue

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Implementation front Max A queue can be implemented with the help of a linked list: 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 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: 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

  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%.

More Related