Sunday 25 March 2012

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


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

#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 :- ");
      fflush(stdin);
      scanf("%c",&ch);
      if(ch!='y')
       {
                temp->link=NULL;
                break;
       }
   }
    printf("\nThe nodes before Deleting:\n");
   display();
   printf("\nEnter Place where you want to Delete :- ");
   scanf("%d",&n);
   temp=start;
   for(i=1;i<=n-1;i++)
   {
    prev=temp;
    temp=temp->link;
   }
   n1=temp->link;
   temp->link=NULL;
   prev->link=n1;
   printf("\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
Enter value for Node :- 65
Do you want to create one more node :- y
Enter value for Node :- 15
Do you want to create one more node :- y
Enter value for Node :- 20
Do you want to create one more node :- n
The nodes before Deleting:
   45   65   15   20
Enter Place where you want to Delete :- 3
the nodes after Deleting:
   45   65   20

No comments:

Post a Comment