Data Structure Lesson 2 Stackn Queuecmp313
Data Structure Lesson 2 Stackn Queuecmp313
Email: [email protected]
[email protected]
TEL NO: EXT. 4502/ +6011133097094
FACEBOOK: rashidah lanre
Data Structure: Stack
and Queue
TOPIC 2
Queues
8
Queue
Analysis of Algorithms
A queue represents a waiting list. A queue can
be viewed as a special type of list, where the
elements are inserted into the end (tail) of the
queue, and are accessed and deleted from the
beginning (head) of the queue.
9
Array implementation of queues
front = 0 rear = 3
Initial queue: 17 23 97 44
front = 1 rear = 4
If you know where the last node in a list is, it’s hard to
remove that node, but it’s easy to add a node after it
Node to be
last enqueued
first
44 97 23 17
last
first
44 97 23 17
14
15
With a linked-
With an array
list
implementation:
implementation:
Queue Operations
Analysis of Algorithms
Queue Animation
Queue Animation
http://liveexample.pearsoncmg.com/liang/animation/web/Queue.
html
17
18
https://people.ok.ubc.ca/ylucet/DS/QueueArray.html
▪ https://people.ok.ubc.ca/ylucet/DS/Qu
https://people.ok.ubc.ca/ylucet/DS/QueueArray.html
eueArray.html
Queue https://people.ok.ubc.ca/ylucet/DS/QueueLL.html
▪ https://people.ok.ubc.ca/ylucet/DS/Qu
implementation https://people.ok.ubc.ca/ylucet/DS/QueueLL.html
eueLL.html
(Array and LL)
Analysis of Algorithms
Priority Queue
A regular queue is a first-in
and first-out data structure.
19
Stacks
23
24
Analysis of Algorithms
Abstract Data Types
Direct applications
• Page-visited history in a Web
browser
• Undo sequence in a text editor
• Reversing of strings
• Chain of method/function calls
Applications (Recursion)
of Stacks • Compiler : Postfix notation
Indirect applications
• Auxiliary data structure for
algorithms
• Component of other data
structures
Pushing and popping
0 1 2 3 4 5 6 7 8 9
stk: 17 23 97 44
top = 3 or count = 4
myStack:
44 97 23 17
Stack operations
Analysis of Algorithms
Stack Animation
www.cs.armstrong.edu/liang/animation/StackAnimation.html
www.cs.armstrong.edu/liang/animation/StackAnimation.html
34
35
https://people.ok.ubc.ca/ylucet/DS/StackArray.html
▪ https://people.ok.ubc.ca/ylucet/DS/Sta
https://people.ok.ubc.ca/ylucet/DS/StackArray.html
ckArray.html
Stack Animation
https://people.ok.ubc.ca/ylucet/DS/StackLL.html
▪ https://people.ok.ubc.ca/ylucet/DS/Sta
(Array and LL
https://people.ok.ubc.ca/ylucet/DS/StackLL.html
ckLL.html
based)
Analysis of Algorithms
36
Python Stack
Implementation
Programming class
Analysis of Algorithms
▪Using an array list to implement Stack
▪Use a linked list to implement a Queue
37