MikeFoster
April 1, 2001, 03:19 am
Ok, I just don't know when to stop, I admit it. Now I'm gonna do someone's homework for them. The following is from a C++ class I taught a few years ago. Can you come up with a good use for it?
// stack.h --------------------------------------------------------------------
// Snead State Community College
// CIS 252 Winter 96/97 Advanced C++
//-----------------------------------------------------------------------------
//
// An Array Implementation of a Stack
//
// DECLARATIONS
//
//-----------------------------------------------------------------------------
// Constants ------------------------------------------------------------------
const int
STACKSIZE = 100,
EMPTYINDEX = -1;
// Class Declaration ----------------------------------------------------------
class Stack {
private:
int top; // top of stack index
double stk[ STACKSIZE ]; // stack buffer
public:
Stack(); // init to empty
int Empty(); // returns true if stack is empty
int Push( double element ); // stores element on top of stack
double Pop(); // removes element from top of stack
double Peek(); // returns top of stack without removing it
};
// Description of the Stack Class =============================================
// A stack is a data structure that works similar to a stack of plates at
// a buffet. An element can only be inserted at the top of the stack. An
// element can only be removed from the top of the stack. For this reason
// this data structure is also called a LIFO structure (Last In First Out).
// The following Stack class has two data members:
// stk - an array of doubles, it is used to hold the stack elements.
// top - an array index to the top of the stack. The next pop will
// remove the array element at this index. The next push will
// increment this index by one and store an element at that
// position in the array.
// The Stack class has five function members:
// Stack - the constructor; it initializes the stack to empty.
// Empty - returns true if the stack is empty, else false.
// Push - stores a new element on top of the stack and returns true,
// else returns false if the stack is full.
// Pop - removes the element currently on top of the stack and returns it,
// else returns false if the stack is empty.
// Peek - returns the element on top of the stack without removing it.
// end stack.h ----------------------------------------------------------------
// stack.cpp ------------------------------------------------------------------
// Snead State Community College
// CIS 252 Winter 96/97 Advanced C++
//-----------------------------------------------------------------------------
//
// An Array Implementation of a Stack
//
// DEFINITIONS
//
//-----------------------------------------------------------------------------
#include "stack.h"
// Class Member Function Definitions ------------------------------------------
// Initialize the stack to empty.
Stack::Stack()
{
top = EMPTYINDEX;
}
// Return true if the stack is empty, else false.
int Stack::Empty()
{
int status = 0;
if ( top == EMPTYINDEX ) {
status = 1;
}
return status;
}
// Store element on top of the stack and return true,
// else return false if stack is empty.
int Stack:http://www.helpfromtechs.com/ubb/smilies/tongue.gifush( double element )
{
int status = 0;
if ( top + 1 < STACKSIZE ) {
++top;
stk[ top ] = element;
status = 1;
}
return status;
}
// Remove the element currently on top of the stack and return it,
// else return false if the stack is empty.
double Stack:http://www.helpfromtechs.com/ubb/smilies/tongue.gifop()
{
double element = 0;
if ( top != EMPTYINDEX ) {
element = stk[ top ];
--top;
}
return element;
}
// Return without removing the element currently on top of the stack,
// return false if the stack is empty.
double Stack:http://www.helpfromtechs.com/ubb/smilies/tongue.gifeek()
{
double element = 0;
if ( top != EMPTYINDEX ) {
element = stk[ top ];
}
return element;
}
// end stack.cpp --------------------------------------------------------------
------------------
Try these HelpFromTechs.com Web Development Resources:
Reference Links (http://www.helpfromtechs.com/ubb/Forum8/HTML/000054.html)*|*User Authentication (http://www.helpfromtechs.com/ubb/Forum8/HTML/000071.html)*|*HTML Editors (http://www.helpfromtechs.com/ubb/Forum8/HTML/000030.html)*|*Perl/CGI (http://www.helpfromtechs.com/ubb/Forum8/HTML/000016.html)*|*Embedding Music (http://www.helpfromtechs.com/ubb/Forum8/HTML/000067.html)
// stack.h --------------------------------------------------------------------
// Snead State Community College
// CIS 252 Winter 96/97 Advanced C++
//-----------------------------------------------------------------------------
//
// An Array Implementation of a Stack
//
// DECLARATIONS
//
//-----------------------------------------------------------------------------
// Constants ------------------------------------------------------------------
const int
STACKSIZE = 100,
EMPTYINDEX = -1;
// Class Declaration ----------------------------------------------------------
class Stack {
private:
int top; // top of stack index
double stk[ STACKSIZE ]; // stack buffer
public:
Stack(); // init to empty
int Empty(); // returns true if stack is empty
int Push( double element ); // stores element on top of stack
double Pop(); // removes element from top of stack
double Peek(); // returns top of stack without removing it
};
// Description of the Stack Class =============================================
// A stack is a data structure that works similar to a stack of plates at
// a buffet. An element can only be inserted at the top of the stack. An
// element can only be removed from the top of the stack. For this reason
// this data structure is also called a LIFO structure (Last In First Out).
// The following Stack class has two data members:
// stk - an array of doubles, it is used to hold the stack elements.
// top - an array index to the top of the stack. The next pop will
// remove the array element at this index. The next push will
// increment this index by one and store an element at that
// position in the array.
// The Stack class has five function members:
// Stack - the constructor; it initializes the stack to empty.
// Empty - returns true if the stack is empty, else false.
// Push - stores a new element on top of the stack and returns true,
// else returns false if the stack is full.
// Pop - removes the element currently on top of the stack and returns it,
// else returns false if the stack is empty.
// Peek - returns the element on top of the stack without removing it.
// end stack.h ----------------------------------------------------------------
// stack.cpp ------------------------------------------------------------------
// Snead State Community College
// CIS 252 Winter 96/97 Advanced C++
//-----------------------------------------------------------------------------
//
// An Array Implementation of a Stack
//
// DEFINITIONS
//
//-----------------------------------------------------------------------------
#include "stack.h"
// Class Member Function Definitions ------------------------------------------
// Initialize the stack to empty.
Stack::Stack()
{
top = EMPTYINDEX;
}
// Return true if the stack is empty, else false.
int Stack::Empty()
{
int status = 0;
if ( top == EMPTYINDEX ) {
status = 1;
}
return status;
}
// Store element on top of the stack and return true,
// else return false if stack is empty.
int Stack:http://www.helpfromtechs.com/ubb/smilies/tongue.gifush( double element )
{
int status = 0;
if ( top + 1 < STACKSIZE ) {
++top;
stk[ top ] = element;
status = 1;
}
return status;
}
// Remove the element currently on top of the stack and return it,
// else return false if the stack is empty.
double Stack:http://www.helpfromtechs.com/ubb/smilies/tongue.gifop()
{
double element = 0;
if ( top != EMPTYINDEX ) {
element = stk[ top ];
--top;
}
return element;
}
// Return without removing the element currently on top of the stack,
// return false if the stack is empty.
double Stack:http://www.helpfromtechs.com/ubb/smilies/tongue.gifeek()
{
double element = 0;
if ( top != EMPTYINDEX ) {
element = stk[ top ];
}
return element;
}
// end stack.cpp --------------------------------------------------------------
------------------
Try these HelpFromTechs.com Web Development Resources:
Reference Links (http://www.helpfromtechs.com/ubb/Forum8/HTML/000054.html)*|*User Authentication (http://www.helpfromtechs.com/ubb/Forum8/HTML/000071.html)*|*HTML Editors (http://www.helpfromtechs.com/ubb/Forum8/HTML/000030.html)*|*Perl/CGI (http://www.helpfromtechs.com/ubb/Forum8/HTML/000016.html)*|*Embedding Music (http://www.helpfromtechs.com/ubb/Forum8/HTML/000067.html)