Stacks...
Could someone please post a quick stack example here?
or even a couple?
-=Lohrno
Stack. FILO (First In Last Out), very simple example:
Excuse my syntax.
,Jay
Edited by - Jason Zelos on February 21, 2002 6:51:58 PM
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
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
but isnt there a built in stack thing for C++?
I thought there was...
-=Lohrno
heres one basically from practical c++ programming, best damn c++ book ever, id recommend it
stack.hpp:
stack.cpp:
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
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 usepublic: 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
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();}
Ahhhhhh...thats what I needed sabreman Thanks alot =)
And thanks to everyone else for the homrebew ones hehe!
-=Lohrno
And thanks to everyone else for the homrebew ones hehe!
-=Lohrno
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?
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement