Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Stack.h"
- Stack::Stack(int size){ // constructor
- cout << "Constructing a stack...\n";
- stack = new int[size];
- top = 0;
- maxsize = size - 1;
- }
- Stack::Stack(const Stack &oldStack){ // copy constructor
- cout << "Copying the stack...\n";
- stack = new int[oldStack.capacity()];
- top = oldStack.top;
- maxsize = oldStack.maxsize;
- stack = oldStack.stack;
- }
- Stack::~Stack(){ // destructor
- delete stack;
- delete ⊤
- delete &maxsize;
- }
- void Stack::push(int val){ // push an int into a Stack
- if (top <= maxsize){
- cout << "pushing " << val << " to index " << top << "\n";
- stack[top] = val;
- ++top;
- }
- else{
- cout << "could not push; stack full\n";
- }
- }
- int Stack::pop(){ // pop an int from a Stack
- if (top != 0){
- //cout << stack[top-1];
- int val = stack[top-1]; //the value we're popping from the stack
- stack[top-1] = 0;
- --top;
- return val;
- }
- else{
- cout << "could not pop; stack empty\n";
- return -1;
- }
- }
- bool Stack::empty() const{ // is the Stack empty?
- if (top == 0)
- return true;
- else
- return false;
- }
- bool Stack::full() const{ // is the Stack full?
- if (top == maxsize)
- return true;
- else
- return false;
- }
- int Stack::capacity() const{ // capacity of the stack
- return maxsize + 1;
- }
- int Stack::size() const{ // current size of the stack
- return top;
- }
- ostream &operator <<(ostream &out, const Stack ¤tStack){
- //this loop will display each element on its own line.
- //each line will be preceded by the index corresponding to the element.
- for (int i = currentStack.size() - 1; i >= 0; --i)
- {
- out << "<" << i << "> " << currentStack.stack[i] << '\n';
- }
- return out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement