#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<dos.h>
struct node
{
int data;
struct node *next;
};
void addatbeg();
void addatend();
void afterany();
void display();
void delatbeg();
void delatend();
void delafter();
node * search();
node *head;
void main()
{
head=NULL;
clrscr();
char ans='y';
while((ans=='y')||(ans=='Y'))
{
clrscr();
cout<<"Operations on Linked list are"<<endl;
cout<<"1. Addition at beginning"<<endl;
cout<<"2. Addition at end"<<endl;
cout<<"3. Addition after any element"<<endl;
cout<<"4. Display link list"<<endl;
cout<<"5. Deletion at beginning"<<endl;
cout<<"6. Deletion from end"<<endl;
cout<<"7. Deletion after any element"<<endl;
cout<<"8. Exit from program"<<endl;
int ch;
cin>>ch;
switch(ch)
{
case 1:
addatbeg();
break;
case 2:
addatend();
break;
case 3:
afterany();
break;
case 4:
display();
break;
case 5:
delatbeg();
break;
case 6:
delatend();
break;
case 7:
delafter();
break;
case 8:
exit(0);
}
cout<<"Do you wish to return to the main menu"<<endl;
cin>>ans;
}
getch();
}
void addatbeg()
{
node *ptr;
ptr=new node;
cout<<"Enter the element"<<endl;
cin>>ptr->data;
if(head==NULL)
{
ptr->next=NULL;
}
else
{
ptr->next=head;
}
head=ptr;
}
void display()
{
node *ptr;
ptr=head;
while(ptr!=NULL)
{
cout<<ptr->data<<"\t";
ptr=ptr->next;
}
cout<<endl;
}
void addatend()
{
node *ptr,*loc;
ptr=new node;
cout<<"Enter the element"<<endl;
cin>>ptr->data;
ptr->next=NULL;
if(head==NULL)
{
head=ptr;
}
else
{
loc=head;
while(loc->next!=NULL)
{
loc=loc->next;
}
loc->next=ptr;
}
}
node * search()
{
int num;
cout<<"Enter an element to search"<<endl;
cin>>num;
node *loc;
loc=head;
while(loc->next!=NULL)
{
if(loc->data==num)
{
return loc;
break;
}
loc=loc->next;
}
}
void afterany()
{
node *ptr,*loc;
ptr=new node;
loc=search();
cout<<"Enter the element to be inserted"<<endl;
cin>>ptr->data;
ptr->next=loc->next;
loc->next=ptr;
}
void delatbeg()
{
node *ptr;
if(head==NULL)
{
cout<<"Linklist is empty"<<endl;
textcolor(134);
cprintf("Press any key to exit...............");
getch();
exit(0);
}
else
{
ptr=head;
head=head->next;
delete(ptr);
}
}
void delatend()
{
node *ptr,*loc;
ptr=head;
while(ptr->next!=NULL)
{
loc=ptr;
ptr=ptr->next;
}
loc->next=NULL;
delete(ptr);
}
void delafter()
{
node *loc,*ptr;
loc=search();
if(loc->next==NULL)
{
cout<<"It is the last element so can't perform operation"<<endl;
}
else
{
ptr=loc->next;
loc->next=ptr->next;
delete(ptr);
}
}
This comment has been removed by the author.
ReplyDeleteSir we miss u alot but please be with us always...!!!!!
ReplyDeletesir plse give me an information on upper limit and lower limit used for writing alphabet in both capital as well as small letter
ReplyDeletegaurav if u r asking abt ascii values They are
ReplyDeleteA-Z = 65-90
a-z = 97-122
0-9 = 48-57
enter key= 13
space bar = 32
Back space = 8
Sir...
ReplyDeletecould u please help us with the program which tells us whether a number is prime or not...
without using exit(0)???
thank u sir.........
ReplyDelete