有人知道我在哪里可以找到 Spaghetti stack 的例子吗?用 C 写的?
最佳答案
应该是这样的:
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;
}
请注意,意大利面条堆栈在您想要保留从堆栈中弹出的内容的引用时很有用,因为有许多并行的链表以一个公共(public)结尾根。所以你必须保留你弹出的项目的引用,因为你需要以自下而上的方式从叶到根遍历它们,当然使用不同的叶节点会产生不同的链接与从其他叶子开始的其他列表具有共同元素的列表..
关于c - C 中的意大利面条堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1683810/