Professional Documents
Culture Documents
STACK OVERVIEW
Stack ADT
Basic operations of stack
―Pushing, popping etc.
Implementations of stacks using
– array
– linked list
Stack ADT
A stack is a linear data structure in which items are
inserted and deleted at the same end which is
known as the top of the stack
.
Stacks are known as LIFO (Last In, First Out) lists The
last element inserted will be the first to be retrieved
Primary Operations
top
C
top
B BB
top
A A A
top
Primary Operations
Pop
top
B
A
top A
C
B B
A
Pseudocode for Push operation of stack
Sub push(stack1,top,element,size)
If top>=size then
Endif
top=top+1
stack1[top]=element
return
endsub
Pseudocode for Pop operation of stack
sub pop[stack1,top]
if top=0 then
display “stack in underflow”
exit
endif
top<=top-1
return[s(top+1)]
endsub
Application of stack
Conversion of expressions: Using stacks we can
easily convert expressions from infix to postfix,prefix to
postfix etc
Evaluation of expressions: An expression written in
the form of postfix or prefix can be easily evaluated
Recursion: Stacks can also be used in recursive
functions
Runtime memory management: Stacks are used in run time
memory management
Stack Implementation
Stack can be implemented in two different ways:
Implementation of queue
Array
Linked list
QUEUE ADT
A queue is an ordered group of homogeneous items
(elements), in which new elements are added at one end
(the rear), and elements are removed from the other end
(the front).
A queue is like a line of people waiting for a bank teller.
The queue has a front and a rear. A queue is a FIFO
“first in, first out” structure
$ $
Rear Front
Queue Operations
Enqueue: New people must enter the queue at the
rear. The C++ queue class calls this a push, although
it is usually called an enqueue operation
$ $
Front
Rear
Queue Operations
Dequeue :When an item is taken from the queue, it
always comes from the front. The C++ queue calls
this a pop, although it is usually called a dequeue
operation
$ $
Front
Rear
Pseudocode for Enqueue operation of Queue
sub insertrear(rear,item,queue1,queuesize )
If(rear==queuesize-1)
display ”queue overflow”
return
endif
rear=rear+1
queue1[rear]=item
endsub
Pseudocode for Dequeue operation of Queue
sub deletefront(queue1,front,rear)
If(front>rear)
display ”queue underflow”
return
Endif
Display”element deleted is queue1[(front)++)
If(front>rear)
front=0,rear=-1
endif
Endsub
Types of queues
Ordinary Queue
Circular Queue
Priority Queue
Circular Queue
A circular Queue is used to overcome
a disadvantage of an ordinary queue
where we cannot insert elements in
queue once its full even if free slots
are available
Rear=rear+1%queuesize
rather than
Rear=rear+1
Applications of Queues
Direct applications
Indirect applications