728x90 AdSpace

Latest Article



How to Implement stack Class in C++ with Source Code Example




Sample code on How to Implement stack Class data structure in C++ with Source Code Example?

# include <iostream.h>
# include <conio.h>
# include <string.h>

//template <class type>

class stack
 {
   private:
      int* array;
      int max;
      int top;

   public:
      // function prototypes
    stack(int maxsize=5);

      ~stack();

      void add(const int&);

      void del();

      bool isfull();

      bool isempty();

      int* full() ;

      void empty() ;

      void stackfull();

      void pop();

      int pop2();



  };


//************function Defintions*****************//

//template <class type>
stack::stack(int maxsize):max(maxsize),top(0)
 {
   array   = new int[max] ;
   }

//template <class type>
void stack::add(const int &x)
 {
   if (isfull())
      {
    stackfull();
      }

      array[top++]=x;

   }



//template <class type>
void stack::stackfull()
 {
   cout << "\n Data structure full" ;
   cout << "\n Creating new data structure..." ;
   int* newarray = new int[2*max];
   for (int i=0;i<max;i++)
    newarray[i]=array[i];

   delete[] array ;

   array = newarray ;
   cout << "\n New data structure created" ;
   }

//template <class type>
void stack::del()
 {
   if (isempty())
    {
      cout << "\n Data structure empty; "
           << "cannot delete" ;
      }

   else
    {
      --top;
      }

    }

//template <class type>
bool stack::isempty()
 {
   if (top==0)
    return true;

   return false;
   }


//template <class type>
bool stack::isfull()
 {
   if (top==(max-1))
    return true;
   return false;
   }

//template <class type>

void stack::pop()
 {
 cout << endl << array[--top];
   }

//template <class type>
int stack::pop2()
 {
   return array[top--] ;
   }

//template<class type>
stack::~stack()
 {
   delete[] array;
   }

//*****************************************************//
//template<class type>
class tracker
 {
   private:
    stack record;
      stack data;
   public:
      void pushone()
       {
         int a           ;
         cout << "\n Enter data: " ;
         cin  >> a       ;
         data.add(a)     ;
         record.add(0)   ;
         cout << "\n 00000000000000000000000000000" ;
         }

      void pushtwo()
       {
         int a , b  ;
         cout << "\n Enter data: " ;
         cin >> a ;
         data.add(a) ;
         cout << "\n Enter data: " ;
         cin >> b ;
         data.add(b) ;
         record.add(1) ;
         cout << "\n 11111111111111111111111111111" ;
         }

      void specialpop()
       {
         data.pop();
         cout << "\n pop" ;
         if(record.pop2())
          {
            data.pop();
            cout << "pop" ;
            }
         }


   };











//*****************************************************//

void main()
 {
   tracker object1;
   object1.pushone();
   object1.pushone();
 object1.pushtwo();
   object1.pushone();
   object1.pushtwo();
   object1.pushone();
   object1.pushtwo();
   object1.pushtwo();
   object1.specialpop();
  object1.specialpop();
   object1.specialpop();
   object1.specialpop();
   object1.specialpop();
   object1.specialpop();
   object1.specialpop();
   object1.specialpop();
   getch();
   }


no image






  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment