Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct Node {
- int value;
- Node *next;
- Node():value(0),next(nullptr) {}
- };
- class Stack {
- Node *head;
- public:
- Stack() : head(nullptr) {}
- void push(int x) {
- Node *tmp = new Node;
- tmp->value = x;
- tmp->next = head;
- head = tmp;
- }
- int top() {
- checker();
- return head->value;
- }
- void pop() {
- checker();
- Node *tmp = head;
- head = head->next;
- delete tmp;
- }
- Stack (const Stack& other):
- {}
- ~Stack() {
- while (head != nullptr) {
- pop();
- }
- }
- private:
- void checker()
- {
- if (head==nullptr)
- {
- throw std::runtime_error(" Called on empty list");
- }
- }
- };
- int main()
- {
- Stack s;
- s.top();
- }
Advertisement
Add Comment
Please, Sign In to add comment