Sei sulla pagina 1di 16

LN #15

(1Hrs)

Algorithms 3

CTPS 2018

Department of CSE,Coimbatore
Stack

Department of CSE,Coimbatore
Stack
• Stack is a linear data structure which follows a particular order in
which the operations are performed. The order is LIFO(Last In
First Out) or FILO(First In Last Out).

• Basic operations
1. Push: Adds an item in the stack.
If the stack is full, then it is an Overflow condition.
2. Pop: Removes an item from the stack
If the stack is empty, then it is an Underflow condition.
3. Peek or Top: Returns top element of stack.
4. isEmpty: Returns true if stack is empty, else false.
5. isFull: Returns true if stack is full, else false.
Department of CSE,Coimbatore
Department of CSE,Coimbatore
Department of CSE,Coimbatore
Department of CSE,Coimbatore
Department of CSE,Coimbatore
Adding item into Delete an item from
stack: push ( ) stack: pop( )
//remove top element
top: current top of stack
n: maximum size
if (top is equal to -1)
then Print “stackempty”
if (top is equal to n-1)
else {
then Print “stackfull”
item = stack(top)
else {
top = top-1
top = top+1
}
stack(top) = item
Return
}
Return

Department of CSE,Coimbatore
Isempty( )

Peek ( ) if (top is equal to -1)


if (top is equal to -1) then Return 1
then { else Return 0
Print “stackempty”
Return
} IsFull( )
else {
element = stack(top) if (top is equal to n-1)
Return element then Return 1
} else Return 0

Department of CSE,Coimbatore
Queue

Department of CSE,Coimbatore
Queue
Queue is a linear data structure which follows a particular order
in which the operations are performed. The order is FIFO (First
In First Out)

Basic operations
1. enqueue() − add an item to the queue.
2. dequeue() − remove an item from the queue.
3. peek() − get the element at the front of the queue without
removing it.
4. isfull() − Check if the queue is full.
5. isempty() − Check if the queue is empty.
Department of CSE,Coimbatore
Department of CSE,Coimbatore
Department of CSE,Coimbatore
Adding item into Delete an item from
queue: enqueue ( ) queue: dequeue( )
if (rear is equal to n)
then Print “queuefull”
else { if (front is equal to rear)
rear =rear+1 then Print “queueempty”
q[rear]=item else {
} front = front+1
item = q[front]
Return }
Return

Department of CSE,Coimbatore
Basis for
Stack Queue
comparison
Working principle LIFO (Last in First out) FIFO (First in First out)

One end is used for insertion,


Same end is used to insert i.e., rear end and another end is
Structure
and delete elements. used for deletion of elements,
i.e., front end.

Number of pointers used One Two

Operations performed Push and Pop Enqueue and dequeue

Examination of empty Front == -1 or Front == Rear


Top == -1
condition +1

Examination of full
condition Top == Max - 1 Rear == Max - 1
Department of CSE,Coimbatore
https://www.tutorialspoint.com/data_structures_a
lgorithms/stack_algorithm.htm
Department of CSE,Coimbatore

Potrebbero piacerti anche