Facebook
From Harmless Crane, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 53
  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 *pre,*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.         pre->next=p;
  27.     }
  28.     pre=p;
  29.     }
  30.  
  31.   int x,y;
  32.   scanf("%d %d",&x,&y);
  33.  
  34.   struct node* ptr;
  35.   ptr=head;
  36.  
  37.   int i=0;
  38.   while(ptr!=NULL)
  39.    {
  40.     i++;
  41.     if(i==x){
  42.     ptr->data=y;
  43.     break;
  44.     }
  45.     ptr=ptr->next;
  46.    }
  47.    ptr=head;
  48.    while(ptr!=NULL)
  49.    {
  50.        printf("%d ", ptr->data);
  51.        ptr=ptr->next;
  52.    }
  53.     return 0;
  54. }
  55.