0% found this document useful (0 votes)
161 views9 pages

Menu Driven Program For Creation, Deletion, Insertion

This document describes a menu driven C program that implements operations on a linked list including creation, sorting, deletion, and insertion. The main menu allows the user to select these operations and includes options to create the list by inputting elements, sort in ascending or descending order, delete a specified element, insert a new element at a specified position, and display the list. Functions are defined to implement each operation including create, sort_asc, sort_dsc, deletion, insert, and print.

Uploaded by

shiv kapila
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
161 views9 pages

Menu Driven Program For Creation, Deletion, Insertion

This document describes a menu driven C program that implements operations on a linked list including creation, sorting, deletion, and insertion. The main menu allows the user to select these operations and includes options to create the list by inputting elements, sort in ascending or descending order, delete a specified element, insert a new element at a specified position, and display the list. Functions are defined to implement each operation including create, sort_asc, sort_dsc, deletion, insert, and print.

Uploaded by

shiv kapila
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

7. 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; }

You might also like