link list operation

/* link list operation like append,delete,insert,find,display*/

#include
#include
#include
#include
#include
int ex=0;
struct linklist
{
int no;
char name[50];
struct linklist *link;
};
class linkl
{
linklist *list,*temp;
public:
linklist *first;
void getdata();
void display(linklist *first);
int choice(linklist *first);
};
typedef class linklist stu;
void linkl :: getdata()
{
clrscr();
char x;
list=new stu;
first=list;
while(1)
{
cout << "\nEnter the roll no :- ";
cin >> list->no;
cout << "\nEnter the name :- ";
cin >> list->name;
cout << "\nDo you want to continue [y/n] :- ";
cin >> x;
if(x=='n' || x=='N')
break;
list->link=new stu;
list=list->link;
}
list->link=NULL;
display(first);
}
void linkl :: display(linklist *first)
{
clrscr();
list=first;
while(1)
{
cout << "\nROLL NO :-"<< list->no;
cout << "\nNAME :- " << list->name;
if(list->link==NULL)
break;
list=list->link;
}
choice(first);

}
int linkl :: choice(linklist *first)
{
int ch,x;
ex=0;
cout << "\n\t\t\t 1. APPEND ";
cout << "\n\t\t\t 2. DELETE ";
cout << "\n\t\t\t 3. INSERT ";
cout << "\n\t\t\t 4. FIND ";
cout << "\n\t\t\t 5. DISPLAY ";
cout << "\n\t\t\t 6. EXIT ";
cout << "\nEnter your choice :- ";
cin >> ch;
switch(ch)
{

case 1: list=first;
while(1)
{
if(list->link==NULL)
{
list->link=new stu;
list=list->link;
cout << "\nEnter the roll no :- ";
cin >> list->no;
cout << "\nEnter the name :- ";
cin >> list->name;
list->link=NULL;
display(first);
if(ex)
return 1;
}
else
list=list->link;
}
display(first);
case 2: del :
clrscr();
list=first;
while(1)
{
cout << "\nROLL NO :- " << list->no;
cout << "\nNAME :- " << list->name;
if(list->link==NULL)
break;
list=list->link;
}
list=first;
cout << "\n\t\t\t 1. DELETING AT FIRST ";
cout << "\n\t\t\t 2. DELETING AT LAST ";
cout << "\n\t\t\t 3. DELETE IN BETWEEN ";
cout << "\n\t\t\t 4. GO BACK ";
cout << "\nEnter your choice :- ";
cin >> ch;
switch(ch)
{
case 1: first=first->link;
goto del;
case 2: while(list->link->link!=NULL)
{
list=list->link;
}
list->link=NULL;
goto del;
case 3:cout << "\nEnter the roll no to be deleted :- ";
cin >> x;
while(list->link->no!=x)
{
list=list->link;
}
list->link=list->link->link;
goto del;
case 4: display(first);
default : break;
}
case 3: del1:
clrscr();
list=first;
while(1)
{
cout << "\nROLL NO :- " << list->no;
cout << "\nNAME :- " << list->name;
if(list->link==NULL)
break;
list=list->link;
}
list=first;
cout << "\n\t\t\t 1. INSERT AT FIRST ";
cout << "\n\t\t\t 2. INSERT AT LAST ";
cout << "\n\t\t\t 3. INSERT IN BETWEEN ";
cout << "\n\t\t\t 4. GO BACK ";
cout << "\nEnter your choice :- ";
cin >> ch;
switch(ch)
{
case 1:list=new stu;
cout << "\nEnter the roll no :- ";
cin >> list->no;
cout << "\nEnter the name :- ";
cin >> list->name;
list->link=first;
first=list;
goto del1;
case 2:list=first;
while(1)
{
if(list->link==NULL)
{
list->link=new stu;
list=list->link;
cout << "\nEnter the roll no :- ";
cin >> list->no;
cout << "\nEnter the name :- ";
cin >> list->name;
list->link=NULL;
goto del1;
}
else
list=list->link;
}
break;
case 3: list=first;
cout << "\nEnter the roll no before you want to insert :- ";
cin >> x;
while(list->link->no!=x)
{
list=list->link;
}
temp=new stu;
temp->link=list->link;
list->link=temp;
list=list->link;
cout << "\nEnter the roll no :- ";
cin >> list->no;
cout << "\nEnter the name :- ";
cin >> list->name;
goto del1;
case 4:display(first);
break;
default :break;
}
case 4: list=first;
cout << "\nEnter the roll no to be found :- ";
cin >> x;
int flag=0;
int i=1;
while(1)
{
if(list->no==x)
{
flag=1;
goto b;
}
else
{
if(list->link==NULL)
{
flag=0;
goto b;
}
}
list=list->link;
i=i+1;
}
b:
if(flag==1)
{
cout << "\nThe key is found in position "<< i;
sleep(1);
display(first);
}
else
{
cout << "\nThe key is invalid !!! ";
sleep(1);
display(first);
}
break;
case 5: clrscr();
list=first;
while(1)
{
cout << "\nROLL NO :-"<< list->no;
cout << "\nNAME :- " << list->name;
if(list->link==NULL)
break;
list=list->link;
}
cout << "\n\n\t\t\t PLEASE WAIT ... ";
sleep(3);
display(first);
case 6: ex=1;return 0;
default : break;
}
}
main()
{
clrscr();
char ch;
linkl a,b;
a.getdata();
cout << "\nDo you want to create another linklist :- ";
cin >> ch;
if(ch=='n'||ch=='N')
return 0;
b.getdata();
cout << "\nDo you want to wrok with the previoue one :- ";
cin >> ch;
if(ch=='n'||ch=='N')
return 0;
a.display(a.first);
getch();
return 0;
}

0 comments:

Post a Comment