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;
- }
- int StrToInt(string b)
- {
- int l, M;
- l = b.length();
- M = 0;
- for (int i = 0; i < l; i++)
- {
- M += (b[i] - '0') * pow(10, l - i - 1);
- }
- return M;
- }
- struct stack {
- int data;
- struct stack* next;
- };
- void s_push(stack** top, int D) {
- stack* node;
- node = new stack();
- node->data = D;
- if (top == NULL) {
- *top = node;
- }
- else
- {
- node->next = *top;
- *top = node;
- }
- }
- void s_print(stack* top) {
- stack* node = top;
- if (top == NULL)
- {
- cout << "The stack is empty \n";
- }
- else
- {
- cout << "The stack consists : \n";
- while (node) {
- cout << node->data << " ";
- node = node->next;
- }
- }
- }
- void s_delete_key(stack** top,int p) {
- if (p <= 0)
- {
- cout << "You`re idiot!!! The stack is empty\n";
- }
- else
- {
- stack* node = *top;
- *top = node->next;
- free(node);
- node->data = NULL;
- node->next = NULL;
- cout << "The top element is deleted \n";
- }
- }
- int main()
- {
- stack* top = NULL;
- char choice;
- string number;
- int num=0,p=0;
- do {
- metka:
- choice = menu();
- switch (choice)
- {
- case '1':
- cout << "Input an element :";
- cin >> number;
- for (auto c : number)
- {
- if (c >= '0' && c <= '9')
- {
- num = StrToInt(number);
- }
- else
- goto metka;
- }
- cout << endl;
- s_push(&top, num);
- cout << "The element is added \n";
- p++;
- break;
- case '2':
- s_delete_key(&top,p);
- p--;
- 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