Advertisement
vaibhav1906

LL(Head-add)

Jun 27th, 2021
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Node{
  4. public:
  5. int val;
  6. Node * next;
  7. };
  8.  
  9. Node * insert(Node * head , int x){
  10. Node * a = new Node();
  11. a->val = x;
  12.  
  13. if(head==NULL){
  14. head = a;
  15. }
  16.  
  17. else{
  18. a->next = head;
  19. head = a;
  20. }
  21.  
  22.  
  23. return head;
  24. }
  25.  
  26. void printl(Node * head){
  27. Node * temp = head;
  28.  
  29. while(temp!=NULL){
  30. cout<<temp->val<<" ";
  31. temp = temp->next;
  32. }
  33.  
  34. }
  35.  
  36. int main(){
  37. int n;
  38. Node * head= NULL;
  39. cout<<"Enter the size of your linkedlist : \n";
  40. cin>>n;
  41.  
  42. for(int i=0; i<n; i++){
  43. cout<<"Enter a number : \n";
  44. int x; //3->2->1
  45. cin>>x;
  46. head = insert(head,x);
  47. }
  48.  
  49. printl(head);
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement