C and C++ Programming Resources

Custom Search

Using Templates to implement Stack

Using Templates to implement Stack

/*******************************************************
*     MYCPLUS Sample Code - http://www.mycplus.com     *
*                                                     *
*   This code is made available as a service to our   *
*      visitors and is provided strictly for the      *
*               purpose of illustration.              *
*                                                     *
* Please direct all inquiries to saqib at mycplus.com *
*******************************************************/

#include            // for sleep()
#include      // for I/0
#include      // for MessageBox() API
#include 


#define MAX 10        // MAXIMUM STACK CONTENT


template     // Using Templates so that any type of data can be
               // stored in Stack without multiple defination of class
class stack
{


  protected:


  T arr[MAX];       // Contains all the Data


  public:
    T item,r;
    int top;        //Contains location of Topmost Data pushed onto Stack
  stack()         //Constructor
   {
    for(int i=0;ia;    // Create object of class a with int Template
 int opt=1;
 while (opt!=3)
 {
 clrscr();
 cout<<" MAX STACK CAPACITY="<<((MAX-a.top)-1)<<"\n\n\n\n";
 cout<<"1) Push Item\n";
 cout<<"2) Pop Item\n";
 cout<<"3) Exit\n\n";
 cout<<"Option?";
 cin>>opt;
 switch(opt)
 {
  case 1:
  cout<<"Which Number should be pushed?";
  cin>>a.item;
  a.push(a.item);
  break;


  case 2:
  a.r=a.pop();
  cout<<"Item popped from Stack is:"<
    	

Tags: , , ,

There are No Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.


Leave a Reply

You must be logged in to post a comment.