Does anybody know where I can find an example of a Spaghetti stack written in C?
It should be something similar to that:
struct stack_item;
struct stack_item
{
stack_item *parent;
void *ptr_data;
};
stack_item *stack_pointer = null;
void push(stack_item *item)
{
if (stack_pointer == null)
item->parent = null;
else
item->parent = cur;
stack_pointer = item;
}
/* like push but doesn't update cur stack item to the one pushed, just add a child */
void push_parallel(stack_item *item)
{
if (stack_pointer == null)
{
stack_pointer = item;
item->parent = null;
}
else
item->parent = stack_pointer;
}
stack_item *pop()
{
if (stack_pointer == null)
{
printf("error: stack is empty.\r\n");
return null;
}
stack_item *current = stack_pointer;
stack_pointer = current->parent;
return current;
}
Mind that a spaghetti stack is useful when you want to keep references of things that you pop out of the stack, having many parallel linked list that end to a common root. So you have to keep references of item you pop out because you need to traverse them in a bottom-up way from the leaf to the root, and of course using a different leaf node will produce a different linked list that has elements in common with other lists that start from other leaves..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With