Course Code: AI203
Course Name: Data Structures &
Algorithms
Prof. Pragati P. Patil
Unit – II : Linear Data Structures
• Stack: Definition, Representation and Applications of
Stack.
• Queue: Definitions, Representation and Applications of
Linear Queue, Circular Queue, and Priority Queue.
Introduction to Array
• An array is defined as a set of finite number of homogeneous
elements or same data items.
• It means an array can contain one type of data only, either all
integer, all float-point number or all character.
• Simply, declaration of array is as follows: int arr[10] ;
• Where int specifies the data type or type of elements arrays stores.
• “arr” is the name of array & the number specified inside the square
brackets is the number of elements an array can store; this is also
called sized or length of array.
Introduction to Array
• The elements of array will always be stored in the consecutive (continues) memory
location.
• The number of elements that can be stored in an array, that is the size of array or
its length is given by the following equation: (Upperbound - lowerbound)+1
• For the above array it would be (9-0)+1=10,where 0 is the lower bound of array
and 9 is the upper bound of array.
• Array can always be read or written through loop.
for(i=0;i<=9;i++)
{
scanf(“%d”,&arr[i]);
printf(“%d”,arr[i]);
}
Memory Allocation of Array
• The elements of linear array are stored in consecutive memory
locations. It is shown below:
• Address of element a[k]=B + w * k
Types of Array
• Single Dimension Array
◦ Array with one subscript
• Two Dimension Array
◦ Array with two subscripts (Rows and Column)
• Multi Dimension Array
◦ Array with Multiple subscripts
Basic operations of Array
• Some common operation performed on array are:
◦ Traversing
◦ Searching
◦ Insertion
◦ Deletion
◦ Sorting
◦ Merging
Stack
• Stack is used as Linear data structure
which can be accessed from only one
end .
• LIFO implies that the element that is
inserted last, comes out first and FILO
implies that the element that is inserted
first, comes out last.
• Stack is Ordered List of Elements of
Same Type.
• Stack is Linear List
• In Stack all Operations such as Insertion
Stack
• Common Example :
• Suppose at your home you have
multiple chairs then you put
them together to form a vertical
pile.
• From that vertical pile the chair
which was placed first is
removed last. In this way we can
see how stack is related to us.
Types of Stack
• Fixed Size Stack :
• Stack has a fixed size and cannot grow or shrink dynamically.
• If the stack is full and an attempt is made to add an element to it, an overflow
error occurs.
• If the stack is empty and an attempt is made to remove an element from it, an
underflow error occurs.
• Dynamic Size Stack :
• A dynamic size stack can grow or shrink dynamically.
• When the stack is full, it automatically increases its size to accommodate the
new element.
• When the stack is empty, it decreases its size.
• This type of stack is implemented using a linked list, as it allows for easy
Array Representation of Stack in C
Programming
• 1-D array is used to hold the element of the stack.
• Variable “top” keeps track of “Topmost” Element in the
stack.
• “MAX” is used as Constant which gives maximum size of
Stack.
Values of stack and top
Values of stack and top
Visual Representation of Stack
• View 1 : When Stack is Empty
• When Stack is said to empty then it does not contain any
element inside it.
• Whenever the Stack is Empty the position of topmost
element is -1.
Visual Representation of Stack
• View 2 : When Stack is Not Empty
• Whenever we add very first element then topmost
position will be incremented by 1.
• After adding First Element top = 0.
Visual Representation of Stack
• View 3 : Deletion of 1 element
• After Deletion of 1 Element Top Will be Decremented by
1
Basic Operations on Stack
In order to make manipulations in a stack, there are certain
operations provided to us.
• push() to insert an element into the stack
• pop() to remove an element from the stack
• top() Returns the top element of the stack.
• isEmpty() returns true if stack is empty else false.
• isFull() returns true if the stack is full else false.
To implement stack, we need to maintain reference to the top
item.
Push Operation on Stack
• Stacks may be represented in the computer in various ways,
usually by means of a one-way list or Linear array STACK .
• A variable TOP, which contains the location of the top element
of the stack, and a variable MAX which gives the maximum
number of elements that can be held by the array STACK.
• The condition TOP = -1 or TOP = NULL will indicate that the
stack is empty.
• The condition TOP = MAX-1 will indicate that the stack is full.
Push Operation on Stack
The steps involved in the PUSH operation is given below:
• In executing the procedure PUSH, one must first test whether there is
space in the stack for the new item(If(TOP=MAX-1)).
• If stack if full, then we have the condition known as overflow.
• When we initialize a stack, we set the value of top as -1 to check that
the stack is empty.
• When the new element is pushed in a stack, first, the value of the top
gets incremented, i.e., top=top+1, and the element will be placed at
the new position of the top.
• The elements will be inserted until we reach the max size of the stack.
Algorithm for Push Operation on
Stack
• PUSH(STACK, ITEM)
//This procedure inser the top element
of STACK and assigns it to the variable ITEM.
Step-1: If TOP = Max-1
Print “Overflow”
Goto Step 4
Step-2: Set TOP= TOP + 1 (Increased top by 1)
Step-3: Set Stack[TOP]= ELEMENT
Step-4: END
POP Operation on Stack
• While executing the procedure POP, we must first test
whether there is an element in the stack to be deleted
• If there is no element to delete (If(TOP<0)or(TOP==-1))
then there is the condition known as underflow.
• If element for deletion is available in stack then first
remove that element from stack (Item=Stack[TOP]) and
then decrease TOP by 1 (TOP=TOP-1).
POP Operation on Stack
The steps involved in the POP operation is given below:
• Before deleting the element from the stack, we check whether the
stack is empty.
• If we try to delete the element from the empty stack, then the
underflow condition occurs.
• If the stack is not empty, we first access the element which is pointed
by the top.
• Once the pop operation is performed, the top is decremented by 1,
i.e., top=top-1.
Algorithm: POP Operation on Stack
• POP(ITEM)
//This procedure deletes the top
element of STACK and assigns it to the
variable ITEM.
Step-1: If TOP= 0 or TOP=-1
Print “Underflow”
Goto Step 4
Step-2: Set VAL= Stack[TOP] (assign top ele.
to val)
Step-3: Set TOP= TOP-1 (Decreased top by 1)
Step-4: END
PEEK Operation on Stack
• When we need to return the Algorithm:
value of the topmost element of
the Stack without deleting it from Step-1: If TOP = NULL
the Stack, the Peek operation is
PRINT “Stack is Empty”
used.
• This operation first checks if the Goto Step 3
Stack is empty, i.e., TOP = NULL;
if it is so, then an appropriate Step-2: Return Stack[TOP]
message will display, else the
value will return. Step-3: END
Animation
• https://yongdanielliang.github.io/animation/web/Sta
ck.html
C Program of Stack Using Array
#include<stdio.h> case 1: {
int stack[100], choice, n, top, x, i; push();
void push(void); break; }
void pop(void); case 2:{
void display(void); pop();
int main() break; }
{ case 3: {
top=-1; display();
printf("\n Enter the size of STACK"); break; }
scanf("%d", &max); case 4: {
printf("\n\t EXIT POINT ");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t break; }
4.EXIT"); default: {
do printf ("\n\t Please Enter a Valid
{ Choice(1/2/3/4)"); }
printf("\n Enter the Choice:"); }
scanf("%d",&choice); } while(choice!=4);
switch(choice) { return 0;}
C Program of Stack Using Array
void push() {
if(top==max-1) { void display()
printf("\n\tSTACK is over flow"); } {
else { if(top>=0)
printf(" Enter a value to be pushed:"); {
scanf("%d",&x); printf("\n The elements in STACK \
top=top+1; n");
stack[top]=x; }
for(i=top; i>=0; i--)
}
printf("\n%d",stack[i]);
void pop() {
if(top==-1) {
}
printf("\n\t Stack is under flow"); } else
else { {
item=stack[top]; printf("\n The STACK is empty");
top=top-1;; }
printf("\n\t The popped elements is
%d",item); } }
}
Applications of Stack
• A Stack can be used for evaluating expressions consisting of
operands and operators.
• Stacks can be used for Backtracking, i.e., to check parenthesis
matching in an expression.
• It can also be used to convert one form of expression to another
form.
• It can be used for systematic Memory Management.
Advantages of Stack
• A Stack helps to manage the data in the ‘Last in First out’
method.
• When the variable is not used outside the function in any
program, the Stack can be used.
• It allows you to control and handle memory allocation and
deallocation.
• It helps to automatically clean up the objects.
Disadvantages of Stack
• It is difficult in Stack to create many objects as it increases the
risk of the Stack overflow.
• It has very limited memory.
• In Stack, random access is not possible.
Q. 1 Consider the following stack of city names:
STACK: London, Berlin, Rome, Paris, _____, _____
Examine the stack contents as the following operations take
place:
1. PUSH(STACK, Athens)
2. POP(STACK, ITEM)
3. POP(STACK, ITEM)
4. PUSH(STACK, Madrid)
5. PUSH(STACK, Moscow)
6. POP(STACK, ITEM)
7. PEEK
Answer: INITIAL STACK: London, Berlin, Rome, Paris,
_____, _____
•Step-by-step process: 4. PUSH(STACK, Madrid):
1. PUSH(STACK, Athens): Add "Madrid" to the stack.
Add "Athens" to the stack. Stack after operation: London, Berlin, Rome,
Stack after operation: London, Berlin, Rome, Madrid, _____, _____
Paris, Athens, _____
5. PUSH(STACK, Moscow):
2. POP(STACK, ITEM): Add "Moscow" to the stack.
Remove the topmost element from the stack, which Stack after operation: London, Berlin, Rome,
is "Athens". Madrid, Moscow, _____
Stack after operation: London, Berlin, Rome,
6. POP(STACK, ITEM):
Paris, _____, _____
ITEM removed: Athens Remove the topmost element from the stack, which is
"Moscow".
3. POP(STACK, ITEM): Stack after operation: London, Berlin, Rome,
Remove the next topmost element from the stack, Madrid, _____, _____
which is "Paris".
ITEM removed: Moscow
Stack after operation: London, Berlin, Rome,
_____, _____, _____ Final Stack: London, Berlin, Rome, Madrid, _____, _____
ITEM removed: Paris Removed Items (in order): Athens, Paris, Moscow
Q. 2 Consider the following stack of integers:
STACK: 10, 20, 30, 40, ___, ___
Examine the stack contents as the following operations
take place:
1. PUSH(STACK, 50) Final Answers:
2. POP(STACK, ITEM) 1. Values popped from the stack:
3. PUSH(STACK, 60) 2. Number of elements remaining
in the stack:
4. PUSH(STACK, 70)
5. POP(STACK, ITEM)
6. POP(STACK, ITEM)
7. PUSH(STACK, 80)
Answer: INITIAL STACK: 10, 20, 30, 40, ___, ___
•Step-by-step process: 5. POP(STACK, ITEM):
Remove the topmost element (70)
1. PUSH(STACK, 50): from the stack.
Add “50" to the stack. ITEM: 70
Stack after operation: 10, 20, 30, 40, 50, _____ STACK: 10, 20, 30, 40, 60, _____
6. POP(STACK, ITEM):
2. POP(STACK, ITEM):
Remove the topmost element (60)
Remove the topmost element from the stack,
from the stack.
which is “50".
ITEM: 60
Stack after operation: 10, 20, 30, 40, _____, STACK: 10, 20, 30, 40, _____, _____
_____ 7. PUSH(STACK, 80):
ITEM removed: 50 Add 80 to the stack.
STACK: 10, 20, 30, 40, 80, _____
3. PUSH(STACK, 60):
Final Answers:
Add “60” to the stack.
1. Final state of the stack:
Stack after operation: 10, 20, 30, 40, 60, _____ STACK: 10, 20, 30, 40, 80
4. PUSH(STACK, 70): 2. Values popped from the stack:
Add “70" to the stack.
50, 70, 60
3. Number of elements remaining in the
Stack after operation: 10, 20, 30, 40, 60, 70
stack:
5 elements (10, 20, 30, 40, 80)
Q. 3 Consider the following stack of integers:
STACK: A, B, C, _____, _____, _____
Examine the stack contents as the following operations take
place:
•PUSH(STACK, D)
•PUSH(STACK, E)
•POP(STACK, ITEM)
•PUSH(STACK, F)
•POP(STACK, ITEM)
•POP(STACK, ITEM)
•PUSH(STACK, G)
Q. 4 Consider the following stack of integers:
STACK: 1, 2, 3, _____, _____, _____, _____
Examine the stack contents as the following
operations take place:
7. PUSH(STACK, 8)
1. PUSH(STACK, 4)
8. POP(STACK, ITEM)
2. POP(STACK, ITEM)
9. POP(STACK, ITEM)
3. PUSH(STACK, 5)
10. PUSH(STACK, 9)
4. PUSH(STACK, 6)
11. PUSH(STACK, 10)
5. POP(STACK, ITEM)
12. POP(STACK, ITEM)
6. PUSH(STACK, 7)
Stack Applications
1. Expression Evolution
2. Expression conversion
a. Infix to Postfix.
b. Infix to Prefix.
c. Postfix to Infix.
d. Prefix to Infix.
3. Parsing
4. Simulation of recursion
5. Function call