//Program for Singly Linked List #include #include struct node { - TopicsExpress



          

//Program for Singly Linked List #include #include struct node { int info; struct node *next; }; void insert_at_begin(struct node **start,int num) { struct node *newnode; newnode=(struct node*)malloc(sizeof(struct node)); if(newnode==NULL) { clrscr(); printf("a NOT ENOUGH MEMORY !"); getch(); exit(0); } else { newnode->info=num; newnode->next=NULL; if(*start==NULL) *start=newnode; else { newnode->next=*start; *start=newnode; } clrscr(); printf(" Your Info Has been Successfully Added !"); printf(" Press any key to continue...");getch(); } } void insert_at_end(struct node **start,int num) { struct node *temp,*newnode; newnode=(struct node*)malloc(sizeof(struct node)); if(newnode==NULL) { clrscr(); printf("a NOT ENOUGH MEMORY !"); getch(); exit(0); } else { newnode->info=num; newnode->next=NULL; if(*start==NULL) *start=newnode; else { temp=*start; while(temp->next!=NULL) temp=temp->next; temp->next=newnode; clrscr(); printf(" Your Info Has been Successfully Added !"); printf(" Press any key to continue...");getch(); } } } void insert_at_pp(struct node **start,int num,int pos) { struct node *temp,*newnode;int count=0,i; temp=*start; while(temp) { count++; temp=temp->next; } if(pos==1) insert_at_begin(start,num); else if(pos==count+1) insert_at_end(start,num); else if(pos>count+1) { clrscr();printf("a Invalid Choose !"); printf(" Press any key to continue...");getch(); } else { newnode=(struct node*)malloc(sizeof(struct node)); if(newnode==NULL) { clrscr(); printf("a NOT ENOUGH MEMORY !"); getch(); exit(0); } newnode->info=num; newnode->next=temp; temp=*start; for(i=0;inext; newnode->next=temp->next; temp->next=newnode; clrscr(); printf(" Your Info Has been Successfully Added !"); printf(" Press any key to continue...");getch(); } } void traverse(struct node *start) { clrscr(); if(start==NULL) { printf("a There is No Any Information In Linked List"); printf(" Press any key to continue..."); getch(); } else { printf(" Total Information in Linked List "); while(start) { printf(" %d",start->info); start=start->next; } printf(" Press any key to continue..."); getch(); } } void delete_at_begin(struct node **start) { struct node *temp; if(*start==NULL) { clrscr(); printf("a There is No Any Information In Linked List"); printf(" Press any key to continue...");getch(); } else if((*start)->next==NULL) { free(*start); *start=NULL; clrscr(); printf(" Your Info Has been Deleted Successfully !"); printf(" Press any key to continue...");getch(); } else { temp=(*start)->next; free(*start); *start=temp; clrscr(); printf(" Your Info Has been Deleted Successfully !"); printf(" Press any key to continue...");getch(); } } void delete_at_end(struct node **start) { struct node *temp; if(*start==NULL) { clrscr(); printf("a There is No Any Information In Linked List"); printf(" Press any key to continue...");getch(); } else if((*start)->next==NULL) { free(*start); *start=NULL; clrscr(); printf(" Your Info Has been Deleted Successfully !"); printf(" Press any key to continue...");getch(); } else { temp=*start; while(temp->next->next!=NULL) temp=temp->next; free(temp->next); temp->next=NULL; clrscr(); printf(" Your Info Has been Deleted Successfully !"); printf(" Press any key to continue...");getch(); } } void delete_at_pp(struct node **start,int num) { struct node *back,*temp,*ptr;int flag=0; temp=*start; back=NULL; if(*start==NULL) { clrscr();flag=1; printf("a There is No Any Information In Linked List"); printf(" Press any key to continue...");getch(); } else if(temp->info==num) { flag=1; delete_at_begin(start); } else { while(temp) { if(temp->info==num) { flag=1; back->next=temp->next; free(temp); clrscr(); printf(" Your Info Has been Deleted Successfully !"); printf(" Press any key to continue...");getch(); break; } else { back=temp; temp=temp->next; } } } if(flag==0) { clrscr(); printf("a %d is not present in the Linked List !",num); printf(" Press any key to return...");getch(); } } void main() { struct node *head; int num,choice,pos; _setcursortype(0);textbackground(2);textcolor(WHITE); clrscr();head=NULL; do { clrscr();printf(" Linked List Operation "); printf(" 1.Insert At Begining"); printf(" 2.Insert At End"); printf(" 3.Insert At Particular Position"); printf(" 4.Traversing"); printf(" 5.Deletion At Begining"); printf(" 6.Deletion At End"); printf(" 7.Deletion At Particular Position"); printf(" 8.Exit"); printf(" Choose Your Option & Press Enter Key : "); scanf("%d",&choice); switch(choice) { case 1: clrscr(); printf(" Enter an Integer No : "); scanf("%d",&num); insert_at_begin(&head,num); break; case 2: clrscr(); printf(" Enter an Integer No : "); scanf("%d",&num); insert_at_end(&head,num); break; case 3: clrscr(); printf(" Enter an Integer No : "); scanf("%d",&num); printf(" Enter the Position where You want to Add info : "); scanf("%d",&pos); insert_at_pp(&head,num,pos); break; case 4: traverse(head); break; case 5: delete_at_begin(&head); break; case 6: delete_at_end(&head); break; case 7: clrscr(); printf(" "); printf(" Enter the Information which You want to Delete : "); scanf("%d",&num); delete_at_pp(&head,num); break; case 8: exit(0); break; default:clrscr();printf("a INVALID CHOOSE !"); printf(" Press any key to continue...");getch(); } }while(choice!=8); }
Posted on: Sun, 07 Jul 2013 07:04:40 +0000

Trending Topics



Recently Viewed Topics




© 2015