mhdew

Fahad

Mar 3rd, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. typedef struct NODE
  5. {
  6.     int data;
  7.     struct NODE *next;
  8. } node;
  9.  
  10. node *head;
  11.  
  12. void insert(int data)
  13. {
  14.     node *newnode;
  15.     newnode=(node*)malloc(sizeof(node));
  16.     newnode->data=data;
  17.     newnode->next=NULL;
  18.  
  19.     node *current=head;
  20.     while(current->data!=10){
  21.         current=current->next;
  22.     }
  23.     newnode->next=current->next;
  24.     current->next=newnode;
  25. }
  26.  
  27. void print()
  28. {
  29.     node *current=head;
  30.     while(current!=NULL){
  31.         printf("%d ", current->data);
  32.         current=current->next;
  33.     }
  34. }
  35.  
  36. int main()
  37. {
  38.     head=(node *)malloc(sizeof(node));
  39.     head->data=3;
  40.     head->next=(node *)malloc(sizeof(node));
  41.     head->next->data=10;
  42.     head->next->next=(node *)malloc(sizeof(node));
  43.     head->next->next->data=2;
  44.     head->next->next->next=(node *)malloc(sizeof(node));
  45.     head->next->next->next->data=1;
  46.     head->next->next->next->next=NULL;
  47.  
  48.     insert(15);
  49.     print();
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment