CPP Program for Cursor Implementation of List ADT

Aim:

To write a C++ program for cursor implementation of list ADT.

Description:

  • A linked list is a sequence of data structures, which are connected together via links.
  • If linked lists are required and pointers are not available, then an alternate implementation must be used.
  • The alternate method we will describe is known as a cursor implementation. The two important items present in a pointer implementation of linked lists are
    1. The data is stored in a collection of structures. Each structure contains the data and
      a pointer to the next structure.
    2. A new structure can be obtained from the system’s global memory by a call to
      malloc and released by a call to free.

Algorithm:

  1. Start the program.
  2. Create a node with two fields’ data and link field. O Allocate space for the node dynamically.
  3. Create link between the created nodes and let the last node be with NULL Link
  4. Insert the input data in the data field and press –1 to stop the same.
  5. Get the choice of operations either insertion or deletion.
  6. For insertion get the position in which insertion is to be done and the element to be inserted. Check for the start, middle or end position of insertion. Insert the node and change its link accordingly.
  7. For deletion get the position in which deletion is to be done. Delete the node and then link it to the next node. Before deletion check whether there is data in the list to be deleted.
  8. Using display option lists the elements of the list.
  9. Stop the program

Program:

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 20
class LIST
{ private:
int List[MAX];
public:
int create();
void display(int);
void reverse(int);
int search(int);
void delet(int);
};
int LIST::create()
{
int n,i;
cout<<"\n how many elements you want in the list:";
cin>>n;
if(n>MAX)
cout<<"\n Error:Number of elements exceeds the limit";
for(i=0;i<n;i++)
 {
 cout<<"\nEnter the element number"<<i+1<<":";
 cin>>List[i];
}
 cout<<"\n The List is successfully created\n";
 return(n);
}
void LIST::display(int n)
{
int i;
cout<<"\n the list is:\n";
for(i=0;i<n;i++)
cout<<"\n"<<List[i];
}
void LIST::reverse(int n)
{
int i;
cout<<"\n the reversed list is:.\n";
for(i=n-1;i>=0;i--)
cout<<"\n"<<List[i];
}
int LIST::search(int n)
{
int i,key;
cout<<"\n enter the number you want to search?";
cin>>key;
for (i=0;i<n;i++)
{
if(List[i]==key)
{
cout<<"\n the given number is at
position:"<<i<<"\n";
return i;
}
}
cout<<"\n the given number is not in the list\n";
return -1;
} 
void LIST::delet(int n)
{
int i;
i=search(n);
if(i!=NULL)
{
List[i]=-1;
cout<<"Element deleted";
}
}
void main()
{
LIST obj;
int choice,len,position;
char ans;
clrscr();
cout<<"\n\t program to perform operations on ordered
list"; cout<<"\n 1.create";
cout<<"\n 2.display";
cout<<"\n 3.search for a number";
cout<<"\n 4.reverse";
cout<<"\n 5.delete";
cout<<"\n 6.Quit";
do
{
cout<<"\n enter your choice(1-6)";
cin>>choice;
switch(choice)
{
case 1:
len=obj.create();
break;
case 2:
obj.display(len);
break;
case 3:
position=obj.search(len);
break;
case 4:
obj.reverse(len);
break;
case 5:
obj.delet(len);
break;
case 6:
exit(0);
break;
default:
cout<<"\n invalid choice, try again";
break;
}
getch();
}while(choice!=6);
} 

Output:

Result:

Thus the C++ program for cursor implementation of list ADT was created, executed and output was verified successfully.

Leave a Comment