#include<iostream>
#include<string>
#include<stack>
#include<cstddef>
#include<cassert>
usingnamespace std;
typedef string ItemType;
class Stack {
public:
// Creates an empty stack.
Stack();
// Makes a "deep" copy of another stack, i.e. it copies
// all the values in the stack.
// Note that if this copy constructor is not included
// then C++ makes a simple default copy constructor
// that does a "shallow" copy, i.e. the default
// copy constructor only copies the head
// pointer (and not the stack elements).
Stack(const Stack& other);
// Completely destroys a stack.
~Stack();
// Pre:
// Post: returns true if this stack is empty, and
// false otherwise
bool empty() const;
// Pre:
// Post: x is put on the top of the stack
void push(const ItemType& x);
// Pre: stack is not empty
// Post: returns a copy of the item on top of the stack;
// does *not* remove the item from the stack
ItemType top() const;
// Pre: stack is not empty
// Post: deletes the top element of the stack
void remove_top();
// Pre: stack is not empty
// Post: removes the top item of the stack and returns it
ItemType pop();
private:
struct Node {
ItemType val;
Node* next;
Node(const ItemType& vv, Node* nn) : val(vv), next(nn) { }
Node(const ItemType& vv) : val(vv), next(0) { }
};
Node* head;
};
// Makes a "deep" copy of another stack.
// It's interesting to note that this is, by far, the most complicated
// function in Stack, and it's only needed to prevent Stacks from
// being copied incorrectly by the default copy constructor C++
// automatically creates.
Stack::Stack()
{
head = NULL;
}
Stack::Stack(const Stack& other)
{
if (other.empty())
{
head = NULL;
} else
{
Node* p = other.head; // points to current node on other
Node* tmp = new Node(p->val); // make a copy of the first node
head = tmp;
Node* tail = tmp; // points to last node of this list
while (p->next != NULL)
{
p = p->next;
tmp = new Node(p->val);
tail->next = tmp;
tail = tmp;
}
}
}
bool Stack::empty() const
{
return (head == NULL);
}
//void push(const ItemType& x)
//{
// ItemType *newNode;
//newNode = new ItemType;
// assert(newNode != NULL);
// newNode->val = x;
// newNode->next = top();
// top() = newNode;
//}//end push
//};
// Pre:
// Post: x is put on the top of the stack
void Stack::push(const ItemType& x)
{
Node* tmp = new Node(x, head);
head = tmp;
}
// Pre: stack is not empty
// Post: returns a copy of the item on top of the stack;
// does *not* remove the item from the stack
ItemType Stack::top() const {
if(!empty());
return head->val;
}
// Pre: stack is not empty
// Post: deletes the top element of the stack
void Stack::remove_top() {
if(!empty());
Node*temp = head;
head = head->next;
delete temp;
}
// Completely destroys a stack.
Stack::~Stack() {
while (!empty()) remove_top();
}
//Pre: stack is not empty
//Post: removes the top item of the stack and returns it
ItemType Stack::pop()
{
//assert(!empty());
ItemType result = top();
remove_top();
return result;
}
int main()
{
//cout<<"See Programming Exercise 1"<<endl;
Stack myStack;
//Stack yourStack;
myStack.push("summer");
myStack.push("automn");
myStack.push("winter");
myStack.push("fall");
myStack.push("spring");
while(!myStack.empty()) //didn't look... you should have an empty checker member somewhere
{
cout << myStack.pop() << endl;
}
Stack yourStack(myStack);
while(!yourStack.empty())
{
cout <<yourStack.pop() << endl;
}
return 0;
}
Here is the output:
1 2 3 4 5 6 7 8
spring
fall
winter
automn
summer
Process returned 0 (0x0) execution time : 0.060 s
Press any key to continue.
Help as to why yourStack does not print on the screen would be appreciated:
In member function 'ItemType Stack::top() const':
114:14: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
In member function 'void Stack::remove_top()':
121:14: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
Always use braces, even if there is only 1 statement. Do you see what happened?
When I fix those problems, then this happens:
In member function 'void Stack::remove_top()':
122:6: warning: unused variable 'temp' [-Wunused-variable]
124:9: error: 'temp' was not declared in this scope In member function 'ItemType Stack::top() const': 116:1: warning: control reaches end of non-void function [-Wreturn-type]