I am having some issues with making my circular singly linked list class working
ID: 669612 • Letter: I
Question
I am having some issues with making my circular singly linked list class working. Having issues completing steps 4-6. Anyone willing to help me out. The language is C++. Thanks!
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Implement a Templated Circular Singly Linked List Class
The class must implement the following operations:
1. Constructor for creating the List
2. Destructor for safely deleting the List
3. Inserting an element at index x, where x is read from stdin
4. Deleting an element at index x, where x is read from stdin
5. Printing out the list in sequential order(from the head to the item before the head).
6. Printing the element at index x.
Explanation / Answer
Answer:
Note:Here your modified code:
# include <iostream.h>
# include <process.h>
# include <conio.h>
# include <malloc.h>
int cur_li_list1=1;
int view_menu();
// struct
{
int no;
struct li_LIST1 *after;
};
class link
{
li_LIST1 *LIST;
public:
li_LIST1 *top;
link()
{
LIST=NULL;
top=NULL;
}
void fetch_LIST();
void view_LIST();
void insert();
void delete_LIST();
void sort();
void merge(li_LIST1 *,li_LIST1*);
friend void union_LIST(li_LIST1 *,li_LIST1 *);
friend void intersact(li_LIST1 *,li_LIST1 *);
void re();
};
void link :: fetch_LIST()
{
int no;
LIST=top;
while(LIST->after!=NULL)
{
LIST=LIST->after;
}
while(1)
{
cout<<"ENTER NUMBER :";
cin>>no;
if(no!=0)
{
if(LIST==NULL)
{
LIST=new li_LIST1;
top=LIST;
}
LIST->no=no;
LIST->after = new li_LIST1;
LIST=LIST->after;
}
else
{
LIST->after=NULL;
break;
}
}
}
void link :: view_LIST()
{
LIST=top;
cout<<endl;
if (LIST==NULL)
{
cout<<"LINK LIST IS EMPTY !!!";
return;
}
while(LIST->after!=NULL)
{
cout<<LIST->no<<" ";
LIST=LIST->after;
}
}
void link :: insert()
{
int ch;
LIST=top;
cout<<endl;
cout<<"[ 1 ] : INSERT AT FIRST"<<endl;
cout<<"[ 2 ] : INSERT IN MIDDLE"<<endl;
cout<<"[ 3 ] : INSERT AT LAST"<<endl;
cout<<"[ 4 ] : BACK TO MAIN MENU"<<endl;
cout<<"ENTER YOUR CHOICE :";
cin>>ch;
li_LIST1 *newnode;
newnode=new li_LIST1;
switch(ch)
{
case 1:
cout<<"ENTER NUMBER :";
cin>>newnode->no;
LIST=top;
if(LIST==NULL)
{
LIST=newnode;
newnode->after=NULL;
top=LIST;
}
else
{
newnode->after=LIST;
top=newnode;
}
break;
case 2: int no;
cout<<endl;
cout<<"ENTER NUMBER AFTER WHICH YOU WANT TO INSERT :";
cin>>no;
LIST=top;
while(LIST->after !=NULL)
{
if(LIST->no==no)
{
cout<<"ENTER NUMBER TO INSERT :";
cin>>newnode->no;
newnode->after=LIST->after;
LIST->after=newnode;
if(LIST==top)
{
top=newnode;
}
return;
}
LIST=LIST->after;
}
cout<<"Key not found ..."<<endl;
break;
case 3 : list=top;
while(list->after!=NULL)
{
list=list->after;
}
cout<<"ENTER NUMBER :";
cin>>newnode->no;
if(top==NULL)
{
list=newnode;
top=list;
}
else
{
list->after=newnode;
newnode->after=NULL;
}
break;
}
}
void link :: delete_list()
{
cout<<endl;
list=top;
int no;
cout<<"ENTER THE NUMBER TO DELETED :";
cin>>no;
if(top->no==no)
{
top=top->after;
return;
}
while(list->after!=NULL)
{
if(list->after->no==no)
{
list->after=list->after->after;
return;
}
list=list->after;
}
cout<<"Number not not found !!!";
}
void link :: sort()
{
li_list1 *i,*j,*t;
for(i=top;i->after!=NULL;i=i->after)
{
for(j=top;j->after!=NULL;j=j->after)
{
if(i->no < j->no)
{
t->no=i->no;
i->no=j->no;
j->no=t->no;
}
}
}
}
void union_list(li_list1 *l1,li_list1 *l2)
{
cout<<endl;
li_list1 *h;
h=l1;
while(l1->after!=NULL)
{
cout<<l1->no<<" ";
l1=l1->after;
}
int flag=0;
while(l2->after!=NULL)
{
l1=h;
flag=0;
while(l1->after!=NULL)
{
if(l1->no==l2->no)
{
flag=1;
break;
}
l1=l1->after;
}
if(flag==0)
{
cout<<l2->no<<" ";
}
l2=l2->after;
}
}
void intersact (li_list1 *l1,li_list1 *l2)
{
li_list1 *h;
h=l2;
while(l1->after!=NULL)
{
l2=h;
while(l2->after!=NULL)
{
if(l1->no==l2->no)
{
cout<<l1->no<<" ";
break;
}
l2=l2->after;
}
l1=l1->after;
}
}
void link :: re()
{
int a[50];
list=top;
int i=0;
while(list->after!=NULL)
{
a[i]=list->no;
list=list->after;
i=i+1;
}
int n=i-1;
i=n;
list=top;
while(list->after!=NULL)
{
list->no=a[i];
list=list->after;
i=i-1;
}
}
void link :: merge(li_list1 *l1,li_list1 *l2)
{
top=NULL;
list=new li_list1;
while(l1->after !=NULL)
{
if(top==NULL)
{
top=list;
}
list->no=l1->no;
list->after=new li_list1;
list=list->after;
l1=l1->after;
}
while(l2->after !=NULL)
{
list->no=l2->no;
list->after=new li_list1;
list=list->after;
list->after=NULL;
l2=l2->after;
}
list->after=NULL;
}
void main()
{
clrscr();
link l1,l2,l3;
while(1)
{
switch(view_menu())
{
case 1: cout<<"Enter LinkList Number [ 1 , 2 , 3 ]:";
int n;
cin>>n;
if(n>=1 && n<=3)
{
cur_li_list1=n;
}
break;
case 2: switch(cur_li_list1)
{
case 1: l1.fetch_list();
break;
case 2: l2.fetch_list();
break;
case 3: l3.fetch_list();
break;
}
getch();
break;
case 3: switch(cur_li_list1)
{
case 1 : l1.insert();
break;
case 2 : l2.insert();
break;
case 3 : l3.insert();
break;
}
getch();
break;
case 4: switch(cur_li_list1)
{
case 1: l1.view_list();
break;
case 2: l2.view_list();
break;
case 3: l3.view_list();
break;
}
getch();
break;
case 5:
switch(cur_li_list1)
{
case 1: l1.view_list();
l1.delete_list();
break;
case 2: l2.view_list();
l2.delete_list();
break;
case 3: l3.view_list();
l3.delete_list();
break;
}
getch();
break;
case 6: cout<<endl;
switch(cur_li_list1)
{
case 1: l1.sort();
l1.view_list();
break;
case 2: l2.sort();
l2.view_list();
break;
case 3: l3.sort();
l3.view_list();
break;
}
cout<<endl<<endl<<"Linklist sorted !!!";
getch();
break;
case 7: cout<<endl<<endl<<"Union of First two List..."<<endl;
union_list(l1.top,l2.top);
getch();
break;
case 8:cout<<endl<<endl<<"Intersaction of First two list..."<<endl;
intersact(l1.top,l2.top);
getch();
break;
case 9: switch(cur_li_list1)
{
case 1: l1.re();
break;
case 2: l2.re();
break;
case 3: l3.re();
break;
}
getch();
break;
case 10 : l3.merge(l1.top,l2.top);
cout<<endl;
cout<<"First two linklist merged in third link list !!!";
l3.view_list();
getch();
break;
case 11 : exit(1);
}
}
}
int view_menu()
{
clrscr();
cout<<endl;
cout<<" [ 01 ] Select Linklist (Selected List is:"<<cur_li_list1<<")"<<endl;
cout<<" [ 02 ] Get Elements"<<endl;
cout<<" [ 03 ] Insert"<<endl;
cout<<" [ 04 ] Display"<<endl;
cout<<" [ 05 ] Delete"<<endl;
cout<<" [ 06 ] Sort"<<endl;
cout<<" [ 07 ] Union"<<endl;
cout<<" [ 08 ] Intersection"<<endl;
cout<<" [ 09 ] Re"<<endl;
cout<<" [ 10 ] Merge Linklist"<<endl;
cout<<" [ 11 ] Exit"<<endl;
cout<<" Enter your choice :";
int ch;
cin>>ch;
return ch;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.