Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- struct node
- {
- int value;
- node* previous;
- node(int value, node* previous)
- {
- this->value = value;
- this->previous = previous;
- }
- };
- class Stack
- {
- private:
- node* top = nullptr;
- public:
- void push(int value);
- void pop();
- int peek()const;
- void print();
- bool isEmpty()const;
- Stack();
- Stack(const Stack& s);
- ~Stack();
- };
- #include "Stack.h"
- #include <iostream>
- void Stack::push(int value)
- {
- if (top == nullptr)
- {
- top = new node(value, nullptr);
- }
- else
- {
- node* newNode = new node(value, top);
- top = newNode;
- }
- }
- void Stack::pop()
- {
- if (!isEmpty())
- {
- node* temp = top->previous;
- delete top;
- top = temp;
- }
- }
- int Stack::peek() const
- {
- if (!isEmpty())
- {
- return top->value;
- }
- // TODO: insert return statement here
- }
- void Stack::print()
- {
- if (!isEmpty())
- {
- node* temp = top;
- while (temp != nullptr)
- {
- std::cout << temp->value << std::endl;
- temp = temp->previous;
- }
- }
- else
- {
- std::cout << "Stack is empty." << std::endl;
- }
- }
- bool Stack::isEmpty()const
- {
- if (top == nullptr)return true;
- return false;
- }
- Stack::Stack()
- {
- //
- top = nullptr;
- }
- Stack::Stack(const Stack & s)
- {
- }
- Stack::~Stack()
- {
- while (!isEmpty())pop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement