Code: #include<stdio.h> #include<conio.h> #include<malloc.h> struct node { int num; struct node *next; }; typedef struct node NODE; NODE *start=NULL; NODE *last=NULL; void eledisplay(); eleinsert(NODE *start); eledelete(NODE *start); void main() { int ch; do { printf("1.insert\n"); printf("2.Delete\n"); printf("3.Display\n"); printf("4.Exit"); printf("Enter ur choice"); scanf("%d",&ch); switch(ch) { case 1:start=eleinsert(start); break; case 2:eledelete(start); break; case 3:eledisplay(); break; case 4:return; } } while(ch!=4); } NODE *eleinsert(NODE *start) { NODE *p; p=(NODE *)malloc(sizeof(NODE)); printf("Enter ele to insert\n"); scanf("%d",&p->num); if(start==NULL) { p->next=p; start=p; last=p; } else { p->next=start; start=p; last->next=p; } return start; } void eledisplay() { NODE *p; p=start; if(p==NULL) { printf("List is empty"); } else { while(p->next!=start) { printf("%d",p->num ); p=p->next; } } } NODE *eledelete(NODE *start) { NODE *p; p=start; if(p==NULL) { printf("List is empty"); } else { p=start; start=start->next; printf("Deleted ele is:%d",p->num); last->next=p; } return start; }
Your deletion logic is not at all right. Search the forum and you will find code on how to delete a node in a linked list