2b Stacks Queues
2b Stacks Queues
• Stacks
• Queues
• Stacks
• Introduction to Stacks
• Array representation of Stacks
• Linked representation of Stacks
• Applications of Stacks
• Introduction to Stacks
• Stack is a linear data structure
• Elements in a stack are added/deleted only from one end (called top)
• Stack is called a LIFO (Last-In-First-Out) data structure
• Operations on a stack: push(), pop()
• Introduction to Stacks
• Last-In-First-Out (LIFO)
top
E
top top
D D D
C top C C C
B top B B B B
top A A A A A
• Introduction to Stacks
• Example of function call
#include <stdio.h> X=?
main(){ invoke fact(5)
int x; invoke fact(4)
x = fact(5); invoke fact(3)
} invoke fact(2)
invoke fact(1)
int fact(int n){ return from fact(1) = 1
if (n>1) return from fact(2) = 2
return n*fact(n-1); return from fact(3) = 6
else return from fact(4) = 24
return 1; return from fact(5) = 120
}
Data Structures & Algorithms 6
…Stacks
• Array representation of stacks
• Variable top stores the address of the topmost element of the stack,
the element will be added to or deleted from top
• Variable MAX is used to store the maximum number of elements that
the stack can hold.
ð top = -1, stack is empty; top = MAX–1, the stack is full
• Example,
Top = 4
• top = 4, so insertions/deletions will be done at this position.
• five more elements can still be stored.
Data Structures & Algorithms 7
…Stacks
• Every node has two parts: data & the address of the next node
• The start pointer of the linked list is used as top.
• Additions/deletions are done at the node pointed by top.
• top = NULL, stack is empty
• Boundary conditions
• top = NULL iff the ith stack is empty and
• IS_FULL(temp) iff the memory is full
• Comparing representations
• Array representation of Stacks
• Fixed size (cannot grow and shrink dynamically)
• Linked representation of Stacks
• May need to perform realloc() calls when the currently allocated size
is exceeded
• But push and pop operations can be very fast
• Applications of Stacks
• Reversing a list
• Parentheses checker
• Matching parentheses and HTML Tags
• Conversion of an infix expression into a postfix expression
• Evaluation of a postfix expression
• Conversion of an infix expression into a prefix expression
• Evaluation of a prefix expression
• Recursion
• Tower of Hanoi
• …
a - (b + c * d)/ e
ðabcd*+e/-
case operator:
aStack.push(); break; // save new operator
} } // end of switch and for
// append the operators in the stack to postfixExpr
while (!isStack.isEmpty()) {
postfixExpr=postfixExpr+(top of stack);
aStack(pop);
}
• Queues
• Introduction to Queues
• Array representation of Queues
• Linked representation of Queues
• Applications of Queues
• Introduction to Queues
• First-In-First-Out (FIFO) list
rear
D
rear C D rear
C
B rear B B C
rear front front A front B front
A A A
front
• Introduction to Queues
• Example of Job scheduling
J5 J6 J5
[0] [5] [0] [5]
front =0
front =4
rear = 5
rear =3
Data Structures & Algorithms 35
…Queues
front
rear
element link
NULL
• Every element has two parts: data & the address of the next element
• The start pointer of the linked list is used as front. The rear pointer store the
address of the last element in the queue.
• Additions will be done at the rear, deletions will be done at the front.
• front = rear = NULL, the queue is empty.
add
delete
• Boundary conditions
• front = NULL iff the ith queue is empty and
• IS_FULL(temp) iff the memory is full
• Comparing representations
• Array representation of Queues
• A statically allocated array
– Prevents the enqueue operation from adding an item to the queue if
the array is full
• A resizable array or a reference-based implementation
– Does not impose this restriction on the enqueue operation
• Linked representation of Queues
• A linked list implementation
– More efficient; no size limit
• Applications of Queues
• Job scheduling
• Waiting lists for a single shared resource like printer, disk, CPU.
• Transfer data asynchronously (data not necessarily received at same rate as sent)
between two processes (IO buffers), e.g., fileIO, sockets.
• Buffers on MP3 players and portable CD players, iPod playlist.
• Playlist to add songs to the end, play from the front of the list.
• Operating system for handling interrupts.
• …
r o t a t o r
• Queue:
Front Back
r Top
o
t
• Stack:
a
The results of inserting a string
t
into both a queue and a stack
o
r
Data Structures & Algorithms 46
…Queues
• Stacks
• Queues