myStack [user defined Stack Operations]

#include <iostream> #include <cstdlib> using namespace std; struct myStack { int data = 0; myStack *next = NULL; } *Head; int counT() { int c = 0; for (myStack *Current = Head; Current != NULL; Current = Current -> next) c++; return c; } void display() { if (Head == NULL) cout << "Stack is Empty, Nothing to Display."; else for (myStack *Current = Head; Current != NULL; Current = Current -> next) cout << Current -> data << " "; cout << endl << endl; } void push(int num) { myStack *temp = new myStack; temp -> data = num; temp -> next = Head; Head = temp; } void pop() { if (Head == NULL) cout << "Stack is Empty, Nothing to Pop." << endl; else { myStack *temp = Head; Head = Head -> next; delete temp; } cout << endl; } void top() { if (Head == NULL) cout << "Stack is Empty, Nothing to Show."; else cout << Head -> data; cout << endl << endl; } int main() { int choice, data; while (true) { cout << "Stack Operations" << endl; cout << "----------------" << endl; cout << "1. Push" << endl; cout << "2. Pop" << endl; cout << "3. Top" << endl; cout << "4. Display" << endl; cout << "5. Size" << endl; cout << "6. Exit" << endl; cout << " --Choice: "; cin >> choice; switch (choice) { case 1: { cout << "Enter the Integer Value: "; cin >> data; cout << endl; push(data); break; } case 2: { pop(); break; } case 3: { top(); break; } case 4: { display(); break; } case 5: { cout << "Size: " << counT() << endl << endl; break; } case 6: exit(0); default: cout << "Invalid Choice, Try Again." << endl << endl; } } return 0; }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.