How to display stack?

Nov 12, 2013 at 12:45am
Hello programmers. I am learning how to make stacks. The instructor wants me to display the stack after you enter the elements or pop them out. All the examples I have seen so far from the book and the internet haven't shown me how to display them. So, not really sure how to do this. Here's the code I got so far from the header file.

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
#ifndef INTSTACK_H
#define INTSTACK_H
#include <iostream>
using namespace std;

class IntStack
{
private:
	int *stackArray;
	int stackSize;
	int top1;
	
public:
	IntStack(int);

	IntStack(const IntStack &);

	~IntStack();

	bool isEmpty();    // returns true if stack is empty
	bool isFull();     // return true if stack is full
	void push(int n);  // item n is pushed onto stack, throw exception on full stack.
	void pop(int &);   // removes top item from stack, throw exception on empty stack.
	int top();         // returns top of stack, throw exception on empty stack.
	void printStack(); // prints stack, stack is unchanged
};

IntStack::IntStack(int size)
{
	stackArray = new int[size];
	stackSize = size;
	top1 = -1;
}

IntStack::IntStack(const IntStack &obj)
{
	if (obj.stackSize > 0)
		stackArray = new int[obj.stackSize];
	else
		stackArray = NULL;

	stackSize = obj.stackSize;

	for (int count = 0; count < stackSize; count++)
		stackArray[count] = obj.stackArray[count];

	top1 = obj.top1;
}

IntStack::~IntStack()
{
	delete [] stackArray;
}

void IntStack::push(int num)
{
	if (isFull())
	{
		cout << "The stack is full.\n";
	}
	else
	{
		top1++;
		stackArray[top1] = num;
	}
}

void IntStack::pop(int &num)
{
	if (isEmpty())
	{
		cout << "The stack is empty.\n";
	}
	else
	{
		num = stackArray[top1];
		top1--;
	}
}

bool IntStack::isFull()
{
	bool status;
	
	if (top1 == stackSize - 1)
		status = true;
	else
		status = false;

	return status;
}

bool IntStack::isEmpty()
{
	bool status;

	if (top1 = -1)
		status = true;
	else
		status = false;

	return status;
}
#endif 


As you can see, I have the operation called printStack(). The question is how do you code this operation?

I was thinking about using the for loop and somehow code it that way. Not sure how to do this.
Nov 12, 2013 at 12:54am
It's just like what you did on lines 44 and 45, except you print the elements instead of assigning values to them.
Nov 12, 2013 at 1:43am
This is my program output. Why is it doing that?



What do you want to do?
1 - Push an item onto the stack
2 - Pop an item off the stack
3 - Quit the program
Enter your choice: 1

Enter an item: 1
11-842150451-842150451-842150451-842150451-842150451-842150451-842150451-8421504
51-842150451-842150451-842150451-842150451-842150451-842150451
What do you want to do?
1 - Push an item onto the stack
2 - Pop an item off the stack
3 - Quit the program
Enter your choice: 1

Enter an item: 2
212-842150451-842150451-842150451-842150451-842150451-842150451-842150451-842150
451-842150451-842150451-842150451-842150451-842150451
What do you want to do?
1 - Push an item onto the stack
2 - Pop an item off the stack
3 - Quit the program
Enter your choice: 1

Enter an item: 1
1121-842150451-842150451-842150451-842150451-842150451-842150451-842150451-84215
0451-842150451-842150451-842150451-842150451
What do you want to do?
1 - Push an item onto the stack
2 - Pop an item off the stack
3 - Quit the program
Enter your choice: 2
The stack is empty.
121-842150451-842150451-842150451-842150451-842150451-842150451-842150451-842150
451-842150451-842150451-842150451-842150451
What do you want to do?
1 - Push an item onto the stack
2 - Pop an item off the stack
3 - Quit the program
Enter your choice: 1

Enter an item: 3
3321-842150451-842150451-842150451-842150451-842150451-842150451-842150451-84215
0451-842150451-842150451-842150451-842150451
What do you want to do?
1 - Push an item onto the stack
2 - Pop an item off the stack
3 - Quit the program
Enter your choice: 3
Press any key to continue . . .


Here's my code for header file.

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
#ifndef INTSTACK_H
#define INTSTACK_H
#include <iostream>
using namespace std;

class IntStack
{
private:
	int *stackArray;
	int stackSize;
	int top1;
	
public:
	IntStack(int);

	IntStack(const IntStack &);

	~IntStack();

	bool isEmpty();    // returns true if stack is empty
	bool isFull();     // return true if stack is full
	void push(int n);  // item n is pushed onto stack, throw exception on full stack.
	void pop(int &);   // removes top item from stack, throw exception on empty stack.
	int top();         // returns top of stack, throw exception on empty stack.
	void printStack(); // prints stack, stack is unchanged
};



IntStack::IntStack(int size)
{
	stackArray = new int[size];
	stackSize = size;
	top1 = -1;
}

IntStack::IntStack(const IntStack &obj)
{
	if (obj.stackSize > 0)
		stackArray = new int[obj.stackSize];
	else
		stackArray = NULL;

	stackSize = obj.stackSize;

	for (int count = 0; count < stackSize; count++)
		stackArray[count] = obj.stackArray[count];

	top1 = obj.top1;
}

IntStack::~IntStack()
{
	delete [] stackArray;
}

void IntStack::push(int num)
{
	if (isFull())
	{
		cout << "The stack is full.\n";
	}
	else
	{
		top1++;
		stackArray[top1] = num;
	}
}

void IntStack::pop(int &num)
{
	if (isEmpty())
	{
		cout << "The stack is empty.\n";
	}
	else
	{
		num = stackArray[top1];
		top1--;
	}
}

bool IntStack::isFull()
{
	bool status;
	
	if (top1 == stackSize - 1)
		status = true;
	else
		status = false;

	return status;
}

bool IntStack::isEmpty()
{
	bool status;

	if (top1 = -1)
		status = true;
	else
		status = false;

	return status;
}

void IntStack::printStack()
{
	for (int count = 0; count < stackSize; count++)
		cout << stackArray[count];
}
#endif 


This one is the main file.

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
#include <iostream>
#include <string>
#include "IntStack.h"
using namespace std;

int item;

const int PUSH_CHOICE = 1,
		  POP_CHOICE = 2,
		  QUIT_CHOICE = 3;

void menu(int &);

int main()
{
	IntStack stack(15);

	int choice;

	do
	{
		menu(choice);

		if (choice != QUIT_CHOICE)
		{
			switch (choice)
			{
			case PUSH_CHOICE:
				 cout << "\nEnter an item: ";
				 cin >> item;
				 cout << item;
				 stack.push(item);
				 break;
			case POP_CHOICE:
				 stack.pop(item);
			}
			stack.printStack();
		}
	} while (choice != QUIT_CHOICE);


	system("pause");
	return 0;
}

void menu(int &choice)
{
		cout << "\nWhat do you want to do?\n"
			 << PUSH_CHOICE
			 << " - Push an item onto the stack\n"
			 << POP_CHOICE
			 << " - Pop an item off the stack\n"
			 << QUIT_CHOICE
			 << " - Quit the program\n"
			 << "Enter your choice: ";
		cin >> choice;

		while (choice < PUSH_CHOICE || choice > QUIT_CHOICE)
		{
			cout << "Enter a valid choice: ";
			cin >> choice;
		}
}
Nov 12, 2013 at 2:17am
closed account (D80DSL3A)
What is the top1 variable for?
Why line 31 in main() function?
Last edited on Nov 12, 2013 at 2:18am
Nov 12, 2013 at 5:06am
Oops. I don't think I wanted to put that there. What I had in mind was to display the stack but I am supposed to do that with printStack(). Taking that out.

top1 is the top of the stack.

The instructor specified that I need top() operation so it wouldn't let me to use "top" variable. So, instead, I put the 1 at the end so that way it doesn't act like it's trying to use the top() operation. Otherwise, I would get an error.

EDIT: Took "cout << item" out and still have the same results. Help!

I don't think I am properly coding the printStack() operation correctly. I think that is the source of the problem.
Last edited on Nov 12, 2013 at 5:18am
Nov 12, 2013 at 6:28am
closed account (D80DSL3A)
Good reply, but I asked about top1 because it's the key to the output problem. You are outputting all stackSize elements when only top1+1 elements should be displayed.
Topic archived. No new replies allowed.