Click to See Complete Forum and Search --> : basic C++ pointer stuff


Hyena
April 27th, 1999, 10:57 AM
i wrote this stack program a while ago, and have been unable to make the listWrite and pop functions work. please help me.

CODE:
//program stack- linked list data structure

#include <iostream.h>
#include <stddef.h>

struct nodeType;
typedef nodeType* nodePtrType;
typedef int itemType;



//--------------Definition of struct nodeType---------------
struct nodeType
{
itemType items;
nodePtrType nextPtr;
};
//----------------------------------------------------------


itemType item;



//---------------Definition of class stack------------------
class stack {
public:
initStack();
//initializes stack

void listWrite();
//prints out entire stack

void push(int&);
//puts an inputed integer on top of stack

void pop(int&);
//removes top item from stack

int topItem();
//returns top item of stack

private:
nodePtrType S;
};
//----------------------------------------------------------



//____________________-MEMBER FUNCTIONS-_____________________
/***********************************************************/
stack::initStack()
//pre: S is an empty stack
//post: S is initialized to null
{
S = NULL;
return 0;
}

/***********************************************************/
void stack::listWrite()
//pre: S is not empty
//post: list is printed
{
nodePtrType currPtr;
currPtr = S;


while(currPtr != NULL)
{
cout << currPtr->items << "\n";
currPtr = currPtr->nextPtr;
}


}

/************************************************************/
void stack::push(int& item)
//pre: stack is valid and initialized
//post: entered integer is put on top of stack
{
nodePtrType temp;

temp->items = item;
temp->nextPtr = S;
S = temp;
}

/*************************************************************/
void stack::pop(int& item)
//pre: stack has at least one element
//post: top element is removed from stack
{
item = S->items;
S = S->nextPtr;
}

/*************************************************************/
int stack::topItem()
//pre: stack has at least one item
//post: top item of stack is returned
{
item = S->items;
return item;
}



//__________________________-MAIN-_____________________________
void main ()
{
stack s;

s.initStack();

cout << "Enter an integer (999 to quit): ";
cin >> item;



while(item != 999)
{
s.push(item);
cout << "Enter an integer (999 to quit): ";
cin >> item;
}

cout << "\n\nTop item is " << s.topItem() << "\n";


}

END CODE

additionally, the book i'm using recommends breaking any class program into 3 files, but it provides no examples on how to do this. please show me.

Allen Y
April 27th, 1999, 11:34 AM
When you push one item into stack, actually, you need to allocate a piece of memory and its size is struct nodeType. In your push(), you only decalre a nodePtrType, which is a pointer address not a entire struct of nodeType.
void stack::push(int& item)
{
nodePtrType *temp; // pointer

temp = new (struct nodeType); // allocate a new node from heap
temp->items = item;
temp->nextPtr = S;

S = temp;
}

After you pop out one item, you need to free the memory. There are one problem: if there are no more item in the stack, what are you gonna do?
/*************************************************************/
void stack::pop(int& item)
//pre: stack has at least one element
//post: top element is removed from stack
{
nodePtrType temp;

if(S == NULL){
cout << "No more item in stack!!\n";
return;
}
temp = S;
item = temp->items;
S = temp->nextPtr;
delete temp; // free memory
}

Also, you need to test the S before accessing it to avoid crashing.
int stack::topItem()
//pre: stack has at least one item
//post: top item of stack is returned
{
if(S == NULL){
cout <<"Stack is empty!!\n";
return 0;
}
item = S->items;
return item;
}


/////////////////////////////////////////////////////////////////
Hope this will help. Good luck.

Allen

Hyena
April 27th, 1999, 12:22 PM
thanx. this was a great help. but one questions remains; how do code the class definition and its member functions to make it usable as a header file?

sally
April 27th, 1999, 07:45 PM
File stack.h
=============
//stack- definition of linked list data structure

struct nodeType;
typedef nodeType* nodePtrType;
typedef int itemType;

struct nodeType
{
itemType items;
nodePtrType nextPtr;
};

//---------------Definition of class stack------------------
class stack {
public:
initStack(); //initializes stack
void listWrite(); //prints out entire stack
void push(int&); //puts an inputed integer on top of stack
void pop(int&); //removes top item from stack
int topItem(); //returns top item of stack

private:
nodePtrType S;
};

//end of header file


File stack.cpp

#include "stack.h"

and the rest of your source code changed as per Allen's specifications

sally

Sally
April 27th, 1999, 07:45 PM
File stack.h
=============
//stack- definition of linked list data structure

struct nodeType;
typedef nodeType* nodePtrType;
typedef int itemType;

struct nodeType
{
itemType items;
nodePtrType nextPtr;
};

//---------------Definition of class stack------------------
class stack {
public:
initStack(); //initializes stack
void listWrite(); //prints out entire stack
void push(int&); //puts an inputed integer on top of stack
void pop(int&); //removes top item from stack
int topItem(); //returns top item of stack

private:
nodePtrType S;
};

//end of header file


File stack.cpp

#include "stack.h"

and the rest of your source code changed as per Allen's specifications

sally

April 27th, 1999, 08:19 PM
I hope this is the correct answer, for the question you asked is incredibly simple...
All you have to do is put the class definition and member functions in a seperate file with the extension *.h, then just include that where you want to use the declarations