Advertisement
Guest User

Insert beginning in Linked list

a guest
Oct 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<iostream>
  3. using namespace std;
  4. struct Node
  5. {
  6.     int data;
  7.     Node* next;
  8. };
  9. Node* head;
  10. void insert(int x)
  11. {
  12.     Node* newnode;
  13.     newnode = (Node*)malloc(sizeof(Node));
  14.     newnode->data=x;
  15.     newnode->next=head;
  16.     head=newnode;
  17. }
  18. void print()
  19. {
  20.     Node* newnode;
  21.     cout<<"List is : ";
  22.     while(newnode!=NULL)
  23.     {
  24.         cout<<newnode->data<<" ";
  25.         newnode=newnode->next;
  26.     }
  27.     cout<<"\n";
  28. }
  29. int main()
  30. {
  31.     head=NULL;
  32.     int i, n, x;
  33.     cout<<"How many nodes?\n";
  34.     cin>>n;
  35.     for(i=0;i<n;i++)
  36.     {
  37.         cout<<"Enter the number: \n";
  38.         cin>>x;
  39.         insert(x);
  40.         print();
  41.     }
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement