Saturday, February 27, 2016

Stack Program Using C++

************** Stack Program Using C++*******************

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Program Name :- MyStack.cpp

Description :- This program illustrate the concept of stack in C++. Object Oriented Implementation of stack.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include<iostream>
using namespace std;

typedef int BOOL;

#define TRUE 1
#define FALSE 0

//
// Declaration of class stack
//

class Stack
{
        //
        // Class Member
        //
        private:
                    int top;
                    int MAX;
                    int *arr;

       public :
                   Stack();                   // Default Constructor.
                  Stack(int);               // Parameterize constructor.
                  Stack(Stack &);      // Copy Constructor.
                  ~Stack();                 // Destructor.

   ////////////// /////////Wrapper Functions ///////////////////////////////////

                  void Display();    
                  void Push(int);
                  int Pop();

 /////////////////////// Helper Functions ///////////////////////////////////////

      private:
                 BOOL IsStackFull();
                 BOOL IsStackEmpty();
};

/////////////////////////////// Function Definations ////////////////////////////////////////////////////////

Stack :: Stack()
{
         top = 0;
         MAX = 0;
        arr = new int[MAX];
}


Stack :; Stack( int Size = 10)
{
             top = -1;
             MAX = Size;
             arr = new int[MAX];
}

Stack :: Stack(Stack &ref)
{
          top = ref.top;
          MAX  = ref.MAX;
          arr = new int[MAX];
       
          for(int i = 0; i < ref.MAX; i++)
          {
                  arr[i] = ref.arr[i];
           }
}

Stack :: ~Stack()
{
           delete arr[];
}

//
// To Check Stack is Full or Not
//

BOOL Stack :: IsStackFull()
{
             if( top == MAX)
             {
                            return TRUE;
             }
              else
             {
                            return FALSE;
              }
}

//
// To Check Stack is Empty of not
//

BOOL Stack :: IsStackEmpty()
{
            if( top == -1)
            {
                     return TRUE;
             }
             else
             {
                    return FALSE;
              }
}

//
// Insert Element in to Stack
//

void Stack :: Push(int iNum)
{
         if(IsStackFull())
         {
                return;
         }
         arr[++top] = no;
}

//
// Delete Element From Stack
//
int Stack :: Pop()
{
         if(IsStackEmpty())
        {
               return -1;
        }
        return arr[top--];
}

//
// Display Element of Stack
//

void Stack :: Display()
{
       for(int i = top; i >= 0; i--)
       {
             count<<arr[i];
       }
}

//
// Entry Point Function
//

int main()
{
         Stack obj1;       // Size is 10
         Stack obj2(20);       // Size is 20

         obj1.Push(10);
         obj1.Push(20);

         obj2.Push(33);
         obj2.Push(40);
      
        Stack obj3(obj1);         // Copy Constructor

         cout<<obj1.Pop();     // 20

        obj3.Display();           // 20, 10

         return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                 

No comments:

Post a Comment

To Connect Java program with the MYSQL Database

To connect java application with the MYSQL database, first of all to install MYSQL database in your machine. To install MYSQL in your mach...