如何解释栈的方式是C [英] Explanation of how stacks work in C

查看:87
本文介绍了如何解释栈的方式是C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我推到一个堆栈数据时,只是想链接过程的一个简单的解释。我知道如何建立在使用code从我的书,但我真的不知道我的理解,当你从一个移动栈头链接到下一个流程是如何工作的。

I am just wanting a simple explanation of the linking process when pushing data onto a stack. I know how to build on using the code from my book, but I am not really sure I understand how the process works when you move the stack head link from one to the next.

对于像堆栈:

typedef struct node
{
    void dataptr;
    struct node* link;
}STRUCT_NODE;

typedef struct
{
    int count;
    STACK_NODE* top;
}STACK;

如何改变链接指向新的数据压入堆栈。此外,我不知道

How do you change the link to point to the new data pushed on the stack. Also I do not know

推荐答案

堆叠可以以各种方式来实现,但考虑到这样你那句你的问题我假设你的筹码仅仅是一个链表,像

Stacks can be implemented in various ways, but given the way you phrase your question I'm assuming your stack is just a linked list, something like

head
↓
A → B → C → D → 0

图片只是改变为当你从一个到下一个移动栈头链接:

"when you move the stack head link from one to the next" the picture just changes to:

    head
    ↓
A → B → C → D → 0

当然是一个不可达的任何更多的在该图中,所以你最好有另一个指针指向它的地方(无论是只处理它),但是这是要点怎么怎么堆栈被弹出(通过头=流浆>接下来如果堆栈中的各个节点是一个结构节点接下来字段,这是一个结构节点* ,当然还有结构节点* 以及)。

Of course A is not reachable any more in this graph, so you'd better have another pointer to it somewhere (be it only to dispose of it), but this is the gist how how the stack is popped (by making head = head->next if each node in the stack is a struct node with a next field that's a struct node*, and of course head is a struct node* as well).

这对弹出的东西从堆栈(你应该释放在这种情况下由 A 使用的内存)。在详细的步骤,这将是:

That's for popping something off the stack (and you should free the memory used by A in that case). In detailed steps, it would be:

1 /保存旧的头。

      head
      ↓
old → A → B → C → D → 0

2 /调整头。

2/ Adjusting the head.

          head
          ↓
old → A → B → C → D → 0

3 /返回老人头(由指向)。

如果不是你在谈论的东西推的的堆栈,这是涉及一个操作:

If instead you're talking about pushing something onto the stack, that's an operation that involves:

1 /创建一个新的元素。

1/ Creating a new element.

    head
    ↓
Z   A → B → C → D → 0

2 /指着它向现任掌门

2/ Pointing it towards the current head

    head
    ↓
Z → A → B → C → D → 0

3 /调整头部指向它。

3/ Adjusting the head to point to it.

head
↓
Z → A → B → C → D → 0

这篇关于如何解释栈的方式是C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆