0% found this document useful (0 votes)
826 views7 pages

Implement Circular Link List Using Templates. Include Function For Insertion, Deletion, Search of A No., Reverse The List.

The document describes an implementation of a circular linked list template class in C++. The class includes functions for insertion, deletion, searching for a number, and reversing the list. It allows the user to create a circular linked list to store and manipulate integer, float, or character data. The user can insert nodes, display the list, delete nodes, search for a value, and reverse the order of the list.

Uploaded by

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

Implement Circular Link List Using Templates. Include Function For Insertion, Deletion, Search of A No., Reverse The List.

The document describes an implementation of a circular linked list template class in C++. The class includes functions for insertion, deletion, searching for a number, and reversing the list. It allows the user to create a circular linked list to store and manipulate integer, float, or character data. The user can insert nodes, display the list, delete nodes, search for a value, and reverse the order of the list.

Uploaded by

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

//18. // IMPLEMENT CIRCULAR LINK LIST USING TEMPLATES.

INCLUDE FUNCTION FOR INSE


RTION, DELETION, SEARCH OF A NO., REVERSE THE LIST.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
template <class X>
class element
{
public:
X data;
element *nxt;
};
template <class X>
class LL
{
element<X> *start,*temp;
public:
LL()
{
start=temp=NULL;
}
inline void insert()
{
element<X> *p;
cout<<"\nEnter the data...";
temp=new element<X>;
cin>>temp->data;
if(start==NULL)
{
start=temp;
start->nxt=start;
}
else
{
for(p=start;(p->nxt)!=start;p=p->nxt);
p->nxt=temp;
((p->nxt)->nxt)=start;
}
temp=NULL;
cout<<"\nDone...";
}
inline void display()
{
temp=start;
do
{
if(start==NULL)
{
break;
}
cout<<endl<<temp->data;
temp=temp->nxt;
}while(temp!=start);

}
void del()
{
display();
cout<<"\nEnter the term u want to delete... ";
X info;
cin>>info;
int flag=0;
element<X> *p;
temp=start;
do
{
if(temp->data==info)
{
if(temp==start)
{
element<X> *q;
for(q=start;q->nxt!=start;q=q->nxt);
start=start->nxt;
q->nxt=start;
if(q==q->nxt)
{
delete start;
start=NULL;
}
}
else
{
p->nxt=temp->nxt;
}
delete temp;
flag=1;
break;
}
p=temp;
temp=temp->nxt;
}while(temp!=start);
if(flag)
{
cout<<"\nDeleted...";
}
else
{
cout<<"\nNot Found...";
}
display();
}
void search()
{
display();
cout<<"\nEnter the term u want to search... ";
X info;
cin>>info;
int flag=0;
temp=start;
do
{
if(temp->data==info)
{

flag=1;
break;
}
temp=temp->nxt;
}while(temp!=start);
if(flag)
{
cout<<"\nFound... "<<info;
}
else
{
cout<<"\nNot Found...";
}
display();
}
inline void rev()
{
int count=1;
for(temp=start;temp->nxt!=start;count++,temp=temp->nxt);
if(start==NULL)
{
count=0;
}
X *p;
element<X> *q;
p=new X[count];
int i;
for(temp=start,i=0;i<count;i++,temp=temp->nxt)
{
p[i]=temp->data;
}
i--;
for(temp=NULL;i>=0;i--)
{
if(i==(count-1))
{
temp=new element<X>;
temp->data=p[i];
temp->nxt=temp;
start=temp;
}
else
{
for(;temp->nxt!=start;temp=temp->nxt);
q=new element<X>;
q->data=p[i];
q->nxt=start;
temp->nxt=q;
}
}
cout<<"\nReversed...";
display();
}
};
template <class X>
void menu(X x)
{
LL<X> A;

char ch;
do
{
clrscr();
cout<<"\n1)Insert\n";
cout<<"2)Display\n";
cout<<"3)Delete\n";
cout<<"4)Search\n";
cout<<"5)Reverse\n";
cout<<"Enter choice or 0 to exit...\n";
ch=getch();
switch(ch)
{
case '1':
{
do
{
A.insert();
cout<<"\nEnter another... (Y / N) ";
ch=getch();
}while(ch=='y'||ch=='Y');
break;
}
case '2':
{
A.display();
break;
}
case '3':
{
do
{
A.del();
cout<<"\nDelete another... (Y / N) ";
ch=getch();
}while(ch=='y'||ch=='Y');
break;
}
case '4':
{
do
{
A.search();
cout<<"\nSearch another... (Y / N) ";
ch=getch();
}while(ch=='y'||ch=='Y');
break;
}
case '5':
{
A.rev();
break;
}
default:
{
cout<<" \n Enter
the correct choice";
}

}
cout<<"\nPress any key... ";
getch();
}while(ch!='0');
}
void main()
{
clrscr();
cout<<"\nWhich data do you want to work with??\n";
cout<<"1) Integer\n";
cout<<"2) Float\n";
cout<<"3)Character\n ";
char ch;
ch=getch();
switch(ch)
{
case '1':
{
menu(0);
break;
}
case '2':
{
menu(1.0);
break;
}
case '3':
{
menu('a');
break;
}
default:
{
cout<<"\nInteger assumed... Press any key";
getch();
menu(0);
}
}
cout<<"\nProgram Terminated...";
}
/*OUTPUT :-Which data do you want to work with??
1) Integer
2) Float
3)Character
3
1)Insert
2)Display
3)Delete
4)Search
5)Reverse
Enter choice or 0 to exit...
Enter the data...H
Done...

Enter another... (Y / N)
Enter the data...A
Done...
Enter another... (Y / N)
Enter the data...R
Done...
Enter another... (Y / N)
Enter the data...M
Done...
Enter another... (Y / N)
Enter the data...E
Done...
Enter another... (Y / N)
Enter the data...E
Done...
Enter another... (Y / N)
Enter the data...T
Done...
Enter another... (Y / N)
1)Insert
2)Display
3)Delete
4)Search
5)Reverse
Enter choice or 0 to exit...
H
A
R
M
E
E
T
Enter the term u want to search...R
Found... R
Search another... (Y / N)
Press any key
1)Insert
2)Display
3)Delete
4)Search
5)Reverse
Enter choice or 0 to exit...
Press any key
Program Terminated...*/

You might also like