Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

CSD201 – Data Structures and Algorithms (In

C++)

Chapter4: Stacks and


queues

http://www.fpt.edu.vn/ 1
Chapter topics
• Basic operations
• Implementation of stacks and queues
• Stack and Queue in java.util

http://www.fpt.edu.vn/ 2
Stack operations

• push(el) put the element el on the top of the


• stack;
• pop() take the topmost element from the
• stack;
• topEl() return the topmost element in the stack
• without removing it;
• clear() clear the stack;
• isEmpty() check to see if the stack is empty;

http://www.fpt.edu.vn/ 3
Series of operations on a stack

push 55 push 99 push 44 pop topEl push 22

4
9 9 9
9 9
5 5 5 5 5

http://www.fpt.edu.vn/ 4
Applications of stacks
• Delimiter matching
• Operations on long integers
• History of visited pages through a Web browser
• Undo sequence in a word processor
• Sequence of method/function calls

http://www.fpt.edu.vn/ 5
Queue

enqueue(el) put the element el at the end of the queue;


dequeue() take the first element from the queue;
firstEl() return the first element in the queue
without removing it;
clear() clear the queue;
isEmpty() check to see if the queue is empty;

http://www.fpt.edu.vn/ 6
Series of operations on a queue

enqueue 5 5

5 enqueue 9 9

59 enqueue 7 7

dequeue 5 97

firstEl 97

97 enqueue 2 2

http://www.fpt.edu.vn/ 7
Applications of queues
• Waiting lines in simulation problems
• Scheduling processes by the operating system
• Access to shared resources

http://www.fpt.edu.vn/ 8
Stack vs. queue

enqueue
dequeue

pop

push

stack – a LIFO structure (last in, first out) queue –


a FIFO structure (first in, first out)

http://www.fpt.edu.vn/ 9
Q&A

Thank you for your listening!

http://www.fpt.edu.vn/ 10

You might also like