Advertisement
maycod23

Untitled

May 11th, 2022
649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class node {
  4. public:
  5.     int data;
  6.     node* next;
  7.     node(int data)
  8.     {
  9.         this->data = data;
  10.         next = NULL;
  11.     }
  12. };
  13. node* takeinput(node* &head)
  14. {
  15.     int data; cin >> data;
  16.     node* temp = NULL;
  17.     while (data != -1)
  18.     {
  19.         node* n1 = new node(data); //dynamic
  20.         if (head == NULL)
  21.         {
  22.             head = n1;
  23.             temp = n1;
  24.         }
  25.         else
  26.         {
  27.             temp->next = n1;
  28.             temp = n1;
  29.             // node* temp = head;
  30.             // while (temp != NULL)
  31.             // {
  32.             //  temp = temp->next;
  33.             // }
  34.             // temp = n1;
  35.  
  36.             // while (temp->next != NULL)
  37.             // {
  38.             //  temp = temp->next;
  39.             // }
  40.             // temp->next = n1;
  41.         }
  42.         cin >> data;
  43.     }
  44.     while (head != NULL)
  45.     {
  46.         cout << head->data << " ";
  47.         head = (head->next);
  48.     }
  49.     return head;
  50. }
  51. void printlist(node* head)
  52. {
  53.     // node* temp = head;
  54.     while (head != NULL)
  55.     {
  56.         cout << head->data << " ";
  57.         head = (head->next);
  58.     }
  59. }
  60. void fun(node* n1)
  61. {
  62.     n1->data = 100;
  63. }
  64. int main()
  65. {
  66.  
  67.  
  68.     node* head = NULL;
  69.     head = takeinput(head);
  70.     // printlist(head);
  71.  
  72.  
  73.     // node* n1 = new node(10);
  74.     // fun(n1);
  75.     // // node n2(100);//static
  76.     // cout << n1->data << endl;
  77.     // // cout << n2.data << endl;
  78.  
  79.  
  80.     //static
  81.     // node n1(10), n2(20), n3(30), n4(40), n5(50);
  82.  
  83.     // // cout << n1.data << endl;
  84.     // // cout << n2.data << endl;
  85.     // // cout << endl;
  86.     // //linking process
  87.  
  88.  
  89.     // node* head = &n1;
  90.     // n1.next = &n2;
  91.     // n2.next = &n3;
  92.     // n3.next = &n4;
  93.     // n4.next = &n5;
  94.  
  95. //print the values of list
  96.     // while (head != NULL)
  97.     // {
  98.     //  //currently i am at head
  99.     //  cout << (head->data) << endl;
  100.     //  head = (head->next);
  101.     // }
  102.  
  103.  
  104.     //dynamic process
  105.  
  106.  
  107.     // node *n1 = new node(10);
  108.     // node *n2 = new node(20);
  109.  
  110.     // cout << (n1->data) << endl;
  111.     // cout << (n2->data) << endl;
  112.  
  113.     // //linking
  114.     // cout << endl;
  115.     // n1->next = n2;
  116.  
  117.     // cout << (n1->next->data) << endl;
  118.     // node* head = takeinput();
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement