Archived

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

How do i create a stack system in c

This topic is 5773 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
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
You could use the inline assembler since the stack works in LIFO anyway. Just use push and pop. I don''t know what compiler you use but you can just look in the help for help with the inline assembler. It would be something like this:


__asm{
push var1
push var2
push var3
pop ecx
pop ebx
pop eax
}


ecx would hold whatever was stored in var3 when it was "pushed" onto the stack. ebx would hold what var2 had been, and so on...

I may be wrong, but I believe that would work...

Minion

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I used a funny bit like this in a program of mine once ...


stack_el_t *_internal_stack (int fn, stack_el_t *element)
{
static stack_el_t *stack_head = NULL;
static stack_tail = NULL;
switch (fn)
{
case PUSH:
stack_tail->next = ... ;
break;
case POP:
if (NULL != stack_head)
{ ... return (old_tail);}
break;
case PEEP:
return (NULL == stack_head);
}
return (NULL);
}

void push (stack_data_type data)
{
_internal_stack (PUSH, new_stack_element (data));
}

stack_data_type pop ()
{
stack_el_t *popped = _internal_stack (POP, NULL);
if (popped != NULL)
{
stack_data_type data = popped->data;
free_stack_element (popped);
return data;
}
return (NULL);
}

int is_empty ()
{
return (_internal_stack (PEEK, NULL));
}



... and the litle details you can fill in, like the stack_el_t type and the adding/removing which is somewhat dependant thereupon.

quote:
Original post by SauronTheDark
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
Guest Anonymous Poster
just make a stack struct and some functions, I don''t see what the big deal is.

Approach one:

use an array and an int. The int starts at 0. When you push something onto the stack put it in the cell that the int is, and then increment it. So the bottom of the stack is at index 0 and the top is at the index equal to the int. Nice performance, finite stack size.

Approach two: use a linked list. To push something on just put it on the front of the list. To take it off just take it off the top.

Best Approach: buy a book on data structures

Share this post


Link to post
Share on other sites
Here''s some example code using the linked list approach. If you''re using an older C compiler you''ll need to change some of the syntax.

  

struct node
{
int num;
node * next;
};

void push(int n, node ** stack)
{
node * newnode = new node;
newnode->num = n;
newnode->next = *stack;
*stack = newnode;
}

int pop(node ** stack)
{
int ret = (*stack)->num;
node * next = (*stack)->next;
delete *stack;
*stack = next;
return ret;
}

int main()
{

node * stack = NULL;

push(2, &stack);
printf("%d\n", pop(&stack));


return 0;
}

Share this post


Link to post
Share on other sites