Sunday 25 March 2012

program to delete the element from linked list after the given value


program to delete the element from linked list after the given value.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
struct node
 {
  int data;
  struct node *link;
 }*start,*temp,*prev,*n1;
void display();
void main()
 {
  int n,i;
  char ch;
  clrscr();
  start=NULL;
  prev=NULL;
  while(1)
   {
    if(start==NULL)
     {
                temp=(struct node*)malloc(sizeof(struct node));
                printf("\nEnter value for Node:- ");
                scanf("%d",&temp->data);
                start=temp;
                temp->link=NULL;
     }
     else
     {
      temp->link=(struct node*)malloc(sizeof(struct node));
      temp=temp->link;
      printf("\nEnter value for Node:- ");
      scanf("%d",&temp->data);
     }
      printf("\nDo you want to create one more node(Y/N) :- ");
      fflush(stdin);
      scanf("%c",&ch);
      if(ch!='y')
       {
                temp->link=NULL;
                break;
       }
   }
   printf("\n\nThe nodes before Deleting:\n");
   display();
   printf("\n\nEnter node value after which you want to Delete:-");
   scanf("%d",&n);
   temp=start;
   while(temp->data!=n)
    {
     prev=temp;
     temp=temp->link;
    }
   n1=temp->link;
   prev->link=n1;
   temp->link=NULL;
   free(temp);
   printf("\n\nThe nodes after Deleting:\n");
   display();
                getch();
}
void display()
 {
  prev=start;
  while(prev!=NULL)
   {
    printf("%5d",prev->data);
    prev=prev->link;
   }
 }



output:
Enter value for Node:- 45
Do you want to create one more node(Y/N) :- y
Enter value for Node:- 63
Do you want to create one more node(Y/N) :- y
Enter value for Node:- 14
Do you want to create one more node(Y/N) :- y
Enter value for Node:- 95
Do you want to create one more node(Y/N) :- n
The nodes before Deleting:
   45   63   14   95
Enter node value after which you want to Delete:-63
The nodes after Deleting:
   45   14   95

No comments:

Post a Comment