Program does not print the elements of the copied stack

The original stack (myStack) prints elements well on the screen.
The copied stack (yourStack) prints nothing on the screen>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  #include<iostream>
#include<string>
#include<stack>
#include<cstddef>
#include<cassert>

using namespace 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:
You just copied an empty stack!

You know ... the one you emptied by pop'ping whilst printing it out!
Last edited on
The compiler says:

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]

This is a similar problem.



Also don't use NULL , use nullptr instead.

Oh yeah, what lastchance said *facepalm* :+D
Last edited on
Thank you very much again good people.
Your help is greatly valued.
I simply re-pushed all the elements from myStack and the program worked fine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
}
    myStack.push("summer");
	myStack.push("automn");
	myStack.push("winter");
	myStack.push("fall");
	myStack.push("spring");

    Stack yourStack(myStack);
    cout << endl << "Now printing yourStack" << endl;
    while(!yourStack.empty())
    {
        cout << yourStack.pop() << endl;
    }
I simply re-pushed all the elements from myStack and the program worked fine


But you still need to fix the things I mentioned.
Topic archived. No new replies allowed.