Facebook
From Harmless Mockingbird, 3 Years ago, written in C++.
This paste is a reply to Untitled from Ivory Anoa - view diff
Embed
Download Paste or View Raw
Hits: 92
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. struct node
  4.  
  5. {
  6. int data;
  7. struct node *next;
  8. };
  9.  
  10.     int main()
  11.  
  12. {
  13.     struct node *prev,*head,*p;
  14.     head=NULL;
  15.     int n;
  16.     scanf("%d",&n);
  17.     for(int i=0;i<n;i++)
  18.     {
  19.         p=(struct node*)malloc(sizeof(struct node));
  20.         scanf("%d",&p->data);
  21.         p->next=NULL;
  22.         if(head==NULL){
  23.         head=p;
  24.     }
  25.     else {
  26.         prev->next=p;
  27.     }
  28.     prev=p;
  29.     }
  30.  
  31.     int x;
  32.     printf("Enter position: ");
  33.     scanf("%d",&x);
  34.  
  35.     struct node *ptr=head;
  36.    int i;
  37.    while(ptr!=NULL)
  38.    {
  39.        ++i;
  40.        if(i==x)
  41.        {
  42.          prev->next=ptr->next;
  43.          free(ptr);
  44.          break;
  45.        }
  46.        prev=ptr;
  47.        ptr=ptr->next;
  48.    }
  49.  
  50.  
  51.  
  52.    ptr=head;
  53.    while(ptr!=NULL)
  54.    {
  55.        printf("%d ", ptr->data);
  56.        ptr=ptr->next;
  57.    }
  58.     return 0;
  59. }
  60.