Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Lohrno

Stacks...

This topic is 6053 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Stack. FILO (First In Last Out), very simple example:


int(1 to 10) Array;
int StackPointer = 0;


Void PushStack(int NewInt) {
If StackPointer<11 {
Array[StackPointer]=NewInt;
StackPointer++;
}
}

int PopStack() {
If StackPointer>0 {
PopStack = Array[StackPointer];
StackPointer--;
}
}



Excuse my syntax.

,Jay

Edited by - Jason Zelos on February 21, 2002 6:51:58 PM

Share this post


Link to post
Share on other sites
Man its been a long time since I studied stacks hehe...
but isnt there a built in stack thing for C++?
I thought there was...

-=Lohrno

Share this post


Link to post
Share on other sites
heres one basically from practical c++ programming, best damn c++ book ever, id recommend it

stack.hpp:
  
#include <stdlib.h>

#include <iostream.h>

class stack
{
private:
int currentCount;
int max;
int *data; // change int to whatever data type your going to use

public:
stack(int howMany); // i made this so you can reserve how many items you want on the stack

~stack();
bool push(const int item);
int pop(); // change int to whatever type your using with the stack

};


stack.cpp:
  
#include "stack.hpp"

stack::stack(int howMany)
{
data = new int[howMany]; //again, change int to whatever blahblahblah

max = howMany;
currentCount = 0;
}

stack::~stack()
{
delete [] data;
}

bool stack::push(const int item)
{
if (currentCount < max)
{
data[currentCount] = item;
++currentCount;
return 1;
}
cerr << "STACK IS FULL" << endl;
return 0;
}

int stack::pop()
{
if (currentCount >= 0)
{
--currentCount;
return data[currentCount];
}
return 0;
}


that should probably do it, havent tested it yet, but it should check for overflow and underflow, thats basically what a stack may look like

Share this post


Link to post
Share on other sites
Practical C++ Programming has a 1995 publication date and so is unlikely to describe modern C++ techniques. Here''s how to use the STL stack:

  
#include <stack>

int main()
{
std::stack<int> st;

st.push_back(1);
st.push_back(2);
st.push_back(3);

int i1 = st.top();
st.pop();
int i2 = st.top();
st.pop();
int i3 = st.top();
}


Share this post


Link to post
Share on other sites
Ahhhhhh...thats what I needed sabreman Thanks alot =)
And thanks to everyone else for the homrebew ones hehe!

-=Lohrno

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
Practical C++ Programming has a 1995 publication date and so is unlikely to describe modern C++ techniques.

are you claiming that char arrays have no place in modern programming?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!