Advertisement
skb50bd

myStack[StackOperations]

Jun 13th, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. struct myStack {
  6.     int data = 0;
  7.     myStack *next = NULL;
  8. } *Head;
  9.  
  10. int counT() {
  11.     int c = 0;
  12.     for (myStack *Current = Head; Current != NULL; Current = Current -> next)
  13.         c++;
  14.     return c;
  15. }
  16.  
  17. void display() {
  18.     if (Head == NULL)
  19.         cout << "Stack is Empty, Nothing to Display.";
  20.     else
  21.         for (myStack *Current = Head; Current != NULL; Current = Current -> next)
  22.             cout << Current -> data << " ";
  23.     cout << endl << endl;
  24. }
  25.  
  26. void push(int num) {
  27.     myStack *temp = new myStack;
  28.     temp -> data = num;
  29.     temp -> next = Head;
  30.     Head = temp;
  31. }
  32.  
  33. void pop() {
  34.     if (Head == NULL)
  35.         cout << "Stack is Empty, Nothing to Pop." << endl;
  36.     else {
  37.         myStack *temp = Head;
  38.         Head = Head -> next;
  39.         delete temp;
  40.     }
  41.     cout << endl;
  42. }
  43.  
  44. void top() {
  45.     if (Head == NULL)
  46.         cout << "Stack is Empty, Nothing to Show.";
  47.     else
  48.         cout << Head -> data;
  49.     cout << endl << endl;
  50. }
  51.  
  52. int main() {
  53.     int choice, data;
  54.  
  55.     while (true) {
  56.         cout << "Stack Operations" << endl;
  57.         cout << "----------------" << endl;
  58.         cout << "1. Push" << endl;
  59.         cout << "2. Pop" << endl;
  60.         cout << "3. Top" << endl;
  61.         cout << "4. Display" << endl;
  62.         cout << "5. Size" << endl;
  63.         cout << "6. Exit" << endl;
  64.         cout << " --Choice: ";
  65.         cin >> choice;
  66.  
  67.         switch (choice) {
  68.             case 1: {
  69.                 cout << "Enter the Integer Value: ";
  70.                 cin >> data;
  71.                 cout << endl;
  72.                 push(data);
  73.                 break;
  74.             }
  75.             case 2: {
  76.                 pop();
  77.                 break;
  78.             }
  79.             case 3: {
  80.                 top();
  81.                 break;
  82.             }
  83.             case 4: {
  84.                 display();
  85.                 break;
  86.             }
  87.             case 5: {
  88.                 cout << "Size: " << counT() << endl << endl;
  89.                 break;
  90.             }
  91.             case 6:
  92.                 exit(0);
  93.             default:
  94.                 cout << "Invalid Choice, Try Again." << endl << endl;
  95.         }
  96.  
  97.     }
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement