SHARE
TWEET

DLL

a guest Aug 23rd, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node{
  6.     int data;
  7.     Node* next{};
  8.     Node* prev{};
  9. };
  10.  
  11. class DLL{
  12.     private:
  13.     Node* root{};
  14.     Node* tail;
  15.     void clear_helper(Node* &curr);
  16.     public:
  17.     void clear();
  18.     void add(int zahl);
  19.     void push_front(int zahl);
  20.     void push_back(int zahl);
  21.     int pop_front();
  22.     int pop_back();
  23.     DLL(){};
  24.     ~DLL(){ clear(); };
  25. };
  26.  
  27.  
  28. void DLL::clear_helper(Node* &curr) { //CLEAR_HELPER
  29.     if (curr -> next != nullptr) {
  30.         clear_helper(curr -> next);
  31.     } else {
  32.         delete curr;
  33.         curr = nullptr;
  34.     }
  35. }
  36.  
  37. void DLL::clear() { //CLEAR FUNKTION
  38.     if (root != nullptr) {
  39.         clear_helper(root);
  40.         delete tail;
  41.         tail = nullptr;
  42.     }
  43. }
  44.  
  45.  
  46. void DLL::push_front(int zahl){
  47.     if (root == nullptr) {
  48.         root = new Node;
  49.         root -> data = zahl;
  50.         tail = root;
  51.     } else {
  52.         Node* tmp = new Node;
  53.         tmp -> data = zahl;
  54.         tmp -> next = root;
  55.         root -> prev = tmp;
  56.         root = tmp;
  57.     }
  58. }
  59.  
  60. void DLL::push_back(int zahl) {
  61.     if (root == nullptr) {
  62.         root = new Node;
  63.         root -> data = zahl;
  64.         tail = root;
  65.     } else {
  66.         Node *tmp = new Node;
  67.         tmp -> data = zahl;
  68.         tmp -> prev = tail;
  69.         tail -> next = tmp;
  70.         tail = tmp;
  71.     }
  72. }
  73.  
  74. int DLL::pop_front() {
  75.     if (root == nullptr) {
  76.         throw logic_error("Liste leer");
  77.     } else {
  78.         Node* tmp = root;
  79.         int ret = tmp -> data;
  80.         root = root -> next;
  81.  
  82.         if (root == nullptr) {          //if last element was popped
  83.             tail = nullptr;
  84.         } else {
  85.             root -> prev = nullptr;     //prev of first element has to be NULL
  86.         }
  87.         delete tmp;
  88.  
  89.         return ret;
  90.     }
  91. }
  92.  
  93. int DLL::pop_back() {
  94.     if (root == nullptr) {
  95.         throw logic_error("List already empty!");
  96.     } else {
  97.         int ret = tail -> data;
  98.  
  99.         if (root != nullptr && tail == root && root -> next == nullptr) {   //only one element in list
  100.             delete tail;
  101.             root = nullptr;
  102.             tail = nullptr;
  103.         } else {
  104.             Node* tmp = tail;
  105.             tail -> prev -> next = nullptr;
  106.             tail = tail -> prev;
  107.             delete tmp;
  108.         }
  109.  
  110.         return ret;
  111.     }  
  112. }    
  113.  
  114. void DLL::add(int zahl) {
  115.     if(root==nullptr){
  116.         root = new Node;
  117.         root->data = zahl;
  118.         tail = root;
  119.     }
  120.     else{
  121.         Node *curr = root;
  122.  
  123.         while (curr != nullptr && curr -> data > zahl) {
  124.             curr = curr -> next;
  125.         }
  126.  
  127.         if (curr -> prev == nullptr) {
  128.             Node *tmp = new Node;
  129.             tmp -> data = zahl;
  130.             root = tmp;
  131.             tmp -> next = curr;
  132.             curr -> prev = tmp;
  133.         } else if (curr == nullptr){
  134.             Node *tmp = new Node;
  135.             tmp -> data = zahl;
  136.             tail -> next = tmp;
  137.             tmp -> prev = tail;
  138.         } else {
  139.             Node *tmp = new Node;
  140.             tmp -> data = zahl;
  141.  
  142.             tmp -> prev = curr -> prev;
  143.             tmp -> next = curr;
  144.             curr -> prev -> next = tmp;
  145.             curr -> next -> prev = tmp;
  146.         }
  147.     }
  148. }
  149.  
  150. int main(){
  151.     DLL y;
  152.     y.push_back(2);
  153.     y.push_back(4);
  154.     y.pop_back();
  155.     y.pop_front();
  156. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top