a problem on creating a Stack class

[b][red]This message was edited by microBOY at 2003-3-31 19:50:15[/red][/b][hr]
I'm writting a Stack class that able to stack
all kinds of data type.

the solution i have is, instead of stacking the
data into the stack, we stack the pionter of data into
the stack.

There're 3 main function: PUSH,POP,TOP

..a problem in function Top(). It views the data on
the top of stack (without POPing it out) by returning
the pointer of data.

The problem is "the pointer of data is returned". It means
now, data can be modified by any non-members function,
although the data is 'private member' in clsStack class.

Futhermore, the function Top() have to be desigened
to ONLY VIEWs data and not to MODIFIY the data.

is there anyway to ensure the proctection on the data?

i'd include the source code of clsStack class as below:
#ifndef CLSSTACK_H
#define CLSSTACK_H

#if !defined(__STDLIB_H)

/* Filename: clsStack.h
create a stack class using Listed-List implementation

written by stupidiot (20030326)

note: The Stack stores(Push) the pointer of data, instead
of the data itself. It (Pop)s out the pointer of data
instead of the copy of data.

IMPORTANT! the pointer data must point to a dynamic memory
created by 'new' operator. Or else a fault will orccur.

Example : =================================================

void main() {
int *data = new int; //IMPORTANT!
*data = 12;

clsStack Stack;

cout<<data; //display the address of data pointer.

cout<<Stack.Top(); //same address as above

cout<<Stack.Pop(); //still the same address

struct strucNode {
void *pData;
strucNode *pLink;

class clsStack {
int count;
strucNode *pTop;

int Push(void *pNewData);
void *Pop(void);
void *Top(void);

int IsEmptyStack(void);
int IsFullStack(void);


clsStack::clsStack() {
/* Constructor creates an empty stack
Pre : Nothing
Post: an empty stack is created
pTop = NULL;
count = 0;

clsStack::~clsStack() {
/* Destructor destroy the stack
Pre : A stack
Post: the stack is destroyed
strucNode *temp;

//delete all nodes in stack
while( pTop != NULL ) {

//free data entry
delete pTop->pData; //if the pointer sent in
//isnt point to a dynamic memory
//created by 'new' operator,
//this line will cause a fault to occur.

//free node
temp = pTop;
pTop = pTop->pLink;
delete temp;


int clsStack::Push( void *pNewData ) {
/* Pushes an item into a stack
Pre : pData is a pointer to the data to be inserted
Post: returns 1 if success
-or- 0 if heap overflow

strucNode *pNewNode;

pNewNode = new strucNode;
if ( !pNewNode )
return 0;

pNewNode->pData = pNewData;
pNewNode->pLink = pTop;

pTop = pNewNode;

return 1;

void * clsStack::Pop(void) {
/* pops the item on the top of stack
Pre : Stack is created
Post: returns pointer to data if success
-or- NULL if underflow(empty stack)
strucNode *temp;
void *pDataOut;

//stack is empty/underflow
if( count == 0 )
pDataOut = NULL;
else {
temp = pTop;
pDataOut = pTop->pData;
pTop = pTop->pLink;
delete temp;


return pDataOut;

void * clsStack::Top(void) {
/* retrieves the data from the top of stack
whithout poping out the data
Pre : a stack is created
Post: returns a data pointer if found
-or- NULL if underflow

if( count > 0 ) {
return pTop->pData;
return NULL;


int clsStack::IsEmptyStack(void) {
/* determine is stack empty
Pre : stack is created
Post: return 1 if empty
-or- 0 if not empty
return ( count == 0 );

int clsStack::IsFullStack(void) {
/* determine is memory full to add a new node
Pre : stack is created
Post: return 1 if overflow
-or- 0 if not overflow
strucNode *temp;
temp = new strucNode;

short isFull;

if( temp == NULL )
isFull = 1;
isFull = 0;

delete temp;
return isFull;

#endif /* clsStack.h */



  • Try placing const at the end of your function prototype and heading.

    short small(const int *pointer) const;

    I dunno if that will do what you are looking for, but that popped into my head...
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion