Thursday, 21 November 2013

Program In C For Insertion And Deletion In A Linked List

#include<stdio.h>
#include<alloc.h>
#include<conio.h>
struct node
{ int info;
 struct node *next;
 }*start=NULL,*current;
 void insert(int num)
 {
 struct node *new_n=(struct node*)malloc(sizeof(struct node));
 if(start==NULL)
 { start=new_n;
 new_n->info=num;
 new_n->next=NULL; }
 else
 { current=start;
 while(current->next!=NULL)
{ current=current->next;
}
current->next=new_n;
 new_n->info=num;
 new_n->next=NULL; } }
 void deletion()
 {
 printf("%d node deleted ! ",start->info);
 current=start;
 start=start->next;
 free(current);
 }
 void display( )
 { struct node *ptr;
 ptr=start;
 while(ptr!=NULL)
 { printf("%d->",ptr->info);
 ptr=ptr->next; }
 }
void main()
{
int ch,ch1=1,num,c;
clrscr();
again:
printf("Linked List Menu:\n1.Insertion(Press 1)\n2.Deletion(Press2)\n3.Display LInked List(Press 3): \n");
scanf("%d",&ch);
if(ch==1)
{ ch1=1;
while(ch1==1)
{ printf("\nEnter The Number: ");
scanf("%d",&num);
insert(num);
printf("\nLinked List After Insertion Is: \n");
display();
printf("\nDo You Wish To Insert More(Press 1 for yes): ");
scanf("%d",&ch1);}}
if(ch==2)
{ ch1=1;
while(ch1==1)
{ deletion();
printf("\nLinked List After Deletion Is: \n");
display();
printf("\nDo You Wish To Delete More(Press 1 for yes): ");
scanf("%d",&ch1);}}
if(ch==3)
{ display(); }
printf("\n1.Back To Main Menu(Press 1)\n2.Exit(Press 2):");
scanf("%d",&c);
if(c==1)
goto again;
getch();
}

No comments:

Post a Comment