Advertisement
fahimkamal63

Stack Program

Apr 8th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. //  Stack Program
  2. //  Date : 08.04.19
  3. #include<iostream>
  4. #include<cstdlib>
  5. #define max_size 20
  6. using namespace std;
  7.  
  8. class stack_class{
  9.     int stack_array[max_size];
  10.     int top;
  11.     void insert_stack();
  12.     void delete_stack();
  13.     void display();
  14. public:
  15.     stack_class() { top = 0; }
  16.     void control_function();
  17. };
  18.  
  19. void stack_class::display(){
  20.     if(top == 0){
  21.         cout << "The Stack is Underflow nothing to display.\n";
  22.         return;
  23.     }
  24.     else{
  25.         cout << "The element in the Stack are: \n";
  26.         for(int i = top-1; i > -1; i--){
  27.             cout << stack_array[i] << ' ';
  28.         }
  29.         cout << endl << endl;
  30.     }
  31. }
  32.  
  33. void stack_class::delete_stack(){
  34.     if(top == 0){
  35.         cout << "The Stack is Underflow.\n";
  36.         return;
  37.     }
  38.     else{
  39.         cout << "The element " << stack_array[--top]
  40.              << " is deleted\n";
  41.     }
  42. }
  43.  
  44. void stack_class::insert_stack(){
  45.     if(top == max_size){
  46.         cout << "The Stack is Overflow.\n";
  47.         return;
  48.     }
  49.     else{
  50.         cout << "Enter Element to display: ";
  51.         cin >> stack_array[top++];
  52.     }
  53. }
  54.  
  55. void stack_class::control_function(){
  56.     while(1){
  57.         cout << "* * * * * Welcome to Stack program * * * * *\n"
  58.             << "Press 1 to Insert.\n"
  59.             << "Press 2 to Delete.\n"
  60.             << "Press 3 to Display Stack.\n"
  61.             << "Press 4 to Exit.\n"
  62.             << "\nEnter your choice: ";
  63.         int choice; cin >> choice;
  64.         system("cls");
  65.  
  66.         if(choice == 1) insert_stack();
  67.         else if(choice == 2) delete_stack();
  68.         else if(choice == 3) display();
  69.         else if(choice == 4) break;
  70.         else cout << "Wrong Choice.\n\n";
  71.     }
  72. }
  73.  
  74. int main(){
  75.     stack_class ob1;
  76.     ob1.control_function();
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement