C++ Programming Blog

 
 
 
//stack using templates
#include<iostream.h>
#include<iomanip.h>
template<class s>                                ///templats class
class stack
{
      private:
          int top;
          s item;
          s array[5];
      public:
          stack()
          {
              top=-1;
          }
          void getdata()
          {
                  cout<<\"enter item to push in stack\"<<endl;
                cin>>item;

          }
          void push()
          {
              if(top==5)
                  cout<<\"overflow\"<<endl;
              else
              {
                  top++;
                  array[top]=item;
              }
          }
          void pop()
          {
              if(top==-1)
                  cout<<\"underflow\"<<endl;
              else
              {
                  array[top]=NULL;

                  top--;
              }
          }
          void disp()
          {
               for(int i=0;i<=top;i++)
               {
                     cout<<array[i]<<\'\\t\';
               }
                cout<<endl;
          }
};
int main()
{
    stack<int>stacki;
    stack<char>stackc;
    int r;
    char ch;
    do
    {
        cout<<\"integer array\"<<endl;
        cout<<\"press 1 to push element in stack & 2 to pop it\"<<endl;  
        cout<<\"    3 to display stack\"<<endl;
        cin>>r;
        switch (r)
        {
          case 1:
               stacki.getdata();
               stacki.push(); 
               break;
          case 2:
                stacki.pop();
                break;
          case 3:
                stacki.disp();
                break;
          default:
            cout<<\"bad input\"<<endl;
            break;
        }
        cout<<\"do you want to process more y/n\"<<endl;
        cin>>ch;
    }
    while(ch!=\'n\');
do
{    
    cout<<\"character array\"<<endl;
        cout<<\"press 1 to push element in stack & 2 to pop it\"<<endl;  
        cout<<\"    3 to display stack\"<<endl;
        cin>>r;
        switch (r)
        {
          case 1:
               stackc.getdata();
               stackc.push(); 
               break;
          case 2:
                stackc.pop();
                break;
          case 3:
                stackc.disp();
                break;
          default:
            cout<<\"bad input\"<<endl;
            break;
        }
        cout<<\"do you want to process more y/n\"<<endl;
        cin>>ch;
    }
    while(ch!=\'n\');

    return 0;
}

    Related Post:
  1. Program to illustrate the implementation of arrays as a Stack in graphics

  2. Program to draw a 3D Cubic Bezier Curve

  3. Program to construct Newtons Backward Difference Interpolation Formula from the given distinct equally spaced data points

  4. Implement a Matrix class representing 2-dimensional matrix with the following functionalities i.e. operators + , - , * , == , += &lt;&lt; , &gt;&gt; etc

  5. Program to print an array before and after adding 5 to its contents using pointers

  6. Program to illustrate the implementation of Scaling Transformation along a Fixed Point

  7. Program to illustrate pointers , structure and passing structure pointers to function.

  8. Program to estimate the value of Second Derivative of the function at the given points from the given data using Central Difference Formula of order 2

  9. Program to read 2D array and display it row by row and its sum

  10. Program to perform insertion sort

  11. Program that performs addition operation of 2 polar objects by + operator overloading.

  12. Write a function power() to raise a number m to a power n

  13. Program of Sutherland Hodgemann Algorithm for Polygon clipping

  14. Library Management System

  15. Program to illustrate the implementation of arrays as a Linear Queue ( in graphics )

  16. Program to read a Linear System of Equations,then evaluate it by using Guass-Seidel Itrative Method and show the result

  17. Program to illustrate friend function

  18. Program of Liang Barsky Algorithm for Line Clipping

  19. Menu driven Bank Application Project

  20. Program to draw an ellipse using Trigonometric Method

 
 
Didn't find what you were looking for? Find more on Stacks code in C++