C++, Popped off

Hello, how to allow the user to enter how many items to be popped off? Please check my codes.

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
#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 choice, data;

    while(1)
    {
        cout << "1. Push to stack" << endl;
        cout << "2. Pop from stack" << endl;
        cout << "3. Display stack" << endl;
        cout << "4. Exit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;

        if(choice == 1)
        {
            cout << "Enter data to push: ";
            cin >> data;
            stack.push(data);
        }
        else if(choice == 2)
        {
            stack.pop();
        }
        else if(choice == 3)
        {
            stack.display();
        }
        else if(choice == 4)
        {
            break;
        }
        else
        {
            cout << "Invalid choice." << endl;
        }
    }
    return 0;
}

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

struct Node {
	int data {};
	Node* next {};

	Node() {}
	Node(int d) : data(d) {}
};

class Stack {
	Node* top {};

public:
        Stack() = default;
        Stack(const Stack&) = delete;
	Stack& operator=(const Stack&) = delete;

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

			delete top;
			top = tmp;
		}
	}

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

		node->next = top;
		top = node;
	}

	void pop() {
		if (!top)
			std::cout << "Stack is empty!\n";
		else {
			const auto temp { top };

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

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

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

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

	while (choice != 4) {
		std::cout << "1. Push to stack\n"
			<< "2. Pop from stack\n"
			<< "3. Display stack\n"
			<< "4. Exit\n"
			<< "Enter your choice: ";

		std::cin >> choice;

		switch (choice) {
			case 1:
				{
					int data {};

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


			case 2:
				{
					int no {};

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

					for (; no--; stack.pop());
				}
				break;

			case 3:
				stack.display();
				break;

			case 4:
				break;

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

Last edited on
Can you explain this code? I'm not familiar with it.
 
for ( ; no--; stack.pop() );
That is a standard for loop. What part don't you understand? There is no initialisation statement, the condition statement is false when no is decremented to 0 and for each loop iteration it executes stack.pop().
Oh thanks i got it.
Topic archived. No new replies allowed.