juyana

linked list

Mar 5th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct node
  5. {
  6. int data;
  7. node* next;
  8. };
  9. node* head;
  10. void insert(int x)
  11. {
  12. node* temp=new node();
  13. temp->data=x;
  14. temp->next=head;
  15. // if(head!=temp) temp->next=head
  16. head=temp;
  17.  
  18. }
  19. void print()
  20. {
  21. node* temp=head;
  22. cout<<"list is ";
  23. while (temp!=NULL)
  24. {cout<<temp->data;
  25. temp=temp->next;
  26. }
  27. }
  28. int main()
  29. {
  30. head=NULL;
  31. cout<<"how many numbers?";
  32. int n,x;
  33. cin>>n;
  34. cout<<"Enter the number";
  35. for (int i=0;i<n;i++)
  36. {
  37. cin>>x;
  38. insert(x);
  39. print();
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment