---
# System prepended metadata

title: 10998 - Stack
tags: [class]

---

# 10998 - Stack
>author: Utin
###### tags: `class`
---

## Brief
See the code below

## Solution 0

```cpp=
#include <iostream>
#include "function.h"

List_stack::List_stack() : head(NULL), tail(NULL) {}

List_stack::~List_stack() {
    ListNode* curr = head;
    while (curr) {
        ListNode* trash = curr;
        curr = curr->nextPtr;
        delete trash;
    }
}

void List_stack::push(const int& data) {
    ListNode* new_node = new ListNode(data);
    if (!tail) head = tail = new_node;
    else {
        head->prevPtr = new_node;
        new_node->nextPtr = head;
        head = new_node;
    }
}

void List_stack::pop() {
    if (tail) {
        if (head == tail) {
            delete head;
            head = tail = NULL;
        }
        else {
            ListNode* trash = head;
            head = head->nextPtr;
            delete trash;
        }
    }
}

void List_stack::print() {
    ListNode* curr = head;
    while (curr) {
        std::cout << curr->data;
        if (curr->nextPtr) std::cout << " ";
        curr = curr->nextPtr;
    }
}

// Utin
```

## Reference