You are on page 1of 20

STACKS AND QUEUES

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

• Push:Add an element to the top of the stack


empty stack push an element push another Stack full

top
C
top
B BB
top
A A A
top
Primary Operations
Pop

Remove the element at the top of the stack

Stack Full pop an element pop another empty stack


top

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

Display “stack overflow”


return

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:

1. Contiguous stack: the stack is implemented


as an array.

2. Linked stack: pointers and dynamic memory allocation is

used to implement the stack.


QUEUES OVERVIEW

. Queue ADT
.

Basic operations of queue


Enqueuing, dequeuing etc.

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

 Double Ended 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

A circular Queue solves the problem


by using this equation

Rear=rear+1%queuesize
rather than
Rear=rear+1
Applications of Queues
Direct applications

Waiting lists, bureaucracy


Access to shared resources (e.g., printer)
Multiprogramming

Indirect applications

Auxiliary data structure for algorithms


Component of other data structures
Implementation of Queue

Just as stacks can be implemented as arrays or


linked lists, so with queues.

Dynamic queues have the same advantages


over static queues as dynamic stacks have over
static stacks
THANK YOU

You might also like