Sie sind auf Seite 1von 6

/*A program to create a linked list and perform operations such as insertion,deletion,update,reverse in a link list developed by roll no.

2308244*/ #include<stdio.h> #include<conio.h> #include<malloc.h> struct list { int info; struct list *link; }; typedef struct list node; node *start;

void createlist(node *); void insertatbeg(node *start); void insatend(node *start); void invert(node *start); void delatbeg(node *start); void delatlast(node *start); void display(node *start); void main() { char ch,x,tryy; int item,ch1; node *start; choice: clrscr(); printf("\ncreate list (Press 1)"); printf("\ninsert at begining(press 2)"); printf("\ninsert item at last(Press 3"); printf("\nReverse the LINK LIST (Press 4)"); printf("\ndelete item from beg (press 5)"); printf("\ndelete item from last(Press 6)"); printf("\nDisplay Link List ELEMENTS (Press 7)"); printf("\nExit (Press 0)"); scanf("%d",&ch1); switch (ch1) { case 1: createlist(&start);

getch(); goto choice; case 2: insertatbeg(&start); getch(); goto choice; case 3: insatend(&start); getch(); goto choice; case 4: invert(start); getch(); goto choice; case 5: delatbeg(&start); getch(); goto choice; case 6: delatlast(start); getch(); goto choice; case 7: display(start); getch(); goto choice; case 0: printf("\n\nAre u sure (y/n)"); x=getche(); if(x=='y'||x=='Y') { exit(0); } default: printf("You Enter Wrong Choice....Try Again(y/n)"); tryy=getche(); if(tryy=='y'||tryy=='Y') goto choice; else exit(0); } getch(); } void createlist(node **start)

{ int item; char ch2; node *ptr,*ptr1; (*start)=NULL; do { ptr=(node *)malloc(sizeof(node)); printf("\n Enter an item"); scanf("%d",&item); ptr->info=item; ptr->link=(*start); (*start)=ptr; printf("\n Do you want to enter more node(y/n)"); ch2=getche(); }while(ch2=='y'||ch2=='Y'); } void insertatbeg(node **start) { node*ptr,*new1; new1=(node*)malloc(sizeof(node)); printf("%d",&new1->info); new1->link=(*start); (*start)=new1; } void insatend(node **start) { node *ptr,*new1; ptr=(*star)t; new1=(node*)malloc(sizeof(node)); new1->link=NULL; while(ptr->link!=NULL) { ptr=ptr->link; } ptr->link=new1; }

void invert(node *start) {

node *ptr,*pre,*r; ptr=start; r=NULL; while(ptr!=NULL) { pre=ptr->link; ptr->link=r; r=ptr; ptr=pre; printf("\n%d",r->info); } start=r;

void delatbeg(node **start) { node *ptr; ptr=*start; *start=*start->link; ptr->link=NULL; } void delatlast(node **start) { node *ptr,*pre; int item; printf("\nenter data to be deleted"); ptr=start; if(start==NULL) { printf("\nempty list"); exit(0); } while(ptr->link!=NULL) { pre=ptr; ptr=ptr->link; }

pre->link=NULL; free(ptr); } void display(node *start) { node *ptr; printf("\nentered data is\n\n"); ptr=start; printf("\n\n\n"); while(ptr!=NULL) { printf("%d\t",ptr->info); ptr=ptr->link; } } void delatbeg(node *start) { node *ptr; ptr=start; start=start->link; ptr->link=NULL; } void delatlast(node *start) { node *ptr,*pre; int item; printf("\nenter data to be deleted"); ptr=start; if(start==NULL) { printf("\nempty list"); exit(0); } while(ptr->link!=NULL) { pre=ptr; ptr=ptr->link; } pre->link=NULL; free(ptr);

} void count(node *start) { node *ptr; int count=0; ptr=start; while(ptr!=NULL) { count=count+1; ptr=ptr->link; } printf("\nnumber of nodes=%d",count); }

Das könnte Ihnen auch gefallen