You are on page 1of 5

Data Structure and Algorithm

Lab Manual 3
Ms. Zainab Imtiaz
Stack
What is a Stack?
• Stack is a data structure in which data is added and removed at only one end called the top.
• To add (push) an item to the stack, it must be placed on the top of the stack.
• To remove (pop) an item from the stack, it must be removed from the top of the stack too.
• Thus, the last element that is pushed into the stack, is the first element to be popped out of
the stack. i.e. Last In First Out (LIFO).
Basic Operations:
push − pushing storing an element on the stack.
pop − removing accessing an element from the stack.
top − get the top data element of the stack, without removing it.
isFull − check if stack is full.
isEmpty − check if stack is empty.

Peek():
Algorithm of peek function:−
begin procedure peek
return stack[top]
end procedure

isfull():
Algorithm of isfull function −
begin procedure isfull
if top equals to MAXSIZE
return true
else
return false
endif
end procedure
isempty():
Algorithm of isempty function −
begin procedure isempty
if top less than 0
return true
else
return false
endif
end procedure

PUSH():
Algorithm for PUSH operation:
begin procedure push:
if stack is full
return null
endif
top ← top + 1
stack[top] ← data
end procedure
POP():
A simple algorithm for Pop operation can be derived as follows −
begin procedure pop
if stack is empty
return null
endif
data ← stack[top]
top ← top - 1
return data
end procedure

You might also like