0% found this document useful (0 votes)
20 views

Data Structure and Algorithm: Ms. Zainab Imtiaz Stack

A stack is a data structure that follows the LIFO (Last In First Out) principle. New items are added to the top of the stack and removed from the top. Basic stack operations include push, which adds an item to the top, pop, which removes an item from the top, peek, which returns the top item without removing it, and functions to check if the stack is full or empty. The document then provides pseudocode algorithms for the peek, isfull, isempty, push, and pop functions.

Uploaded by

Usama Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Data Structure and Algorithm: Ms. Zainab Imtiaz Stack

A stack is a data structure that follows the LIFO (Last In First Out) principle. New items are added to the top of the stack and removed from the top. Basic stack operations include push, which adds an item to the top, pop, which removes an item from the top, peek, which returns the top item without removing it, and functions to check if the stack is full or empty. The document then provides pseudocode algorithms for the peek, isfull, isempty, push, and pop functions.

Uploaded by

Usama Tariq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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