Cypher121

Simple Linked List

Sep 23rd, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. class lol_list {
  6.     public:
  7.         class Node {
  8.             public:
  9.                 Node* prev;
  10.                 Node* next;
  11.        
  12.                 int value;
  13.        
  14.             Node(int value) {
  15.                 this->value = value;
  16.                 next = prev = nullptr;
  17.             }
  18.         }; 
  19.    
  20.         int length;
  21.         Node* head;
  22.         Node* tail;
  23.  
  24.         lol_list() {
  25.             length = 0;
  26.         }
  27.    
  28.         int pop() {
  29.             int val;
  30.             if (length > 0) {
  31.                 val = head->value;
  32.                 length--;
  33.                 if (length > 0) {
  34.                     head = head->next;
  35.                     delete head->prev;
  36.                     head->prev = nullptr;
  37.                 } else {
  38.                     delete head;
  39.                     head = tail = nullptr;
  40.                 }
  41.             } else {
  42.                 throw "u wot m8";
  43.             }
  44.             return val;
  45.         }
  46.        
  47.         void push(int val) {
  48.             if (length > 0) {
  49.                 tail->next = new Node(val);
  50.                 tail->next->prev = tail;
  51.                 tail = tail->next;
  52.             } else {
  53.                 tail = head = new Node(val);
  54.             }
  55.             length++;
  56.         }
  57.        
  58.         int getLength() {
  59.             return length;
  60.         }
  61. };
  62.  
  63. int main() {
  64.     lol_list list;
  65.     list.push(1);
  66.     list.push(8);
  67.     list.push(4);
  68.     cout << list.getLength() << endl;
  69.     cout << list.pop() << endl;
  70.     cout << list.getLength() << endl;
  71.     cout << list.pop() << endl;
  72.     cout << list.getLength() << endl;
  73.     cout << list.pop() << endl;
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment