Advertisement
satriafu5710

Menambahkan Node Depan C++

Dec 24th, 2021
1,610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node
  5. {
  6.     int data = 0;
  7.  
  8.     Node *berikut = NULL;
  9. };
  10.  
  11. Node *A = new Node;
  12. Node *B = new Node;
  13. Node *C = new Node;
  14. Node *X = new Node;
  15.  
  16. int tambahNode_depan(Node *X)
  17. {
  18.  
  19.     cout << "\n Inputkan data node : ";
  20.     cin >> X->data;
  21.  
  22.     X->berikut = A;
  23. }
  24.  
  25. int cetakNode(Node *A)
  26. {
  27.  
  28.     Node *jalan = A;
  29.  
  30.     while (jalan != NULL)
  31.     {
  32.  
  33.         cout << jalan->data << " ";
  34.  
  35.         jalan = jalan->berikut;
  36.     }
  37. }
  38.  
  39. int main()
  40. {
  41.  
  42.     Node *root = new Node;
  43.  
  44.     cout << "\n\t Program Menambahkan Node Depan \n";
  45.  
  46.     cout << "\n Node sebelum ditambahkan : ";
  47.  
  48.     // Data-data nodenya
  49.     A->data = 3;
  50.     A->berikut = B;
  51.  
  52.     B->data = 4;
  53.     B->berikut = C;
  54.  
  55.     C->data = 6;
  56.     C->berikut = NULL;
  57.  
  58.     root = A;
  59.     cetakNode(root);
  60.  
  61.     cout << endl;
  62.  
  63.     root = X;
  64.     tambahNode_depan(X);
  65.  
  66.     cout << "\n Node setelah ditambahkan : ";
  67.     cetakNode(root);
  68.  
  69.     cout << endl;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement