/*Write a program to create a singly linked list. 1->Creation. 2->insertion. 3->deletion.*/ #include<stdio.h> #include<stdlib.h> void creation(); void display(); void insert_end(); void insert_beginning(); void insert_middle(); void deletion(); struct link_list { int data; struct link_list *next; }*start=NULL; typedef struct link_list node; node *current,*nnew; void main() { int a; char ch; clrscr(); creation(); do { printf("\nenter your choice\n1->display\n2->insert at end\n3->insert at beginning\n4->insert b/w two nodes\n5->deletion\n"); scanf("%d",&a); switch(a) { case 1: display(); break; case 2: { insert_end(); } break; case 3: { insert_beginning(); } break; case 4: { insert_middle(); } break; case 5: { deletion(); } break; default: printf("wrong choice"); } printf("\npress any key to continue and press n to quit"); scanf("%s",&ch); }while(ch!='n'); } void creation() { int i=0,k; printf("how many nodes you want to create "); scanf("%d",&i); while(i!=0) { nnew=(node*)malloc(sizeof(node)); nnew->next=NULL; if(start==NULL) { printf("enter any integer data "); scanf("%d",&k); nnew->data=k; start=nnew; current=start; } else { printf("enter any integer data "); scanf("%d",&k); nnew->data=k; current->next=nnew; current=nnew; } i--; } } void display() { int i=1; current=start; while(current!=NULL) { printf("%d:: %d\n",i ,current->data); current=current->next; i++; } getch(); } void insert_end() { current=start; while(current->next!=NULL) { current=current->next; if(current->next==NULL) { nnew=(node*)malloc(sizeof(node)); nnew->next=NULL; printf("enter the deta for this node "); scanf("%d",&nnew->data); current->next=nnew; current=nnew; printf("node inserted successfully"); } } } void insert_beginning() { nnew=(node*)malloc(sizeof(node)); nnew->next=NULL; printf("enter the deta for this node "); scanf("%d",&nnew->data); nnew->next=start; start=nnew; printf("node inserted successfully\n"); } void insert_middle() { int a,i,pos,b; current=start; nnew=(node*)malloc(sizeof(node)); nnew->next=NULL; printf("enter the deta for this node "); scanf("%d",&nnew->data); printf("enter the serial number of node accept first and the last node "); scanf("%d",&pos); printf("enter 1 to insert after the selected node and 2 for before "); scanf("%d",&a); if(a==1) { current=start; for(i=1;i<pos;i++) { current=current->next; } nnew->next=current->next; current->next=nnew; } else if(a==2) { current=start; for(i=1;i<pos-1;i++) { current=current->next; } nnew->next=current->next; current->next=nnew; } else { printf("wrong choice"); } } void deletion() { int y,i; current=start; printf("enter the serial number of node you want to delete "); scanf("%d",&y); if(y==1) { start=start->next; } for(i=1;i<y-1;i++) { current=current->next; } if(current->next->next==NULL) { current->next=NULL; } else { current->next=current->next->next; } printf("node is deleted"); }
I started this Blog to Share my learning's with the community. i share what I learn day by day and how I improve my skills etc. Hope you will find something useful in it.
Thursday, 13 March 2014
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment