当前位置:网站首页 > C++编程 > 正文

c++单向链表反转(反转单向链表c语言)

以下是

C语言

中将新节点添加

链表

尾部的示例代码:

 #include <stdio.h> #include <stdlib.h>  // 定义 链表 结构体 struct Node { int data; struct Node* next; };  // 在 链表 尾部添加新节点 void append(struct Node head_ref, int new_data) { // 为新节点分配内存 struct Node* new_node = (struct Node*)malloc(sizeof(struct Node)); struct Node* last = *head_ref; // 用于遍历 链表 // 设置新节点的值和下一个节点为NULL new_node->data = new_data; new_node->next = NULL;  // 如果 链表 为空,则将新节点设置为头节点 if (*head_ref == NULL) { *head_ref = new_node; return; }  // 遍历 链表 ,找到最后一个节点 while (last->next != NULL) { last = last->next; }  // 将新节点添加到 链表 尾部 last->next = new_node; return; }  // 测试代码 int main() { struct Node* head = NULL; // 初始化 链表 为空  // 添加节点 append(&head, 1); append(&head, 2); append(&head, 3);  // 遍历 链表 并输出每个节点的值 struct Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; }  return 0; } 
到此这篇c++单向链表反转(反转单向链表c语言)的文章就介绍到这了,更多相关内容请继续浏览下面的相关 推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • cp215w连接手机(cp1525n连接网络)2025-02-19 19:54:09
  • cmake中文手册pdf(cmake中文手册)2025-02-19 19:54:09
  • uc代码是什么意思(uc代码怎么用)2025-02-19 19:54:09
  • ceph存储(ceph存储原理)2025-02-19 19:54:09
  • console接口是干嘛的(交换机的console连接线)2025-02-19 19:54:09
  • pic头文件(pipe头文件)2025-02-19 19:54:09
  • msvcp140d.dll丢失(msvcp140.dll丢失的解决方法视频)2025-02-19 19:54:09
  • cnn是监督还是非监督(cnn是监督还是非监督的)2025-02-19 19:54:09
  • cmake多级目录(cmake添加目录下所有的文件)2025-02-19 19:54:09
  • apc和安乃近哪个好(apc与安乃近哪个副作用大)2025-02-19 19:54:09
  • 全屏图片