鱼C论坛

 找回密码
 立即注册
查看: 3561|回复: 1

[已解决]单链表

[复制链接]
发表于 2022-12-18 20:22:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
为什么节点没有链接起来
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Node
  4. {
  5.         int value;
  6.         struct Node *next;
  7. };
  8. void get(struct Node *n)
  9. {
  10.         scanf("%d",&n->value);
  11. }
  12. void insertNode(struct Node **head)
  13. {
  14.         struct Node *previous;
  15.         struct Node *current;
  16.         struct Node *n;

  17.         current = *head;
  18.         previous = NULL;
  19.         
  20.         n = (struct Node *)malloc(sizeof(struct Node));
  21.         get(n);
  22.         if(n->value==-1)
  23.         {
  24.                 exit(1);
  25.                 }
  26.         if(*head==NULL);
  27.         {
  28.                 *head=n;
  29.                 n->next=NULL;
  30.                 return;
  31.                 }
  32.         
  33.         while(current!=NULL&&(current->value)<(n->value))
  34.         {
  35.                 previous=current;
  36.                 current=current->next;
  37.                 }
  38.                 if(previous==NULL)
  39.                 {
  40.                         *head=n;
  41.                 }
  42.                 else
  43.                 {
  44.                         previous->next = n;
  45.                 }
  46.                 n->next=current;
  47. }
  48. void printNode(struct Node *head)
  49. {
  50.         struct Node *current;

  51.         current = head;
  52.         while (current != NULL)
  53.         {
  54.                 printf("%d ", current->value);
  55.                 current = current->next;
  56.         }

  57.         putchar('\n');
  58. }
  59. int main(void)
  60. {
  61.         struct Node *head = NULL;

  62.         printf("开始测试插入整数...\n");
  63.         while (1)
  64.         {
  65.                 printf("请输入一个整数(输入-1表示结束):");
  66.                 insertNode(&head);
  67.                 printNode(head);
  68.         }
  69.         
  70.         return 0;
  71. }
复制代码
最佳答案
2022-12-19 18:55:48
  1. if(*head==NULL);
  2.         {
  3.                 *head=n;
  4.                 n->next=NULL;
  5.                 return;
  6.                 }
复制代码

把这个去掉
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-19 18:55:48 | 显示全部楼层    本楼为最佳答案   
  1. if(*head==NULL);
  2.         {
  3.                 *head=n;
  4.                 n->next=NULL;
  5.                 return;
  6.                 }
复制代码

把这个去掉
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 22:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表