[go: up one dir, main page]

0% found this document useful (0 votes)
14 views17 pages

DS Lab Ex1

The document outlines two C++ programs for implementing the List Abstract Data Type (ADT) using arrays and linked lists. The first program uses an array to create, insert, delete, and search elements, while the second program employs a linked list structure for similar operations. Both implementations include detailed algorithms and code snippets for each functionality.

Uploaded by

Muthu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views17 pages

DS Lab Ex1

The document outlines two C++ programs for implementing the List Abstract Data Type (ADT) using arrays and linked lists. The first program uses an array to create, insert, delete, and search elements, while the second program employs a linked list structure for similar operations. Both implementations include detailed algorithms and code snippets for each functionality.

Uploaded by

Muthu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

EX.NO 1: (a) IMPLEMENT THE LIST ADT USING ARRAYS .

DATE:
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.
S 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.
Step 11: Stop
PROGRAM:
#include<iostream>
#include<array>
#define MAX 10
using namespace std;
class arrayadt
{
public:
int a,i,size,n,p,e,f,pos,b[20],x;
void create()
{
cout<<"Creating a list"<<endl;
cout<<"Enter the size of the array:";
cin>>size;
for (i=0;i<size;i++)
{
cin>>b[i];
}
}
//cout<<endl;
void Isfull()
{
if(size==0)
{
cout<<"The array is empty:"<<endl;
}
else
{
cout<<"The array is full:"<<endl;
}
}
//cout<<endl;
void Isempty()
{
if(size==0)
{
cout<<"The array is empty"<<endl;
}
else
{
cout<<"The array is full"<<endl;
}
}
void Insert()
{
cout<<"Enter the position of the array for the element to be inserted:";
cin>>pos;
if(pos>=size)
{
cout<<"Invalid position"<<endl;
}
else
{
for(i=MAX;i>=pos;i--)
{
b[i]=b[i-1];
}
cout<<"Enter the element to be inserted:";
cin>>p;
b[pos]=p;
size++;
}
cout<<"List after insertion"<<endl;
for(i=0;i<size;i++)
{
cout<<b[i]<<"\t";
}
}
void Delete()
{
cout<<"\nEnter the position of the array for the element to be deleted:";
cin>>pos;
if(pos>=size)
{
cout<<"Invalid position"<<endl;
}
else
{
for(i=pos+1;i<size;i++)
{
b[i-1]=b[i];
}
cin>>pos;
if(pos>=size)
{
cout<<"Invalid position"<<endl;
}
else
{
for(i=pos+1;i<size;i++)
{
b[i-1]=b[i];
}
size--;
}
cout<<"The list after deletion"<<endl;
for(i=0;i<size;i++)
{
cout<<b[i]<<"\t";
}
}
void Find()
{
cout<<"\nEnter the element to be searched:";
cin>>x;
for(i=0;i<size;i++)
{
if(b[i]==x)
{
cout<<"Element is at position:"<<i<<endl;
}
}
}
void FindKTH()
{
cout<<"Enter the position of the element to be searched:";
cin>>e;
cout<<"The element at the given position is:"<<b[e]<<endl;
}
void print()
{
cout<<"The final array"<<endl;
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 1: b) LIST ADT IMPLEMENTATION USING LINKEDLIST ADT
DATE:
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 2: Create an object Node with the new keyword.
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.
Step 3 : Print the value of count finally.
Routine : GetNextItem
Step 1: Create a function GetNextItem with integer return type and takes the position as aparameter.
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.
Step 4 : Traverse the list until current is not NULL.
Routine : Insert
Step 1: Create two pointer objects for the Node class *prev and *cur.
Step 2 : Create an object Node *temp = new Node.
Step 3 : Get the element and the position to be inserted as the inputs.
Step 4 : Assign temp->info = n and temp->next = NULL.
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
equalto 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 2 : Declare a variable index = 0
Step 3 : Traverse the list using a while loop and while traversing , if current->info=info , return
itsindex.
Routine : Delete
Step 1 : Get the position to be deleted as input.
Step 2 : Assign Node *prev=NULL and Node *cur =first.
Step 3 : Assign prev=cur , cur=cur->next and increment the value of count.
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>
using namespace std;
class Node
{
public:
int index;
int info;
Node* next;
};
class List:public Node
{
Node *first,*last;
public:
List()
{
first=NULL;
last=NULL;
}
void MakeEmpty();
void Getlength();
int GetNextItem(int Y);
void insert();
int FindKTh(int K);
int Find(int x);
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;
}
cout<<"The number of nodes is:"<<count;
}
int List::GetNextItem(int Y)
{
Node *current=first;
cout<<"Enter the index position:";
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;
Node *temp=new Node;
cout<<"\nEnter an Element:";
cin>>n;
temp->info=n;
temp->next=NULL;
cout<<"\nINSERT AS\n1:FIRSTNODE\n2:LASTNODE\n3:IN BETWEEN FIRST&LAST
NODES";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
temp->next=first;
first=temp;
break;
case 2:
last->next=temp;
last=temp;
break;
case 3:
cout<<"\nEnter the Position to Insert:";
cin>>pos;
while(count!=pos)
{
prev=cur;
cur=cur->next;
count++;
}
if(count==pos)
{
prev->next=temp;
temp->next=cur;
}
else
cout<<"\nNot Able to Insert";
break;
}
}
int List::FindKTh(int K)
{
Node *current=first;
cout<<"Enter the index position:";
cin>>K;
int count=0;
while(current!=NULL)
{
if(count==K)
return (current->info);
count++;
current=current->next;
}
}
int List::Find(int info)
{

int index=0;
Node *current=first;
cout<<"Enter the data value:";
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;
cout<<"\nDELETE\n1:FIRSTNODE\n2:LASTNODE\n3:IN BETWEEN FIRST&LAST
NODES";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
if(first!=NULL)
{
cout<<"\nDeleted Element is "<<first->info;
first=first->next;
}
else
cout<<"\nNot Able to Delete";
break;
case 2:
while(cur!=last)
{
prev=cur;
cur=cur->next;
}
if(cur==last)
{
cout<<"\nDeleted Element is: "<<cur->info;
prev->next=NULL;last=prev;
}
else
cout<<"\nNot Able to Delete";
break;
case 3:
cout<<"\nEnter the Position of Deletion:";
cin>>pos;
while(count!=pos)
{
prev=cur;
cur=cur->next;
count++;
}
if(count==pos)
{
cout<<"\nDeleted Element is: "<<cur->info;
prev->next=cur->next;
}
else
cout<<"\nNot Able to Delete";
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)
{
cout<<"\n**** MENU ****";
cout<<"\n1:MakeEmpty\n2:Getlength\n3:GetNextItem\n4:Insert\n5:FindKTh\n6:Find\
n7.Delete\n8.Display";
cout<<"\nEnter Your Choice:";
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:

RESULT:
Thus the program to implement list ADT using LINKED LIST ADT is executed and the output is
verified.

You might also like