alansam

link-problem.c

Nov 28th, 2021 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void insert_at_end();
  4. struct node{
  5.     int data;
  6.     struct node *link;
  7. };
  8. int main()
  9. {
  10.     struct node *head=malloc(sizeof(struct node));
  11.     struct node *ptr=malloc(sizeof(struct node));
  12.     struct node *pr=malloc(sizeof(struct node));
  13.     head->data=45;
  14.     head->link=ptr;
  15.     ptr->data=50;
  16.     ptr->link=pr;
  17.     pr->data=55;
  18.     pr->link=NULL;
  19.     while(head!=NULL)
  20.     {
  21.         printf("->%d",head->data);
  22.         head=head->link;
  23.     }
  24.     insert_at_end(head,60);
  25.     struct node *ptnr=head;
  26.     while(ptnr!=NULL)
  27.     {
  28.         printf("->%d",ptnr->data);
  29.         ptnr=ptnr->link;
  30.     }
  31. }
  32. void insert_at_end(struct node *head, int data)
  33. {
  34.     struct node *pnt,*temp;
  35.     pnt=head;
  36.     temp->data=60;
  37.     temp->link=NULL;
  38.     while(pnt->link != NULL)
  39.     {
  40.         pnt=pnt->link;
  41.     }
  42.     pnt->link= temp;
  43. }
  44.  
Add Comment
Please, Sign In to add comment