Professional Documents
Culture Documents
AIM:
To write a C++ program to implement LIST ADT using arrays
ALGORITHM:
Step 1 : Start
Step 2: Create a class arrayadt and declare integers and a function void create in public mode.
Step 3 : Inside this function , get the size of the array as an input and get the elements as the input.
Step 4 : Create two functions void Isfull and Isempty and check whether the array is full or empty.
Step 5 : Create another function void Insert and get the element and the position in the array to be
inserted using the MAX 10 header file inclusion.
Step 6 : Create another function void Delete and get the element and the position in the array to be
deleted.
Step 7: Create a function void Find and get the element as the input to be searched and print its
position.
Step 8 : Create a function void KTh and get the position of the element to be searched as the input
and print its position.
Step 9 : Create a function void Print and traverse and print all the elements of the array by using a
simple for loop.
Step 10 : Create a object for the class and invoke the member functions using the object.
PROGRAM:
#include<iostream>
#include<array>
#define MAX 10
class arrayadt
public:
int a,i,size,n,p,e,f,pos,b[20],x;
void create()
cout<<"Creating a list"<<endl;
cin>>size;
for(i=0;i<size;i++)
cin>>b[i];
//cout<<endl;
void Isfull()
if(size==0)
else
//cout<<endl;
void Isempty()
if(size==0)
else
{
cout<<"The array is full"<<endl;
void Insert()
cin>>pos;
if(pos>=size)
cout<<"Invalid position"<<endl;
else
for(i=MAX;i>=pos;i--)
b[i]=b[i-1];
cin>>p;
b[pos]=p;
size++;
for(i=0;i<size;i++)
cout<<b[i]<<"\t";
void Delete()
if(pos>=size)
cout<<"Invalid position"<<endl;
else
for(i=pos+1;i<size;i++)
b[i-1]=b[i];
size--;
for(i=0;i<size;i++)
cout<<b[i]<<"\t";
void Find()
cin>>x;
for(i=0;i<size;i++)
if(b[i]==x)
cout<<"Element is at position:"<<i<<endl;
}
}
void FindKTH()
cin>>e;
void print()
for(i=0;i<size;i++)
cout<<b[i]<<"\t";
};
int main()
arrayadt obj;
obj.create();
obj.Isfull();
obj.Isempty();
obj.Insert();
obj.Delete();
obj.Find();
obj.FindKTH();
obj.print();
return 0;
}
OUTPUT:
RESULT:
Thus the program to implement list ADT using arrays is executed and the output is verified.
EX.NO 10b) LIST ADT IMPLEMENTATION USING LINKED
LIST ADT
Aim:
To write a C++ program to implement list ADT using Linked List Implementation.
Algorithm:
Step 1:Start
Step 2: Create a class named Node and assign the data and head as NULL.
Routine : MakeEmpty
Step 1 : Create a pointer object temp for the node class.
Step 3 : Assign the data and head to NULL and get the elements as input to be inserted.
Routine : GetLength
Step 1 : Declare a variable count=0 and assign Node *temp first.
Step 2: Traverse the list until temp is not NULL and increment the count by 1 for each traversal.
Routine : GetNextItem
Step 1: Create a function GetNextItem with integer return type and takes the position as a
parameter.
Step 2 : Declare a variable count=0 and assign a pointer variable Node*current =first.
Step 3 : Run the loop until current is not NULL. If count==Y+1 , return the value at(Y+1)th position.
Routine : Insert
Step 1: Create two pointer objects for the Node class *prev and *cur.
Step 3 : Get the element and the position to be inserted as the inputs.
Step 5 : If its first position , assign temp=first and if its last position , assign temp=last.
Step 6 : If its in-between two positions , assign cur=prev and traverse over the list.
Routine : FindKTh
Step 1 : Get the index of the position of the element to be searched.
Step 2 : Declare a variable count = 0 , initiate a while loop and run the loop until current is not equal
to NULL.
Step 3: While traversing , if count == K , return the element at that index position.
Routine : Find
Step 1 : Get the data element to be inserted as the input.
Step 3 : Traverse the list using a while loop and while traversing , if current->info=info , return its
index.
Routine : Delete
Step 1 : Get the position to be deleted as input.
Step 4 : If count==pos , print the element is deleted else print not able to delete.
Routine : Print
Step 1 : Assign Node *temp =first.
Step 2 : Initiate a while loop and run the loop until temp!=NULL.
Step 3 : Traverse each node using temp=temp->next and print each element
Main Routine:
Step 1 : Create an object for the List class.
Step 2 : Get the input for choice as input and using switch case , assign numeric values to invoke
each routine.
Program:
#include<iostream>
#include<stdlib.h>
class Node
public:
int index;
int info;
Node* next;
};
Node *first,*last;
public:
List()
first=NULL;
last=NULL;
void MakeEmpty();
void Getlength();
void insert();
void delet();
void display();
};
void List::MakeEmpty()
Node *temp;
temp=new Node;
int n;
cout<<"\nEnter an Element:";
cin>>n;
temp->info=n;
temp->next=NULL;
if(first==NULL)
first=temp;
last=first;
}
else
last->next=temp;
last=temp;
void List::Getlength()
int count=0;
Node *temp=first;
while(temp!=NULL)
count++;
temp=temp->next;
int List::GetNextItem(int Y)
Node *current=first;
cin>>Y;
int count=0;
while(current!=NULL)
if(count==Y+1)
return (current->info);
count++;
current=current->next;
}
void List::insert()
Node *prev,*cur;
prev=NULL;
cur=first;
int count=1,pos,ch,n;
cout<<"\nEnter an Element:";
cin>>n;
temp->info=n;
temp->next=NULL;
cin>>ch;
switch(ch)
case 1:
temp->next=first;
first=temp;
break;
case 2:
last->next=temp;
last=temp;
break;
case 3:
cin>>pos;
while(count!=pos)
{
prev=cur;
cur=cur->next;
count++;
if(count==pos)
prev->next=temp;
temp->next=cur;
else
break;
int List::FindKTh(int K)
Node *current=first;
cin>>K;
int count=0;
while(current!=NULL)
if(count==K)
return (current->info);
count++;
current=current->next;
{
int index=0;
Node *current=first;
cin>>info;
while(current!=NULL)
if(current->info==info)
return index;
current=current->next;
index++;
void List::delet()
Node *prev=NULL,*cur=first;
int count=1,pos,ch;
cin>>ch;
switch(ch)
case 1:
if(first!=NULL)
first=first->next;
else
cout<<"\nNot Able to Delete";
break;
case 2:
while(cur!=last)
prev=cur;
cur=cur->next;
if(cur==last)
prev->next=NULL;
last=prev;
else
break;
case 3:
cin>>pos;
while(count!=pos)
prev=cur;
cur=cur->next;
count++;
if(count==pos)
prev->next=cur->next;
}
else
break;
void List::display()
Node *temp=first;
if(temp==NULL)
cout<<"\nList is Empty";
while(temp!=NULL)
cout<<temp->info;
cout<<"-->";
temp=temp->next;
cout<<"NULL";
int main()
List l;
int ch;
int K;
int info;
int Y;
while(1)
cin>>ch;
switch(ch)
case 1:
l.MakeEmpty();
break;
case 2:
l.Getlength();
break;
case 3:
cout<<l.GetNextItem(Y);
break;
case 4:
l.insert();
break;
case 5:
cout<<l.FindKTh(K);
break;
case 6:
cout<<l.Find(info);
case 7:
l.delet();
break;
case 8:
l.display();
break;
case 9:
cout<<"Exit";
return 0;
return 0;
OUTPUT: