Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- char menu()
- {
- char choice;
- cout << "Menu \n";
- cout << "1. Add an element \n";
- cout << "2. Remove an element \n";
- cout << "3. Show the stack \n";
- cout << "4. Exit\n";
- cin >> choice;
- return choice;
- }
- struct stack {
- int data;
- struct stack* next;
- };
- void s_push(stack** top, int D) {
- stack* q;
- q = new stack();
- q->data = D;
- if (top == NULL) {
- *top = q;
- }
- else
- {
- q->next = *top;
- *top = q;
- }
- }
- void s_print(stack* top) {
- stack* q = top;
- if (top == NULL)
- {
- cout << "The stack is empty \n";
- }
- else
- {
- cout << "The stack consists : \n";
- while (q) {
- cout << q->data << " ";
- q = q->next;
- }
- }
- }
- void s_delete_key(stack** top) {
- stack* q = *top;
- *top = q->next;
- free(q);
- q->data = NULL;
- q->next = NULL;
- }
- int main()
- {
- stack* top = NULL;
- char choice;
- int number;
- do {
- choice = menu();
- switch (choice)
- {
- case '1':
- cout << "Input an element :";
- cin >> number;
- cout << endl;
- s_push(&top, number);
- break;
- case '2':
- s_delete_key(&top);
- cout << "The top element is deleted \n";
- break;
- case '3':
- s_print(top);
- cout << endl;
- break;
- default: cout << "System exit\n";
- }
- } while (choice != '4');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement