Jump to content
  • Advertisement

Archived

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

SauronTheDark

How do i create a stack system in c

This topic is 5992 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

I want to be able to put values into a stack and retrive them in revers order like this: ADD: ABCD RETRIVE: DCBA can anyone please help me if you can understand what i am going on about #define BLOODY_WORK_THIS_TIME;

Share this post


Link to post
Share on other sites
Advertisement
You mean a FIFO (first in first out). There are tons of websites regarding stacks in C. Are you looking for a tut or just some code to rip?

http://www2.iro.umontreal.ca/~ratib/code/
http://moon.king.ac.uk/~m.chaplain/snippets.html
http://www.cs.sunysb.edu/~algorith/implement/c.shtml
http://www.cs.fiu.edu/~weiss/dsaa_c2e/files.html

Share this post


Link to post
Share on other sites
quote:
Original post by LessBread
You mean a FIFO (first in first out).


He described a LIFO. A FIFO would be a queue, not a stack.

--
The placement of a donkey''s eyes in its head enables it to see all four feet at all times.

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
He described a LIFO. A FIFO would be a queue, not a stack.



You're right, I flubbed.





Edited by - lessbread on February 22, 2002 9:44:29 AM

Share this post


Link to post
Share on other sites
Look into the STL container adapters.

The easiest way of doing that would be just create a vector, and use the push_back() and pop_back() methods:

      

#include <iostream>

#include <vector>

int main(int argc, char **argv)
{

std::vector<int> vec;

std::cout << "Pushing values on to the stack..." << std::endl;

while(vec.size()<6)
{
std::cout << vec.size() << std::endl;
vec.push_back(vec.size());
}

std::cout << "Popping values from the stack..." << std::endl;

while(vec.size()!=0)
{
std::cout << vec.back() << std::endl;
vec.pop_back();
}

return 0;
}



which should produce this as output:

Pushing values on to the stack...
0
1
2
3
4
5
Popping values from the stack...
5
4
3
2
1
0


Also, STL has a std::stack container adaptor:

  
#include <iostream>

#include <deque>

#include <stack>


int main(int argc, char **argv)
{
// Uses a std::deque by default

std::stack<int> stack;

std::cout << "Pushing on to stack..." << std::endl;

while(stack.size()<6)
{
std::cout << stack.size() << std::endl;
stack.push(stack.size());
}

std::cout << "Popping from stack..." << std::endl;

while(stack.size()!=0)
{
std::cout << stack.top() << std::endl;
stack.pop();
}
return 0;
}


That'll produce the same thing

Edited by - daerid on February 22, 2002 1:21:04 PM

Edited by - daerid on February 22, 2002 1:21:44 PM

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!