C、打印字符串链表
我必须编写一个使用链表的 C 程序。我创建了一个列表并向列表中添加了元素。但我不知道如何打印列表中的所有元素。该列表是字符串列表。我想我应该以某种方式增加列表,打印那里的每个字符串,但我无法找到一种方法来做到这一点。
简而言之:如何打印链接列表
?
I have to write a C program that uses a linked list. I have created a list and added elements to the list. But I don't know how to print all the elements in the list. The list is a list of strings. I figured I'd somehow increment through the list, printing every string that's there, but I can't figure out a way to do this.
Short: How to I print a linked list
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有愚蠢的问题1。这里有一些伪代码可以帮助您入门:
确实如此,只需从头节点开始,打印出有效负载并移至列表中的下一个节点。
一旦下一个节点是列表的末尾,就停止。
1 嗯,实际上,可能有,但这不是其中之一:-)
There are no stupid questions1. Here's some pseudo-code to get you started:
That's it really, just start at the head node, printing out the payload and moving to the next node in the list.
Once that next node is the end of the list, stop.
1 Well, actually, there probably are, but this isn't one of them :-)
您可以使用指针来遍历链接列表。伪代码:
You can use a pointer to iterate through the link list. Pseudo code:
伪代码:
pseudo code:
我不太确定这是否是您要寻找的,但通常您会在 DS 中存储 pHead (即指向第一个元素的指针),并实现一个检索字符串下一个地址的函数 -节点。
这样做直到下一个地址为 NULL(这意味着您已经到达尾部)。
I'm not quite sure if this is what you're looking for, but usually you store in your DS, a pHead (that is a pointer to the first element), and implement a function that retrieves the next address of the string-node.
You do this until the next address is NULL (which means you've reached your tail).