Simple Stack Template Stack Operator::
#include <iostream.h>
class IStack{
enum {ssize =10};
int s[ssize];
int top;
public:
IStack():top(0){s[top]=0;}
bool Isempty(){
if (top ==0)return true;
else
return false;
}
bool push(int e){
if(top<ssize){
s[top++]=e;
return true;
}
else
return false;
}
int pop(){
return s[top>0 ? --top: top];
}
};
template <class T>
class TStack{
enum { ssize =10 };
T s[ssize];
int top;
public:
TStack():top(0){ s[top]=0; }
bool push(const T e){
if(top<ssize){
s[top++]=e;
return true;
}
else
return false;
}
T pop(){
return s[top>0 ? --top: top];
}
};
-----------------------------------------------------------------------------------------------------
Link List Dynamic increament Stack :
#include <malloc.h>
#include <stdio.h>
#include <iostream.h>
enum { INIT_SIZE=5,INCREMENT=5};
typedef struct{
char * base;
char * top;
int stacksize;
}LStack;
void InitStack(LStack &S){
S.base=(char*)malloc(INIT_SIZE * sizeof(char));
if(! S.base) return ;
S.top=S.base;
S.stacksize=INIT_SIZE;
}
bool Push(LStack &S,char e){
if((S.top-S.base)>=S.stacksize){
S.base=(char*)realloc(S.base,(S.stacksize + INCREMENT)*sizeof(char));
if(!S.base) return false;
S.top=S.base+S.stacksize;
S.stacksize+=INCREMENT;
}
*S.top++=e;
return true;
}
bool Pop(LStack &S,char &e){
if(S.top==S.base)return false;
e=*--S.top;
return true;
}