PDA

You are currently viewing a search engine-friendly (archive) version of this page.

View Full Version : C++ Stack with Array Implementation


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)

MikeFoster
April 1, 2001, 03:22 am
LOL! Check out the methods that start with a 'p'!

http://www.helpfromtechs.com/ubb/smilies/lol.gif

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

glussier
April 1, 2001, 03:32 am
The only reason you would use this code, is for academic purpose. The assembler already has the push and pop instructions to access the system's stack. In C and C++ the push and pop instructions are used extensively, without the user noticing it.

You could use that code as a start to build a forth interpreter.

------------------

MikeFoster
April 1, 2001, 04:07 am
Hi glussier!

You are correct! However, this class is for creating a user-defined stack - it does not provide access to the stack segment.

Stacks are very useful data structures. Ever used an RPN calculator? http://www.helpfromtechs.com/ubb/smilies/smile.gif

If I change the code slightly, so that the pop method pulls from the other end, then I have another data structure called a queue, a FIFO.

You are right, this question is very academic. Stack classes come with most C++ compilers now. And especially in the Standard Template Library.

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

<FONT COLOR="#800080" SIZE="1" FACE="Verdana, Arial">This message has been edited by MikeFoster on April 01, 2001 at 12:07 AM</font>

Tonker
April 2, 2001, 11:34 am
The funny thing about STL is it hasn't been really S until recently. http://www.helpfromtechs.com/ubb/smilies/wink.gif

We don't use STL at work because we use so many different compilers on so many platforms - native compilers on HPUX, Solaris, AIX as well as GCC on all those platforms and unix, then Visual Studio. We often end up implementing our own code for this reason.

I gather that they're becoming more standard now, but many people are still using old compilers. So I guess there might be a use for your code Mike. http://www.helpfromtechs.com/ubb/smilies/smile.gif Of course it might be nice if you could dynamically resize the stack if required. I suppose that wasn't what they were trying to teach you with that exercise, though. http://www.helpfromtechs.com/ubb/smilies/wink.gif

PS: Wish I'd had that when I was doing my C++ course last summer.

------------------

MikeFoster
April 2, 2001, 12:50 pm
Hi Tonker,

Very good points.

If you need a dynamically sized stack, queue, linked list, or tree, I have this code ready for download (http://lineoflight.com/links/programming.html) (its all in C). I also have some of these things in C++.

Thanks!

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

<FONT COLOR="#800080" SIZE="1" FACE="Verdana, Arial">This message has been edited by MikeFoster on April 02, 2001 at 09:51 AM</font>

glussier
April 2, 2001, 01:17 pm
Being an Operating System engineer, I always have liked better assembler. Now, with my consulting company, where 95% of the contracts are on Robotik and Process Control, assembler is still the way to go. Sometimes we will use Visual C++ under windows to design the interface but all the underlying code is written in masm, on Unix systems we use mostly asm and sometimes some of the code is written with gcc, on mainframes we use asm360/370 with some of the code written in PL/I.

------------------

Tonker
April 2, 2001, 02:28 pm
Wow Mike! Some great stuff there!

I notice a certain amount of VB stuff. I was browsing around a couple of weeks ago and found that somebody's working on a GNU port of VB for Linux (presumably Unix in general, but I don't know for sure). Just thought I'd share the merriment. They've even got some screen shots of ported code running under GNOME. =)
http://canvas.gnome.org:65348/gb/


------------------

MikeFoster
April 3, 2001, 06:57 pm
Tonker: Thanks!

glussier: Wow! Low-level robotics control - cool! I work with automation systems, but most of my programming is in IEC-61131 languages.



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