How do i create a stack system in c
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;
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
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
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.
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
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:
which should produce this as output:
Also, STL has a std::stack container adaptor:
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
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...012345Popping values from the stack...543210
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement