C、打印字符串链表

发布于 2024-10-06 08:33:49 字数 151 浏览 1 评论 0原文

我必须编写一个使用链表的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

余生共白头 2024-10-13 08:33:49

没有愚蠢的问题1。这里有一些伪代码可以帮助您入门:

def printAll (node):
    while node is not null:
        print node->payload
        node = node->next

printAll (head)

确实如此,只需从头节点开始,打印出有效负载并移至列表中的下一个节点。

一旦下一个节点是列表的末尾,就停止。


1 嗯,实际上,可能,但这不是其中之一:-)

There are no stupid questions1. Here's some pseudo-code to get you started:

def printAll (node):
    while node is not null:
        print node->payload
        node = node->next

printAll (head)

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 :-)

要走就滚别墨迹 2024-10-13 08:33:49

您可以使用指针来遍历链接列表。伪代码:

tempPointer = head

while(tempPointer not null) {
  print tempPointer->value;
  tempPointer = tempPointer->next;
}

You can use a pointer to iterate through the link list. Pseudo code:

tempPointer = head

while(tempPointer not null) {
  print tempPointer->value;
  tempPointer = tempPointer->next;
}
鹊巢 2024-10-13 08:33:49

伪代码:

struct list
{
  type value;
  struct list* pNext;
}

void function()
{
  struct list L;
  // .. element to L

  // Iterate each node and print
  struct list* node = &L;

  do
  {
    print(node->value)
    node = node->next;
  }
  while(node != NULL)
}

pseudo code:

struct list
{
  type value;
  struct list* pNext;
}

void function()
{
  struct list L;
  // .. element to L

  // Iterate each node and print
  struct list* node = &L;

  do
  {
    print(node->value)
    node = node->next;
  }
  while(node != NULL)
}
我恋#小黄人 2024-10-13 08:33:49

我不太确定这是否是您要寻找的,但通常您会在 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文