home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
tmplt.zip
/
TMPLT0.CPP
< prev
next >
Wrap
Text File
|
1994-03-21
|
3KB
|
85 lines
/*----------------------------------------------------------------------------*/
/* tmplt0.cpp */
/* */
/* template example - create a stack template, push some letters onto it, */
/* then pop them off. */
/* */
/* (c) Larry Morley, 1994 */
/*----------------------------------------------------------------------------*/
#include <stdio.h>
int main(void);
/*----------------------------------------------------------------------------*/
/* NOTE: */
/* If the member functions were not defined inline, they would have to be */
/* declared as, for example: */
/* */
/* template <class ATYPE> int Stack<ATYPE> :: push(ATYPE item) */
/* */
/*----------------------------------------------------------------------------*/
template<class ATYPE>
class Stack
{
private:
ATYPE TheStack[100]; // use a fixed size for demonstration purposes
int sp;
public:
/*-------------------------*/
stack()
{
sp = -1; // initialize the stack ptr
}
/*-------------------------*/
int push(ATYPE item) // push an item onto the stack
{
if (sp < (sizeof(TheStack) / sizeof(ATYPE))- 1)
{
TheStack[++sp] = item;
return sp;
}
else
return 0;
}
/*-------------------------*/
ATYPE pop() // pop an item off of the stack
{
if (sp)
return TheStack[sp--];
else
return (ATYPE) 0;
}
/*-------------------------*/
};
/*----------------------------------------------------------------------------*/
int main()
{
Stack<char> MyStack;
MyStack.push('a');
MyStack.push('b');
MyStack.push('c');
printf("%c, ", MyStack.pop());
printf("%c, ", MyStack.pop());
printf("%c.\n",MyStack.pop());
return 0;
}
/*----------------------------------------------------------------------------*/