链接列表 - 堆栈构建 - C - 本教程是否正确

时间:2022-10-04 07:21:26

I am trying to work a through a computer science course on coursebuffet.com, which referred me to saylor.org, which gave me this to learn about how to implement a stack with a linked list in C.

我正在尝试通过coursebuffet.com上的计算机科学课程,该课程将我推荐给saylor.org,它让我了解如何在C中实现带有链表的堆栈。

First, I think I understood the concept, but if you'd be so kind and scroll down in the link you will find at the end of it a link to a main file, with which you should test your implementation of it. And what absolutely baffles me for the last two days (yeah, that's how much time I already sank in this one problem) is the following passage:

首先,我认为我理解了这个概念,但是如果你是如此善良并在链接中向下滚动,你会在它的末尾找到一个指向主文件的链接,你应该用它来测试你的实现。在过去的两天里,最让我感到困惑的是(是的,我已经在这个问题中沉没了多少时间)是以下段落:

/*
   * Initialize the stack.  Make it at least
   * big enough to hold the string we read in.
*/
StackInit(&stack, strlen(str));

I can't understand how to initialise a linked list. I mean, that's against its concept, isn't it? I would need to create struct Elements before filling them with push commands, but if I do that, I need to follow the stack in two directions. One directions for pushing it and the opposite direction for popping it. That would need two pointers. I thought the whole concept as described here would be one data element and one pointer per ADT-unit.

我无法理解如何初始化链表。我的意思是,那是违背其概念的,不是吗?我需要在使用push命令填充它们之前创建struct Elements,但是如果我这样做,我需要在两个方向上跟踪堆栈。推动它的一个方向和弹出它的相反方向。那需要两个指针。我认为这里描述的整个概念将是一个数据元素和每个ADT单元一个指针。

Can someone please explain this to me?

有人可以向我解释一下吗?

2 个解决方案

#1


1  

When you initialize list to be length of the string you want to read, you will still have stack pointer pointing to the first element of the list. So basically nothing is lost. However you are correct, there is no point of doing something like that.

当您将list初始化为要读取的字符串的长度时,您仍然会将堆栈指针指向列表的第一个元素。所以基本上没有什么丢失。无论你是对的,没有必要做那样的事情。

There is no need for double linked list. Stack pointer will always point to the first element. Basically whenever you want to push() you will add new node to the beginning of the list, and whenever you want to pop() you will remove first node of the list.

不需要双链表。堆栈指针始终指向第一个元素。基本上每当你想推送()时,你会将新节点添加到列表的开头,每当你想要pop()时,你将删除列表的第一个节点。

#2


1  

Assume stack signifies LIFO operation only. i.e last in will be the first to get out.

假设堆栈仅表示LIFO操作。即将是第一个出去的人。

Now Lets think how we would like to implement it.

现在让我们想想我们如何实现它。

First choice: Just have an fixed size internal array. In that case, you will never have the option to resize on the fly. So the comment above stack init is valid, that you should allocate a size that you think will be safe for the purpose.

首选:只需一个固定大小的内部阵列。在这种情况下,您永远不会选择动态调整大小。所以堆栈init上面的注释是有效的,你应该分配一个你认为对于此目的是安全的大小。

Second Choice: Having an array, but using dynamic memory. In that case, even if you hit the limit of existing stack, you always can expand the size by realloc. So comment does not make sense.

第二选择:拥有一个数组,但使用动态内存。在这种情况下,即使您达到了现有堆栈的限制,也始终可以通过realloc扩展大小。所以评论没有意义。

Third Choice: Using linklist, So in theory, even if you initialize the stack with 0 size, it should expand on each node insertion, so the comment is misleading. Just to add, top is always the head of the linklist, and with every insertion, new node will become new head.

第三种选择:使用linklist,理论上,即使你用0大小初始化堆栈,它也应该在每个节点插入时扩展,因此注释具有误导性。只需添加,top始终是linklist的头部,每次插入时,新节点都将成为新的头。

So to answer your question, the comment above is confusing and make sense only when internal implementation is array based.

所以要回答你的问题,上面的评论只是在内部实现是基于数组时才会引起混淆和有意义。

But in common sense and general perception associated to Stack DS, Stack is DS which is always associated to Stack Depth. And when implementing this, its always safe to have max limit of elements that can be pushed and I guess, may be comment meant that.

但是在与Stack DS相关的常识和一般感知中,Stack是DS,它始终与Stack Depth相关联。在实现这一点时,它总是安全的,可以推送的元素的最大限制,我猜,可能是评论意味着。

To further illustrate it with an real example, you must have heard of callstack of functions, though in theory its expands but has MAX possible limit and thats the reason we see stack overflow error when we do infinite recursion.

为了用一个真实的例子进一步说明它,你必须听说过函数的callstack,虽然理论上它的扩展但是有MAX可能的限制,这就是我们在进行无限递归时看到堆栈溢出错误的原因。

#1


1  

When you initialize list to be length of the string you want to read, you will still have stack pointer pointing to the first element of the list. So basically nothing is lost. However you are correct, there is no point of doing something like that.

当您将list初始化为要读取的字符串的长度时,您仍然会将堆栈指针指向列表的第一个元素。所以基本上没有什么丢失。无论你是对的,没有必要做那样的事情。

There is no need for double linked list. Stack pointer will always point to the first element. Basically whenever you want to push() you will add new node to the beginning of the list, and whenever you want to pop() you will remove first node of the list.

不需要双链表。堆栈指针始终指向第一个元素。基本上每当你想推送()时,你会将新节点添加到列表的开头,每当你想要pop()时,你将删除列表的第一个节点。

#2


1  

Assume stack signifies LIFO operation only. i.e last in will be the first to get out.

假设堆栈仅表示LIFO操作。即将是第一个出去的人。

Now Lets think how we would like to implement it.

现在让我们想想我们如何实现它。

First choice: Just have an fixed size internal array. In that case, you will never have the option to resize on the fly. So the comment above stack init is valid, that you should allocate a size that you think will be safe for the purpose.

首选:只需一个固定大小的内部阵列。在这种情况下,您永远不会选择动态调整大小。所以堆栈init上面的注释是有效的,你应该分配一个你认为对于此目的是安全的大小。

Second Choice: Having an array, but using dynamic memory. In that case, even if you hit the limit of existing stack, you always can expand the size by realloc. So comment does not make sense.

第二选择:拥有一个数组,但使用动态内存。在这种情况下,即使您达到了现有堆栈的限制,也始终可以通过realloc扩展大小。所以评论没有意义。

Third Choice: Using linklist, So in theory, even if you initialize the stack with 0 size, it should expand on each node insertion, so the comment is misleading. Just to add, top is always the head of the linklist, and with every insertion, new node will become new head.

第三种选择:使用linklist,理论上,即使你用0大小初始化堆栈,它也应该在每个节点插入时扩展,因此注释具有误导性。只需添加,top始终是linklist的头部,每次插入时,新节点都将成为新的头。

So to answer your question, the comment above is confusing and make sense only when internal implementation is array based.

所以要回答你的问题,上面的评论只是在内部实现是基于数组时才会引起混淆和有意义。

But in common sense and general perception associated to Stack DS, Stack is DS which is always associated to Stack Depth. And when implementing this, its always safe to have max limit of elements that can be pushed and I guess, may be comment meant that.

但是在与Stack DS相关的常识和一般感知中,Stack是DS,它始终与Stack Depth相关联。在实现这一点时,它总是安全的,可以推送的元素的最大限制,我猜,可能是评论意味着。

To further illustrate it with an real example, you must have heard of callstack of functions, though in theory its expands but has MAX possible limit and thats the reason we see stack overflow error when we do infinite recursion.

为了用一个真实的例子进一步说明它,你必须听说过函数的callstack,虽然理论上它的扩展但是有MAX可能的限制,这就是我们在进行无限递归时看到堆栈溢出错误的原因。