The user is provided with the following options..
To insert an item at begining
To insert an item at end
To insert an item at position
To traverse the list
To delete first item
To delete last item
To delete item from position
To count no. of items in list
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
- #include<conio.h>
- #include<stdio.h>
- #include<alloc.h>
- #include<process.h>
- struct node
- { int info;
- struct node *next;
- };
- typedef struct node sn;
- void insertatbeg(sn **,int);
- void insertatend(sn **,int);
- void insertatpos(sn **,int,int);
- void traverse(sn *);
- int delfirst(sn *);
- int dellast(sn *);
- int delpos(sn *,int);
- int count(sn *);
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- void main()
- { sn *p;
- int n,i,l;
- char ch;
- clrscr();
- p=NULL;
- do
- { printf("\n-----select an operation-----");
- printf("\n1->To insert an item at begining<-");
- printf("\n2->To insert an item at end<-");
- printf("\n3->To insert an item at position<-");
- printf("\n4->To traverse the list<-");
- printf("\n5->To delete first item<-");
- printf("\n6->To delete last item<-");
- printf("\n7->To delete item from position<-");
- printf("\n8->To count no. of items in list<-");
- printf("\n9->Exit<-");
- printf("\n->Enter your choice:");
- scanf("%d",&n);
- switch(n)
- { case 1:printf("\n->Enter the item to insert");
- scanf("%d",&i);
- insertatbeg(&p,i);
- break;
- case 2:printf("\n->Enter the item to insert");
- scanf("%d",&i);
- insertatend(&p,i);
- break;
- case 3:printf("\n->Enter the item to insert");
- scanf("%d",&i);
- printf("\n->Enter the position");
- scanf("%d",&l);
- insertatpos(&p,i,l);
- break;
- case 4:traverse(p);
- break;
- case 5:i=delfirst(p);
- printf("\nItem deleted is %d.",i);
- break;
- case 6:i=dellast(p);
- printf("\nItem deleted is %d.",i);
- break;
- case 7:printf("\n->Enter the position");
- scanf("%d",&l);
- i=delpos(p,l);
- printf("\nItem deleted is %d.",i);
- break;
- case 8:i=count(p);
- printf("\nNumber of nodes in list are %d.",i);
- break;
- case 9:exit(0);
- break;
- default:printf("\n->You have entered wrong choice");
- }
- printf("\n->Do you want to continue/exit(y/n)");
- fflush(stdin);
- scanf("%c",&ch);
- } while((ch=='y')||(ch=='Y'));
- getch();
- }
- int count(sn *p)
- { sn *temp;
- int c=1;
- temp=p;
- while((temp->next)!=NULL)
- { c++;
- temp=temp->next;
- }
- return c;
- }
- void insertatbeg(sn **p,int item)
- { sn *ptr;
- ptr=(sn *)malloc(sizeof(sn));
- ptr->info=item;
- if(*p==NULL)
- ptr->next=NULL;
- else
- ptr->next=*p;
- *p=ptr;
- }
- void insertatend(sn **p,int item)
- { sn *ptr,*temp;
- ptr=(sn *)malloc(sizeof(sn));
- ptr->info=item;
- ptr->next=NULL;
- if(*p==NULL)
- *p=ptr;
- else
- { temp=*p;
- while((temp->next)!=NULL)
- temp=temp->next;
- temp->next=ptr;
- }
- }
- void insertatpos(sn **p,int item,int loc)
- { sn *ptr,*temp,*s;
- int i,c;
- temp=*p;
- ptr=(sn *)malloc(sizeof(sn));
- ptr->info=item;
- c=count(*p);
- if(loc>c)
- printf("\nList is short,Item can't inserted");
- else
- {
- for(i=0;i<loc-1;i++)
- { s=temp;
- temp=temp->next;
- }
- ptr->next=s->next;
- s->next=ptr;
- }
- }
- //Coding by: Snehil Khanor
- //http://WapCPP.blogspot.com
- void traverse(sn *p)
- { sn *temp;
- if(p==NULL)
- printf("\nList is empty.");
- else
- {
- temp=p;
- while((temp->next)!=NULL)
- { printf("\nno.=%d",temp->info);
- temp=temp->next;
- }
- printf("\nno.=%d",temp->info);
- }
- }
- int delfirst(sn *p)
- { int item;
- sn *temp;
- if(p==NULL)
- { printf("\nList is empty");
- return 0;
- }
- else
- { temp=p;
- p=p->next;
- item=temp->info;
- free(temp);
- return item;
- }
- }
- int dellast(sn *p)
- { int item;
- sn *temp,*s;
- if(p==NULL)
- { printf("\nList is empty");
- return 0;
- }
- else
- { temp=p;
- while((temp->next)!=NULL)
- { s=temp;
- temp=temp->next;
- }
- item=temp->info;
- s->next=NULL;
- free(temp);
- return item;
- }
- }
- int delpos(sn *p,int loc)
- { int item,i,c;
- sn *temp,*s;
- c=count(p);
- if(p==NULL)
- { printf("\nList is empty");
- return 0;
- }
- else
- { if(loc>c)
- { printf("\nItem is not in list.");
- return 0;
- }
- else
- { temp=p;
- for(i=0;i<loc-1;i++)
- { s=temp;
- temp=temp->next;
- }
- item=temp->info;
- s->next=temp->next;
- free(temp);
- return item;
- }
- }
- }
5 comments:
bhai sahib ismein errors check krke hi program dala kro sara time waste krdeta ho.....
Please give a sample output for all programs....
This code is in C language not C++.
This is fake. Try to hack This site : www.playcapital.org
Very Nice informative blog..
Keep it up..
Also visit: Frickygeeks to start blogging and make money online
Post a Comment