Menu Driven Program For Creation, Deletion, Insertion
Menu Driven Program For Creation, Deletion, Insertion
#include"process.h" #include"conio.h" #include"stdio.h" #include"malloc.h" struct node { int info; struct node *next; } *start,*rear; create(); sort_asc(); sort_dsc(); deletion(); insert(); print(struct node*); void main() { int g,q=1,k; struct node *ptr; clrscr();
while(q==1) { printf("1.create \n 2sort\n 3.deletion \n 4.Insert \n 5.dislay \n6.Exit"); scanf("%d",&k); switch(k) { case 1: create(); print(ptr); break; case 2:printf("ENter u r choice 1=asc 2=dsc"); scanf("%d",&g); switch(g) { case 1:sort_asc(); break; case 2:sort_dsc(); break; } break; case 3: deletion(); print(ptr); break; case 4:insert();
{ print(ptr); } break; case 5:print(ptr); break; case 6:printf("BYEEEEEEEEEEE"); exit(1); break; } } getch(); } create() { struct node *node; printf("Enter elements and press 0 to exit"); if(start==NULL) { node=(struct node*)malloc(sizeof(struct node)); scanf("%d",&node->info); start=node; start->next=NULL;
rear=start; } while(1) { node=(struct node*)malloc(sizeof(struct node)); scanf("%d",&node->info); if(node->info==0) break; node->next=NULL; rear->next=node; rear=node; }} sort_asc() { int temp; char choice; struct node *t1; t1=(struct node*)malloc(sizeof(struct node)); struct node *t2; t2=(struct node*)malloc(sizeof(struct node)); for(t1=start;t1!=NULL;t1=t1->next) { for(t2=t1->next;t2!=NULL;t2=t2->next)
{ if(t1->info>t2->info) { temp=t1->info; t1->info=t2->info; t2->info=temp; } } printf("%d \t",t1->info); } } sort_dsc() { int temp; char choice; struct node *t1; t1=(struct node*)malloc(sizeof(struct node)); struct node *t2; t2=(struct node*)malloc(sizeof(struct node)); for(t1=start;t1!=NULL;t1=t1->next) { for(t2=t1->next;t2!=NULL;t2=t2->next) {
if(t1->info<t2->info) { temp=t1->info; t1->info=t2->info; t2->info=temp; } } printf("%d \t",t1->info); } } deletion() { int item; struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); temp=start; struct node *temp1; temp1=(struct node*)malloc(sizeof(struct node)); temp1=temp; printf("WEnter the item number to delete"); scanf("%d",&item); for(int i=1;i<item;i++) {
temp1=temp; //stores previous node temp=temp->next; //store current node } temp1->next=temp->next; free(temp); } insert() { int item,d; struct node *temp1; printf("INSERT"); scanf("%d",&item); temp1=(struct node*)malloc(sizeof(struct node)); temp1=start; for(int i=1;i<item;i++) { temp1=temp1->next; if(temp1==NULL) { printf("not exist"); break; } }
struct node *temp; temp=(struct node*)malloc(sizeof(node)); printf("Enter the item"); scanf("%d",&d); temp->info=d; temp->next=temp1->next; temp1->next=temp; } print (struct node* ptr) { ptr=start; while(ptr!=NULL) { printf("%d \n",ptr->info); ptr=ptr->next; }