Advertisement
ChaeYuriya

Week 6 : DLL.cpp

Nov 3rd, 2024
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. #include <iostream>
  2. #include "DLL.h"
  3.  
  4. void createNewList(List &L){
  5.     first(L) = NULL;
  6.     last(L) = NULL;
  7. }
  8.  
  9. address createElemen(infotype dataBaru){
  10.     address P = new ElmtMsk;
  11.     info(P) = dataBaru;
  12.     next(P) = NULL;
  13.     return P;
  14. }
  15.  
  16. void insertLast(List &L, address p){
  17.     address x;
  18.     if (first(L) == NULL){
  19.         first(L) = p;
  20.         last(L) = p;
  21.     } else {
  22.         next(last(L)) = p;
  23.         prev(p) = last(L);
  24.         last(L) = p;
  25.     }
  26.  
  27. }
  28.  
  29. void insertAfter(List &L, address &Prec,address &P){
  30.     next(P) = next(Prec);
  31.     prev(next(Prec)) = P;
  32.     next(Prec) = P;
  33.     prev(P) = Prec;
  34. }
  35.  
  36. void deleteFirst(List &L, address &p){
  37.     p = first(L);
  38.     if(next(first(L)) == NULL){
  39.         first(L) = NULL;
  40.         last(L) = NULL;
  41.     }else{
  42.         first(L) = next(first(L));
  43.         prev(first(L)) = NULL;
  44.         next(p) = NULL;
  45.     }
  46. }
  47.  
  48. void deleteAfter(List &L, address &prec, address &p){
  49.     p = next(prec);
  50.     if (next(p) == NULL){
  51.         next(prec) = NULL;
  52.     } else {
  53.         next(prec) = next(p);
  54.         next(p) = NULL;
  55.     }
  56. }
  57.  
  58.  
  59. void deleteLast(List &L, address &p){
  60.     p = last(L);
  61.     if(prev(p) != NULL){
  62.         address lastest = prev(p);
  63.         last(L) = lastest;
  64.         next(lastest) = NULL;
  65.     }else{
  66.         last(L) = NULL;
  67.         first(L) = NULL;
  68.     }
  69.     prev(p) = NULL;
  70. }
  71.  
  72. void printList(List L){
  73.     address p = first(L);
  74.     while(p!=NULL){
  75.         cout << info(p).title <<"||"<<info(p).url <<"||"<<info(p).timestamp <<"||"<<info(p).visitcount<< endl;
  76.         p = next(p);
  77.     }
  78. }
  79.  
  80. void insertFirst(List &L,address P){
  81.     if(first(L) == NULL){
  82.         first(L) = P;
  83.         last(L) = P;
  84.         return;
  85.     }
  86.     prev(first(L)) = P;
  87.     next(P) = first(L);
  88.     first(L) = P;
  89. }
  90.  
  91. void insertAscending(List &L,infotype dataBaru){
  92.     address P = first(L);
  93.     if(P == NULL){
  94.         insertFirst(L,createElemen(dataBaru));
  95.     }else{
  96.         address datanew = createElemen(dataBaru);
  97.         while(P != NULL){
  98.             if(info(P).visitcount > info(datanew).visitcount){
  99.                 if(prev(P) == NULL){
  100.                     insertFirst(L,datanew);
  101.                 }else{
  102.                     address lastBefore = prev(P);
  103.                     insertAfter(L,lastBefore,datanew);
  104.                 }
  105.                 break;
  106.             }else if(next(P) == NULL){
  107.                 insertLast(L,datanew);
  108.                 break;
  109.             }
  110.             P = next(P);
  111.         }
  112.     }
  113. }
  114.  
  115. bool findElemen(List L,string X){
  116.     address p = first(L);
  117.     while(p!=NULL){
  118.         if(info(p).title == X){
  119.             return true;
  120.         }
  121.         p = next(p);
  122.     }
  123.     return false;
  124. }
  125.  
  126.  
  127. int moreThanOnce(List L){
  128.     int total = 0;
  129.     address p = first(L);
  130.     while(p!=NULL){
  131.         if(info(p).visitcount > 0){
  132.             total++;
  133.         }
  134.         p = next(p);
  135.     }
  136.     return total;
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement