Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- class lol_list {
- public:
- class Node {
- public:
- Node* prev;
- Node* next;
- int value;
- Node(int value) {
- this->value = value;
- next = prev = nullptr;
- }
- };
- int length;
- Node* head;
- Node* tail;
- lol_list() {
- length = 0;
- }
- int pop() {
- int val;
- if (length > 0) {
- val = head->value;
- length--;
- if (length > 0) {
- head = head->next;
- delete head->prev;
- head->prev = nullptr;
- } else {
- delete head;
- head = tail = nullptr;
- }
- } else {
- throw "u wot m8";
- }
- return val;
- }
- void push(int val) {
- if (length > 0) {
- tail->next = new Node(val);
- tail->next->prev = tail;
- tail = tail->next;
- } else {
- tail = head = new Node(val);
- }
- length++;
- }
- int getLength() {
- return length;
- }
- };
- int main() {
- lol_list list;
- list.push(1);
- list.push(8);
- list.push(4);
- cout << list.getLength() << endl;
- cout << list.pop() << endl;
- cout << list.getLength() << endl;
- cout << list.pop() << endl;
- cout << list.getLength() << endl;
- cout << list.pop() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment