queue – first in / first out (fifo) data structure that supports two operations: -push for adding...

12
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 Queue

Upload: godfrey-hawkins

Post on 13-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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

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

Page 2: 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

Queue: PushQueue: Push

Max

Lex

Nika

VladPushJoe

top Max

Lex

Nika

Vlad

Joe

top

Page 3: 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

Queue: PopQueue: Pop

pop

Lex

Nika

Vlad

Joe

Max

Lex

Nika

Vlad

Joe

top

Max

Page 4: 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

#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)

Page 5: 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

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

Page 6: 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

A queue can be implemented with the help of a linked list:

ImplementationImplementation

Max

Lex

Nika

Vladend

front

Page 7: 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

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

Page 8: 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

Deque: PushDeque: Push

Max

Lex

Nika

Vlad

push_frontJoe top

endpush_endJane

Max

Lex

Nika

Vlad

top

end

Joe

Jane

Page 9: 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

#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)

Page 10: 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

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

Page 11: 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

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

Page 12: 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

Read chapter 6, prepare for quiz next class.

I will randomly question 10 students. Correct answer earns 1%, incorrect earns -1%.

AssignmentAssignment