Advertisement
ChaeYuriya

DLL.cpp

Nov 2nd, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. #include <iostream>
  2. #include "DLL.h"
  3. #define first(L) L.first
  4. #define next(P) P->next
  5. #define prev(P) P->prev
  6. #define info(P) P->info
  7. #define judul(P) P->judul
  8.  
  9. using namespace std;
  10.  
  11. bool isEmpty(List L){
  12.     if(first(L) == NULL)
  13.         return false;
  14.  
  15.     return true;
  16. }
  17.  
  18. void createList(List &L){
  19.     first(L) = NULL;
  20. }
  21.  
  22. void createNewElement(infotype X,address &P){
  23.     info(P) = X;
  24.     next(P) = NULL;
  25.     prev(P) = NULL;
  26. }
  27.  
  28. void createNewSong(infotype ID,string X,address &P){
  29.     info(P) = ID;
  30.     judul(P) = X;
  31.     next(P) = NULL;
  32.     prev(P) = NULL;
  33. }
  34.  
  35. void insertFirst(List &L, address P){
  36.     next(P) = first(L);
  37.     prev(P) = NULL;
  38.     first(L) = P;
  39. }
  40.  
  41. void inserAfter(List &L, address &Target,address P){
  42.     address cur = first(L);
  43.     while(next(cur)!= NULL){
  44.         if(cur == Target){
  45.             address nextTarget = next(cur);
  46.             next(cur) = P;
  47.  
  48.             prev(P)= cur;
  49.             next(P) = nextTarget;
  50.  
  51.             if(nextTarget != NULL){
  52.                 prev(nextTarget) = P;
  53.             }
  54.             break;
  55.         }
  56.         cur = next(cur);
  57.     }
  58. }
  59.  
  60. void insertLast(List &L, address P){
  61.     if(first(L) == NULL){
  62.         insertFirst(L,P);
  63.     }else{
  64.         address cur = first(L);
  65.         while(next(cur)!= NULL){
  66.             cur = next(cur);
  67.         }
  68.         next(cur) = P;
  69.         prev(P) = cur;
  70.     }
  71. }
  72.  
  73. void deleteFirst(List &L, address &P){
  74.     P = first(L);
  75.     address currentFirst = next(P);
  76.  
  77.     prev(currentFirst)= NULL;
  78.     next(P) = NULL;
  79.     prev(P) = NULL;
  80.  
  81.     first(L) = currentFirst;
  82. }
  83.  
  84. void deleteAfter(List &L, address &Target,address &P){
  85.     address cur = first(L);
  86.     while(next(cur)!= NULL){
  87.         if(cur == Target){
  88.             P = next(cur);
  89.             address nextVal = next(P);
  90.  
  91.             if(nextVal != NULL){
  92.                 prev(nextVal) = prev(P);
  93.                 next(cur) = next(P);
  94.             }else{
  95.                 next(cur) = NULL;
  96.             }
  97.             next(P) = NULL;
  98.             prev(P) = NULL;
  99.             break;
  100.         }
  101.         cur = next(cur);
  102.     }
  103. }
  104.  
  105. void deleteLast(List &L, address &P){
  106.     address cur = first(L);
  107.     while(next(cur)!= NULL){
  108.         cur = next(cur);
  109.     }
  110.     address beforeLast = prev(cur);
  111.     next(beforeLast) = NULL;
  112.     prev(cur) = NULL;
  113. }
  114.  
  115. void concat(List L1, List L2, List &L3) {
  116.     createList(L3);
  117.  
  118.     address cur = first(L1);
  119.     while (cur != NULL) {
  120.         address newElem = new elmList;
  121.         info(newElem) = info(cur);
  122.         next(newElem) = NULL;
  123.         prev(newElem) = NULL;
  124.  
  125.         insertLast(L3, newElem);
  126.         cur = next(cur);
  127.     }
  128.  
  129.     cur = first(L2);
  130.     while (cur != NULL) {
  131.         address newElem = new elmList;
  132.         info(newElem) = info(cur);
  133.         next(newElem) = NULL;
  134.         prev(newElem) = NULL;
  135.  
  136.         insertLast(L3, newElem);
  137.         cur = next(cur);
  138.     }
  139. }
  140.  
  141. address getSongAddress(string judul,List L){
  142.     address cur = first(L);
  143.     while (cur != NULL) {
  144.         if(judul == judul(cur)){
  145.             return cur;
  146.         }
  147.         cur = next(cur);
  148.     }
  149.     return NULL;
  150. }
  151.  
  152. void removeLagu(string judul,List &L){
  153.     address cur = getSongAddress(judul,L);
  154.     if(cur != NULL){
  155.         address prevVal = prev(cur);
  156.         address nextVal = next(cur);
  157.  
  158.         next(cur) = NULL;
  159.         prev(cur) = NULL;
  160.  
  161.         if(prevVal != NULL){
  162.             next(prevVal) = nextVal;
  163.         }
  164.         if(nextVal != NULL){
  165.             prev(nextVal) = prevVal;
  166.         }
  167.     }
  168. }
  169.  
  170. void printInfo(List L){
  171.     address p = first(L);
  172.     while(p!= NULL){
  173.         cout << "==> " << info(p) <<"=" <<judul(p)  << "<== "<<prev(p)<< "<>" << next(p)<< endl;
  174.         p = next(p);
  175.     }
  176.     cout<<endl;
  177. }
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement