Advertisement
ChaeYuriya

SLL.cpp

Nov 3rd, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include <iostream>
  2. #include "SLL.h"
  3. #define first(L) L.first
  4. #define next(P) P->next
  5. #define info(P) P->info
  6. #define judul(P) P->judul
  7.  
  8. using namespace std;
  9.  
  10. void createList(List &L){
  11.     first(L) = NULL;
  12. }
  13.  
  14. address alokasi(string X){
  15.     address P = new elmList;
  16.     info(P) = X;
  17.     next(P) = NULL;
  18.     return P;
  19. }
  20.  
  21. void insertFirst(List &L, address P){
  22.     next(P) = first(L);
  23.     first(L) = P;
  24. }
  25.  
  26. void insertLast(List &L, address P){
  27.     if(first(L) == NULL){
  28.         insertFirst(L,P);
  29.     }else{
  30.         address cur = first(L);
  31.         while(next(cur)!= NULL){
  32.             cur = next(cur);
  33.         }
  34.         next(cur) = P;
  35.     }
  36. }
  37.  
  38. void inserAfter(List &L, address &Target,address P){
  39.     address cur = first(L);
  40.     while(next(cur)!= NULL){
  41.         if(cur == Target){
  42.             address nextTarget = next(cur);
  43.             next(cur) = P;
  44.             next(P) = nextTarget;
  45.             break;
  46.         }
  47.         cur = next(cur);
  48.     }
  49. }
  50.  
  51.  
  52. void deleteFirst(List &L, address &P){
  53.     P = first(L);
  54.     address currentFirst = next(P);
  55.     next(P) = NULL;
  56.     first(L) = currentFirst;
  57. }
  58.  
  59. void deleteLast(List &L, address &P){
  60.     address cur = first(L);
  61.     while(next(cur)!= NULL){
  62.         address check = next(cur);
  63.         if(next(check) == NULL){
  64.             next(cur) = NULL;
  65.             cur = check;
  66.             break;
  67.         }
  68.         cur = next(cur);
  69.     }
  70.     P = cur;
  71. }
  72.  
  73. void deleteAfter(List &L, address Target,address &P){
  74.     address cur = first(L);
  75.     while(next(cur)!= NULL){
  76.         if(cur == Target){
  77.             P = next(cur);
  78.             address nextVal = next(P);
  79.  
  80.             if(nextVal != NULL){
  81.                 next(cur) = next(P);
  82.             }else{
  83.                 next(cur) = NULL;
  84.             }
  85.             next(P) = NULL;
  86.             break;
  87.         }
  88.         cur = next(cur);
  89.     }
  90. }
  91.  
  92. void Show(List L){
  93.     address p = first(L);
  94.     if(p == NULL){
  95.         cout << "Daftar produk kosong" << endl;
  96.         return;
  97.     }
  98.     while(p!= NULL){
  99.         cout << info(p);
  100.         p = next(p);
  101.         if(p!=NULL){
  102.             cout << ", ";
  103.         }
  104.     }
  105.     cout<<endl;
  106. }
  107.  
  108. address findInfo(List L,string name){
  109.     address cur = first(L);
  110.     while (cur != NULL) {
  111.         if(name == info(cur)){
  112.             return cur;
  113.         }
  114.         cur = next(cur);
  115.     }
  116.     return NULL;
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement