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.
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.