Stack

Please help me, lets assume that we have an empty stack in the program when executed. When I pop 2 or more it displays the "Stack is empty" a few times. How to set it to display or cout the "Stack is empty" only once? When I pop more than one it displays the stack is empty a few times based on my input number of how many I want to popped off.

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

class Node
{
    public:
        int data;
        Node* next;

};

class Stack
{
    public:
        Node* top;
        Stack()
        {
            top = NULL;
        }


void push(int data)
{
    Node* node = new Node();
    node -> data = data;
    node -> next = top;
    top = node;
}

void pop()
{
    if(top==NULL)
    {
        cout << "Stack is empty!" << endl;
    }
    else
    {
        Node* temp = top;
        top = top -> next;
        delete temp;
    }
}

void display()
{
    if(top==NULL)
    {
        cout << "Stack is empty!" << endl;
    }
    else
    {
        Node* temp = top;
        while(temp!=NULL)
        {
            cout << temp -> data << " ";
            temp = temp -> next;
        }
        cout << endl;
    }
}
};

int main()
{
    Stack stack;

    int data, no;
    char choice;

    while(1)
    {
        cout << "a. Push to stack" << endl;
        cout << "b. Pop from stack" << endl;
        cout << "c. Display stack" << endl;
        cout << "d. Exit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;

        if(choice == 'a')
        {
            cout << "Enter data to push: ";
            cin >> data;
            stack.push(data);
        }
        else if(choice == 'b')
        {
            cout << "How many items to pop: ";
			cin >> no;

            for (; no--; stack.pop());
        }
        else if(choice == 'c')
        {
            stack.display();
        }
        else if(choice == 'd')
        {
            break;
        }
        else
        {
            cout << "Invalid choice." << endl;
        }
    }
    return 0;
}

  
Data structures (stacks, linked lists, hash tables, etc.) are not normally responsible for printing anything. Instead it is the code that uses them that do the printing. I would suggest you do the same. Instead of printing the stack is empty from inside the pop() function you could check if the stack has become empty in main() before calling pop(), and if it has you print the message and abort the pop loop.
Last edited on
Thanks I got it! I should've remove the display in pop function.
Your Stack needs also a function to check if it is empty.

Example: How std::stack is used: https://cplusplus.com/reference/stack/stack/empty/
There's other issues with this code. Consider:

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
#include <iostream>

class Node {
public:
	int data {};
	Node* next {};

	Node(int d, Node* n = nullptr) : data(d), next(n) {}
};

class Stack {
public:
	Stack() {}

	Stack(const Stack&) = delete;
	Stack& operator=(const Stack&) = delete;

	~Stack() {
		while (top) {
			const auto temp { top->next };

			delete top;
			top = temp;
		}
	}

	void push(int data) {
		const auto node { new Node(data, top) };

		top = node;
	}

	bool pop() {
		if (!isEmpty()) {
			const auto temp { top };

			top = top->next;
			delete temp;
			return true;
		}

		return false;
	}

	bool isEmpty() const {
		return top == nullptr;
	}

	void display() const {
		if (isEmpty())
			std::cout << "Stack is empty!\n";
		else {
			for (auto temp { top }; temp; temp = temp->next)
				std::cout << temp->data  << " ";

			std::cout << '\n';
		}
	}

private:
	Node* top {};
};

int main() {
	Stack stack;
	char choice {};

	while (choice != 'd') {
		std::cout << "a. Push to stack\n";
		std::cout << "b. Pop from stack\n";
		std::cout << "c. Display stack\n";
		std::cout << "d. Exit\n";

		std::cout << "Enter your choice: ";
		std::cin >> choice;

		switch (choice) {
			case 'a':
				{
					int data {};

					std::cout << "Enter data to push: ";
					std::cin >> data;
					stack.push(data);
				}
				break;

			case 'b':
				{
					int no {};

					std::cout << "How many items to pop: ";
					std::cin >> no;

					while (no--)
						if (!stack.pop()) {
							std::cout << "Stack empty!\n";
							break;
						}
				}
				break;

				case 'c':
					stack.display();
					break;

				case 'd':
					break;

				default:
					std::cout << "Invalid choice.\n";
					break;
		}
	}
}

One more thing ... in the public interface of the Stack:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Stack {
public:
	Stack() {}
	Stack(const Stack&) = delete;
	Stack& operator=(const Stack&) = delete;
	~Stack();
	void push(int);
	bool pop();
	bool isEmpty() const:
	void display() const;
private:
	// guts
};

I can push numbers into the stack, but I can't get any back.

Sure, I can display() but as user of Stack I get nothing. Whatever std::cout does is a side-effect that does not benefit the program.
In the best tradition - 'We'll leave that as an exercise for the reader'. :)
Registered users can post here. Sign in or register to post.