==============================
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct nodetype
{ char info;
struct nodetype *next;
};
nodetype *getnode()
{ nodetype *p;
p=new nodetype;
return p;
}
typedef struct nodetype *pointertype;
void initialize(pointertype &plist)
{ plist=getnode();
plist->next=getnode();
plist->next=plist;
plist=NULL;
}
void freenode(pointertype &p)
{ free(p);
}
void clearlist(pointertype *plist)
{ *plist=NULL;
}
void Insertfirst(pointertype &plist,char item)
{ pointertype ptr;
ptr=getnode();
ptr->info=item;
ptr->next=plist->next;
plist->next=ptr;
}
void Insertafter(pointertype &plist,char data,char item)
{ pointertype ptr,p;
p=plist->next;
do
{ p=p->next;
if(p->next==plist->next)
break;
if(p->info==data)
break;
}while(p->info!=data);
if(p->info==data)
{ ptr=getnode();
ptr->info=item;
ptr->next=p->next;
p->next=ptr;
}
}
void Insertend(pointertype &plist,char item)
{ pointertype ptr;
ptr=getnode();
ptr->info=item;
ptr->next=plist->next;
plist->next=ptr;
plist=ptr;
}
void SortDec(pointertype &plist)
{ pointertype p,q;
char temp;
for(p=plist;p->next!=NULL;p=p->next)
for(q=p->next; q!=NULL; q=q->next)
if(p->info<q->info)
{ temp=p->info;
p->info=q->info;
q->info=temp;
}
}
void SortInc(pointertype &plist)
{ pointertype p,q;
char temp;
for(p=plist;p->next!=NULL;p=p->next)
for(q=p->next; q!=NULL; q=q->next)
if(p->info>q->info)
{ temp=p->info;
p->info=q->info;
q->info=temp;
}
}
void search(pointertype plist,char item)
{ int f=0;
pointertype p=plist->next;
do
{ if(p->info==item)
{ cout<<&p->next<<'\t'<<p->info<<endl;
f=1;
}
p=p->next;
}while(p!=plist->next);
if(f==0)
cout<<"Not found"<<endl;
}
void deletefirst(pointertype &plist)
{ pointertype ptr=plist->next;
plist->next=ptr->next;
}
void deleteafter(pointertype &plist,char data)
{ pointertype p=plist->next,ptr;
do
{ if(p->info==data)
{ ptr=p->next;
p->next=ptr->next;
freenode(ptr);
break;
}
p=p->next;
}while(p!=plist->next);
}
void deleteend(pointertype &plist)
{ pointertype p,p1;
p=plist->next;
p1=plist;
while(p->next!=plist)
p=p->next;
p->next=plist->next;
freenode(p1);
}
void Traverse(pointertype plist)
{ pointertype p=plist->next;
do
{ cout<<p->info<<'\t';
p=p->next;
}while(p!=plist->next);
}
void update(pointertype &plist,char data, char item)
{ pointertype p;
p=plist;
while(p->info!=item)
p=p->next;
p->info=data;
}
void main()
{ char menu[8][10]={"Input", "Show", "Search", "Insert", "Delete", "Update","Sort", "Exit"};
pointertype plist;
char ch;
do
{
for(int i=0;i<8;i++)
cout<<endl<<"["<<(i+1)<<"]."<<menu[i];
ch=getch();
clrscr();
if(ch=='1')
{ initialize(plist);
int n;
cout<<"Enter n:";
cin>>n;
char item;
for(int i=0;i<n;i++)
{ cout<<"Enter item:";
cin>>item;
if(plist==NULL)
{ plist=getnode();
plist->info=item;
plist->next=plist;
}
else
Insertfirst(plist,item);
}
}
else if (ch=='2')
{ Traverse(plist);
getch();
}
else if(ch=='3')
{ char data;
cout<<"Enter data u want search:";
cin>>data;
search(plist,data);
getch();
}
else if(ch=='4')
{ cout<<endl;
Traverse(plist);
cout<<endl;
char item;
cout<<"Enter Data u want to insert:";
cin>>item;
cout<<"[1].Insert First\n[2].Insert After\n[3].Insert End";
char ch;
ch=getch();
if(plist==NULL)
{ plist=getnode();
plist->info=item;
plist->next=plist;
}
else
switch(ch)
{ case '1': Insertfirst(plist,item);
break;
case '2': cout<<"\nEnter item which data after it insert:";
char data;
cin>>data;
Insertafter(plist,data,item);
break;
case '3': Insertend(plist,item);
break;
}
}
else if(ch=='5')
{ cout<<endl;
Traverse(plist);
cout<<endl;
cout<<"[1].Delete First\n[2].Delete After\n[3].Delete End";
char ch;
ch=getch();
if(plist==NULL)
cout<<"Nothing to delete!";
else
switch(ch)
{ case '1': deletefirst(plist);
break;
case '2': cout<<"\nEnter item which data after it will delete:";
char data;
cin>>data;
deleteafter(plist,data);
break;
case '3': deleteend(plist);
break;
}
}/*
else if(ch=='6')
{ cout<<endl;
Traverse(plist);
cout<<endl;
cout<<"Enter item u want to update:";
char item;
cin>>item;
cout<<"Enter new data:";
char data;
cin>>data;
update(plist,data,item);
}
else if(ch=='7')
{ cout<<endl;
Traverse(plist);
cout<<endl;
cout<<"[1].Decrease Sort\n[2].Increase Sort";
char ch;
ch=getch();
switch(ch)
{ case '1': SortDec(plist);
break;
case '2': SortInc(plist);
break;
}
}
*/
else if(ch=='8')
exit(0);
clrscr();
}while(ch!=27);
}
//Leave us a command for it ..!
0 comments:
Post a Comment