在C的意粉堆

Ral*_*lph 13 c data-structures

有谁知道我在哪里可以找到用C编写的Spaghetti堆栈的例子?

Jac*_*ack 5

应该是类似的东西:

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;
}
Run Code Online (Sandbox Code Playgroud)

请注意,当您希望保留从堆栈弹出的内容的引用时,意大利面条堆栈很有用,并且有许多并行链接列表以公共根结尾.所以你必须保留你弹出的项目的引用,因为你需要以自下而上的方式从叶子到根遍历它们,当然使用不同的叶子节点会产生一个不同的链表,其中包含与其他列表从其他叶子开始..