Practical - 1: Objective: To Insert and Delete Elements at Specified Locations in An Array
Practical - 1: Objective: To Insert and Delete Elements at Specified Locations in An Array
Practical - 1: Objective: To Insert and Delete Elements at Specified Locations in An Array
34 18
FALSE
Is J>=LOC ?
23 34
TRUE DATA[LOC]=ITEM
67 23
DATA[J+1]=DATA[J]
N 93 67 N=J N=N+1
J=J-1
93 8 N Exit
//CODE : Insert element into Middle of the Array
#include<stdio.h>
main()
{
int DATA[50],LOC,J,N,ITEM;
//Reading size of array DATA
printf("\nHow many elements:");
scanf("%d",&N);
//Reading array DATA
printf("\nRead Array DATA:");
for(int i=1;i<=N;i++)
{
scanf("\n%d",&DATA[i]);
} OUTPUT
//Displaying array DATA before insertion
How Many elements : 5
printf("\nBefore Insertion Array elements are:");
for(int i=1;i<=N;i++) Read Array DATA :
{ 23 45 16 89 9
printf("\n%d",DATA[i]); Before Insertion Array elements are :
} 23
//Algorithm 45
printf("\nRead data element to insert:"); 16
scanf("\n%d",&ITEM);
89
printf("\nEnter the Location where to insert:");
scanf("%d",&LOC); 9
J=N; Read data element to insert : 100
while(J>=LOC) Enter the Location where to insert : 2
{ After Insertion Array elements are :
DATA[J+1]=DATA[J]; 23
J=J-1; 100
}
45
DATA[LOC]=ITEM;
N=N+1; 16
//Displaying array DATA after insertion 89
printf("\nAfter Insertion Array elements are:"); 9
for(int i=1;i<=N;i++)
{
printf("\n%d",DATA[i]);
}
}
(b) Deleting element at specified location from array.
Logic :Let DATA be an array with N elements in computer’s memory . The process of deleting data element
at specified location LOC works as follows :
shift each element one position upward, starting from LOC+1 to Nth element as :
DATA[LOC] = DATA[LOC+1], DATA[LOC+1] = DATA[LOC+2], ,...and so on until DATA[N] is shifted.
Reset length of an array : N = N-1
ALGORITHM:
DELETE_MIDDLE(DATA,N,ITEM,LOC)
[This algorithm delete data element ITEM at specified location LOC from an array DATA
with N elements.]
Step-1: Read LOC [Location where from you want to delete]
Step-2: set ITEM = DATA[LOC]
Step-3: Print “ITEM” is deleted.
Step=4: Repeat Steps (a) & (b) until LOC<=N-1
(a) set DATA[LOC] = DATA[LOC+1] [Shift element one position upward]
(b) set LOC = LOC+1
[End Step-4]
Step-5 : set N = N-1 [reset array length]
Step-6 : Exit
DATA DATA
DATA[1] 45 45 1
DATA[2] 15 15 2
LOC DATA[3] 18 34 3 LOC
DATA[4] 34 23 4
DATA[5] 23 67 5
DATA[6] 67 93 6 N
N DATA[7] 93 7 N
//CODE : Delete element at specified location from Array
#include<stdio.h>
main()
{
int DATA[50],LOC,J,N,ITEM;
//Reading size of array DATA
printf("\nHow many elements:");
scanf("%d",&N);
//Reading array DATA
printf("\nRead Array DATA:");
for(int i=1;i<=N;i++)
OUTPUT
{ How Many elements : 5
scanf("\n%d",&DATA[i]); Read Array DATA :
} 23 45 16 89 9
//Displaying array DATA before deletion Before Deletion Array elements are :
printf("\nBefore Deletion Array elements are:");
23
for(int i=1;i<=N;i++)
{
45
printf("\n%d",DATA[i]); 16
} 89
//Algorithm 9
printf("\nEnter the Location where to delete:"); Enter the Location where to delete : 4
scanf("%d",&LOC); Deleted element is : 89
ITEM=DATA[LOC];
After Deletion Array elements are :
printf(“\nDeleted element is :%d”,ITEM);
while(LOC<=N-1)
23
{ 45
DATA[LOC]=DATA[LOC+1]; 16
LOC=LOC+1; 9
}
N=N-1;
//Displaying array DATA after insertion
printf("\nAfter Insertion Array elements are:");
for(int i=1;i<=N;i++)
{
printf("\n%d",DATA[i]);
}
}
PRACTICAL – 2
Objective : To implement a stack and demonstrate push, pop and peek operations on it.
Stack :- Stack is an ordered collection of data elements which allows insertion & deletion of data elements from
one end only , called the top of the stack (TOP). In other words , in stack , data elements are always removed in the
reverse order in which they were inserted. Due to this property, such type of data structure is also called LIFO (Last-In-
First-Out) type of data structure because data element inserted at last will be removed first.
REMEMBER : Every time TOP is incremented by 1, whenever a new data element is inserted (only if
TOP<STKMAX) and decremented by 1, whenever a data element is deleted from STACK (only if (TOP>=1)
Operations on Stack
POP(STACK,ITEM,TOP) start
[This algorithm delete a data element ITEM from TOP of the stack, where STACK
is an array & TOP is a variable which points to the element to be deleted] FALSE
Is TOP==0 ?
Step-1: Check if TOP==0, then
print” ERROR : STACK Underflow” [ goto step-2] TRUE ITEM=STACK[TOP]
Otherwise,
(a) set ITEM =STACK[TOP] [data element to be deleted] “ERROR: STACK ITEM is deleted
Underflow”
(b) print “ ITEM is deleted”
(c) set TOP=TOP-1 [Decrement TOP by 1] TOP =TOP - 1
[EndIF]
Step-2 : Exit
Exit
PEAK(STACK,ITEM,TOP) start
[This algorithm returns the TOP element of the stack, where STACK is an
array & TOP is a variable which points to the top element in the stack] FALSE
Is TOP==0 ?
Step-1: Check if TOP==0, then
print” ERROR : STACK Underflow” [ goto step-2] TRUE
ITEM=STACK[TOP]
Otherwise,
(a) set ITEM =STACK[TOP] [data element to be returned] “ERROR: STACK
Underflow”
(b) print “ ITEM is the top element in the stack ”
ITEM is the top
[EndIF] element in the stack
Step-2 : Exit
Exit
Menu Driven Program in C Language to implement Stack –using Array
#include<stdio.h>
#define STKMAX 5
int STACK[50],TOP; //Global Variable declaration
//Function to PUSH data element into stack
void traverse();//Function prototype
void push()
{
int ITEM;
if(TOP==STKMAX)
printf("\n\n==> Stack is FULL !!!");
else
{
printf("\n\nEnter Data Element :");
scanf("%d",&ITEM);
TOP++;
STACK[TOP]=ITEM;
traverse();
}
}
//Function to POP from stack
void pop()
{
int ITEM;
if(TOP==0)
printf("\n\n==> Stack is EMPTY..!!!");
else
{
ITEM=STACK[TOP];
printf("\n\n==> Deleted Element is = %d",ITEM);
TOP--;
traverse();
}
}
//Function to return peak (top) element
void peak()
{
if(TOP==0)
printf("\n\nStack is EMPTY...!!!");
else
{
printf("\n\n==> Element at top of the stack is : %d",STACK[TOP]);
traverse();
}
}
//Function to traverse stack
void traverse()
{
int i;
if(TOP==0)
printf("\n\n==> ERROR: Stack is EMPTY..!!!");
else
{
printf("\n\n==>Stack Status :\n");
for(i=TOP;i>=1;i--)
{
printf("\n\tSTACK[%d] : %d",i,STACK[i]);
}
}
}
//Defining main function
int main()
{
int ch;
TOP=0; //Initialize stack
do{
printf("\n\n---MAIN MENU---");
printf("\nPress :1 to PUSH");
printf("\nPress :2 to POP");
printf("\nPress :3 to PEAK");
printf("\nPress :4 to TRAVERSE");
printf("\nPress :5 to EXIT");
printf("\n\nEnter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: peak();
break;
case 4: traverse();
break;
default : printf("\nError : Invalid Choice ?? Please re-enter ..!!");
}
}while(ch!=5);
}
OUTPUT