双向链表的遍历

5 Sept 2024 | 2 分钟阅读

遍历是每种数据结构中最常见的操作。为此,将头部指针复制到任何临时指针 ptr。

然后,通过 while 循环遍历列表。不断移动指针变量 **ptr**,直到找到最后一个节点。最后一个节点在其 next 部分包含 **null**。

虽然遍历意味着访问列表中的每个节点一次以执行某些特定操作。这里,我们正在打印列表中每个节点关联的数据。

算法

  • 步骤 1: 如果 HEAD == NULL
  •   写“下溢”
     转到步骤 6
     [IF 结束]

  • 步骤 2: 设置 PTR = HEAD
  • 步骤 3: 当 PTR != NULL 时重复步骤 4 和 5
  • 步骤 4: 写 PTR → data
  • 步骤 5: PTR = PTR → next
  • 步骤 6: 退出

C 函数

输出

1.Append List
2.Traverse
3.Exit
4.Enter your choice?1

Enter the item
23

Node Inserted
1.Append List
2.Traverse
3.Exit
4.Enter your choice?1

Enter the item
23

Press 0 to insert more ?

Node Inserted
1.Append List
2.Traverse
3.Exit
4.Enter your choice?1

Enter the item
90

Press 0 to insert more ?

Node Inserted
1.Append List
2.Traverse
3.Exit
4.Enter your choice?2
90
23
23

下一个主题双向链表