Practical - 1: Objective: To Insert and Delete Elements at Specified Locations in An Array

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

PRACTICAL – 1

Objective : To insert and delete elements at specified locations in an array.


(a) Inserting element at specified location in an array.
Logic: Let DATA be an array with N elements in computer’s memory . The process of inserting data
element ITEM at specified location LOC works as follows :
First create space in array DATA to insert new data element by shifting each element one position
downward, starting from Nth element to the required position LOC as :
DATA[N+1] = DATA[N], DATA[N] = DATA[N-1], DATA[N-1] = DATA[N-2],...and so on until
DATA[LOC] is shifted.
Then insert data element ITEM at specified location LOC as :
DATA[LOC] = ITEM
Reset length of an array : N = N+1
ALGORITHM:
INSERT_MIDDLE(DATA,N,ITEM,LOC)
[This algorithm insert data element ITEM at specified location LOC into an array DATA
with N elements.]
Step-1: Read ITEM [data element to be inserted]
Step-2: Read LOC [Location where to insert]
Step-3: Initialize : J = N [J is temporary variable initialised to N]
Step=4: Repeat steps (a) & (b) until J>=LOC
(a) set DATA[J+1] = DATA[J] [Shift data element one position
downward]
(b) set J = J-1 [Decrement J by one]
[End Step-4]
Step-5: set DATA[LOC] = ITEM [insert new data element at specified location LOC]
Step-6 : set N = N+1 [reset array length]
Step-7 : Exit
Exp: Flow Chart
ITEM=100
start

DATA DATA Read ITEM


45 45
Read LOC
15 15

LOC 18 100 LOC J=N

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

Exp : Flow Chart

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.

1. Static Implementation – Using Arrays


Representation of stack :- Basic Terminology
: STACK : is a an array
 STKMAX : is variable , indicates maximum size of STACK.

 TOP : is a variable which always points to currently inserted data


element and data element to be deleted also.
 STACK OVERFLOW :- TOP==STKMAX ? is a special
condition occurs when TOP reaches the STKMAX (maximum
size) of STACK. It indicates that STACK is FULL and no more
data elements can be inserted.
 STACK UNDERFLOW :- TOP==0 ? is a special condition occurs
when TOP=0 & we try to delete from STACK. It indicates that
STACK is EMPTY i.e. can not delete from stack.

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

1. PUSH : Inserting data element at the TOP of stack.

2. POP : Deleting data element from the TOP of stack.

3. PEAK : Return TOP element of the stack.

4. TRAVERSE : Visiting each element of stack.


1. ALGORITHM : PUSH into Stack FLOWCHART
PUSH(STACK,ITEM,TOP,STKMAX)
[This algorithm insert a data element ITEM at TOP of the stack, where STACK is
an array with size STKMAX & TOP is a variable points to the currently inserted
ITEM]
Step-1: Check if TOP==STKMAX, then
print” ERROR : STACK Overflow” [ goto step-2]
Otherwise,
(a) Read ITEM [data element to be inserted]
(b) set TOP=TOP+1 [Increment TOP by 1]
(c) set STACK[TOP]=ITEM [store ITEM at TOP of STACK]
[EndIF]
Step-2 : Exit

2. ALGORITHM : POP from Stack FLOWCHART

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

3. ALGORITHM : PEAK of Stack FLOWCHART

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy