luthfy13

Soal UTS

Oct 24th, 2021 (edited)
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node{
  5.     int data;
  6.     Node *next;
  7.     Node *prev;
  8. };
  9.  
  10. Node *n=NULL, *head=NULL, *tail=NULL, *x=NULL;
  11.  
  12. void buatNodeBaru(int i){
  13.     n = new Node;
  14.     n->data = i;
  15.     n->prev = NULL;
  16.     head = n;
  17.     tail = n;
  18.     tail->next = NULL;
  19. }
  20.  
  21. void tambahDiBelakang(int i){
  22.     n = new Node;
  23.     n->data = i;
  24.     n->prev = tail;
  25.     tail->next = n;
  26.     tail = n;
  27.     tail->next = NULL;
  28. }
  29.  
  30. void tambahDiDepan(int i){
  31.     n = new Node;
  32.     n->data = i;
  33.     n->next = head;
  34.     head->prev = n;
  35.     n->prev = NULL;
  36.     head = n;
  37. }
  38.  
  39. void tambahDiTengah(int i, int j){
  40.     x = head;
  41.     while(x->data != j) x=x->next;
  42.     n = new Node;
  43.     n->data = i;
  44.     n->next = x->next;
  45.     x->next = n;
  46.     n->prev = x;
  47.     x = n->next;
  48.     x->prev = n;
  49. }
  50.  
  51. void tambahData(int i){
  52.     if (head==NULL)
  53.         buatNodeBaru(i);
  54.     else
  55.         tambahDiBelakang(i);
  56. }
  57.  
  58. void hapusDiDepan(){
  59.     x = head;
  60.     head = x->next;
  61.     head->prev = NULL;
  62.     delete(x);
  63.     x = NULL;
  64. }
  65.  
  66. void hapusDiBelakang(){
  67.     x = tail;
  68.     tail = tail->prev;
  69.     tail->next = NULL;
  70.     delete(x);
  71.     x = NULL;
  72. }
  73.  
  74. void hapusDiTengah(int i){
  75.     n = NULL;
  76.     x = head;
  77.     while(x->data != i){
  78.         n = x;
  79.         x = x->next;
  80.     }
  81.     n->next = x->next;
  82.     x = x->next;
  83.     delete(x->prev);
  84.     x->prev = n;
  85.        
  86. }
  87.  
  88. void tampilDariDepan(){
  89.     x = head;
  90.     while(x != NULL){
  91.         cout << x->data << " ";
  92.         x = x->next;
  93.     }
  94. }
  95.  
  96. void tampilDariBelakang(){
  97.     x = tail;
  98.     while(x != NULL){
  99.         cout << x->data << " ";
  100.         x = x->prev;
  101.     }
  102. }
  103.  
  104. bool cariData(int i){
  105.     x = head;
  106.     while(x != NULL){
  107.         if (i == x->data) return true;
  108.         x = x->next;
  109.     }
  110.     return false;
  111. }
  112.  
  113. void hapusData(int i){
  114.     if (!cariData(i) || head == NULL) return;
  115.    
  116.     if (i == head->data)
  117.         hapusDiDepan();
  118.     else if (i == tail->data)
  119.         hapusDiBelakang();
  120.     else
  121.         hapusDiTengah(i);
  122. }
  123.  
  124. int getJmlNode(){
  125.     // lengkapi isi method ini
  126.     // method berfungsi untuk mengembalikan nilai
  127.     // berupa jumlah node yg terdapat dalam list
  128. }
  129.  
  130. int main(){
  131.     tambahData(10);
  132.     tambahData(20);
  133.     tambahData(30);
  134.     tambahData(40);
  135.     tambahData(50);
  136.     tambahData(60);
  137.  
  138.     cout << "Jumlah node: " << getJmlNode() << endl; //output ->  Jumlah node: 6
  139. }
  140.  
Add Comment
Please, Sign In to add comment