ARRAY IMPLEMENTATION OF LIST ADT
AIM:
To write a program to implement List
ADT using Array.
ALGORITHM:
1.INSERTION
STEP 1: If the list is empty initialize the top pointer by – 1. Read the
data to be
stored in the list. Otherwise go step
2.
STEP 2: Read the data after which the insertion is to be made.
STEP 3: If the data read from the user is found, shift the existing the
data in the
list by one, from its insert position
to the last position and increment the
top pointer.
STEP 4: Read the data to be stored in the insert position of list.
2.DELETION
STEP 1: Read
the data to be deleted.
STEP 2:If the data read from the user is found, left shift the existing
the data in
the list by one, from its delete
position to the last position.
STEP 3: Decrements top pointer by one.
3.SEARCH
STEP 1:Get the element to be searched
STEP 2:Traverse the list by increment the index value by 1 and compare
with
the data
STEP 3: If it is found print the index value and the data
4.DISPLAY
STEP 1: Create
a function display(), print the values by decremented index value by 1
PROGRAM:
#include<iostream.h>
#include<stdlib.h> #include<string.h>
#include<conio.h>
class ARR_LST
{
private:
struct node
{
int data; int next;
}
a[10]; public:
int head;
ARR_LST();
int Create();
void Display(int);
void Insert();
void Delete(); void Search();
};
ARR_LST::ARR_LST()
{
for(int i=0;i<10;i++)
{
a[i].data=-1;
}
}
int ARR_LST::Create()
{
int head,i;
cout<<"\n Enter the index
for first node";
cin>>i;
head=i;
while(i!=-1)
{
cout<<"\n Enter the data
and index of the first element";
cin>>a[i].data;
cout<<" ";
cin>>a[i].next;
i=a[i].next;
}
return head;
}
void ARR_LST::Display(int i)
{
while(i!=-1)
{
if(a[i].data==-1)
cout<<" ";
else
{
cout<<a[i].data<<"->";
}
i=a[i].next;
}
cout<<"NULL";
}
void ARR_LST::Insert()
{
int i,new_data,temp;
cout<<"\n Enter the new
data which is to be inserted";
cin>>new_data;
cout<<"\n Enter the data
after which you want to insert";
cin>>temp;
for(i=0;i<10;i++)
{
if(a[i].data==temp)
break;
}
if(a[i+1].data==-1)
{
a[i+1].next=a[i].next;
a[i].next=i+1;
a[i+1].data=new_data;
}
}
void ARR_LST::Delete()
{
int i,temp,current,new_next;
cout<<"\n Enter the node
to be deleted";
cin>>temp;
for(i=0;i<10;i++)
{
if(a[i].data==temp)
{
if(a[i].next==-1)
{
a[i].data=-1;
}
current=i;
new_next=a[i].next;
}
}
for(i=0;i<10;i++)
{
if(a[i].next==current)
{
a[i].next=new_next;
a[current].data=-1;
}
}
}
void ARR_LST::Search()
{
int i,temp,flag=0;
cout<<"\n Enter the node
to be searched";
cin>>temp;
for(i=0;i<10;i++)
{
if(a[i].data==temp)
{
flag=1;
break;
}
}
if(flag==1)
cout<<"\n
The"<<temp<<"node is present in the list";
else
cout<<"\n The node is not
present";
}
void main()
{
char ans;
int choice;
ARR_LST obj;
do
{
clrscr();
cout<<"\t\t Program for
implementing list using Array";
cout<<"\n Main menu";
cout<<"\n 1.Creation"; cout<<"\n
2.Display";
cout<<"\n 3.Insertion of
element in the list";
cout<<"\n 4.Deletion of
element from the list";
cout<<"\n 5.Searching of
element from the list";
cout<<"\n 6.Exit";
cout<<"\n Enter your
choice";
cin>>choice;
switch(choice)
{
case 1:
obj.head=obj.Create();
break;
case 2:
obj.Display(obj.head);
break;
case 3:
obj.Insert();
break;
case 4:
obj.Delete();
break;
case 5:
obj.Search();
break;
}
cout<<"\n Do you Wish to
go to Main Menu?";
ans=getch();
}
while(ans=='y'||ans=='Y');
getch();
}
OUTPUT:
Program for implementing list using
Array
Main menu \
2.Display
3.Insertion of element in the list
4.Deletion of element from the list
5.Searching of element from the list
6.Exit
Enter your choice 1
Enter the index for first node 2
Enter the data and index of the first
element 10 1 Enter the data and index of the first element 20 -1
Do you Wish to go to Main Menu?
Y
Enter your choice 2
10->20->NULL
Do you Wish to go to Main Menu?y
Enter your choice 3
Enter the new data which is to be
inserted 21
Enter the data after which you want
to insert 20
Do you Wish to go to Main Menu?Y
Enter your choice 2
10->21->20->NULL
Do you Wish to go to Main Menu?y
Enter your choice 4
Enter the node to be deleted 21
Do you Wish to go to Main Menu?y
Enter your choice 2
10->20->NULL
Do you Wish to go to Main Menu?y
Enter your choice 5
Enter the node to be searched 20
The 20 node is present in the list
Do you Wish to go to Main Menu?y
0 Comments