脑袋都想破了,这也太难了....

weixin_45906870 2020-06-02 05:12:19
#include <stdio.h>
#include <stdlib.h>

struct Node
{
int value;
struct Node *next;
};

void insertNode(struct Node **head, int value)
{
struct Node *previous;
struct Node *current;
struct Node *new;

current = *head;
previous = NULL;

while (current != NULL && current->value < value)
{
previous = current;
current = current->next;

}

new = (struct Node *)malloc(sizeof(struct Node));
if (new == NULL)
{
printf("内存分配失败!\n");
exit(1);
}

new->value = value;
new->next = current;

if (previous == NULL)
{
*head = new;
}
else
{
previous->next = new;
}
}

void printNode(struct Node *head)
{
struct Node *current;

current = head;
while (current != NULL)
{
printf("%d ", current->value);
current = current->next;
}

putchar('\n');
}

void deleteNode(struct Node **head, int value)
{
struct Node *previous;
struct Node *current;

current = *head;
previous = NULL;

while (current != NULL && current->value != value)
{
previous = current;
current = current->next;
}

if (current == NULL)
{
printf("找不到匹配的节点!\n");
return ; //为什么这里写成return 0会出错,写成return不会出错?这里的return有什么用?
}
else
{
if (previous == NULL)
{
*head = current->next;
}
else
{
previous->next = current->next;
}

free(current);
}
}
int main()
{
struct Node *head = NULL;
int input;

printf("开始测试插入整数...\n");
while (1)
{
printf("请输入一个整数(输入-1表示结束):");
scanf("%d", &input);
if (input == -1)
{
break;
}
insertNode(&head, input);
printNode(head);
}

printf("开始测试删除整数...\n");
while (1)
{
printf("请输入一个整数(输入-1表示结束):");
scanf("%d", &input);
if (input == -1)
{
break;
}
deleteNode(&head, input);
printNode(head);
}

return 0;
}
...全文
98 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_45906870 2020-06-03
  • 打赏
  • 举报
回复
引用 4 楼 暁暁暁 的回复:
你写成 return 0会出错是因为你的函数是void
可是main函数也是void类型的,它却可以return 0,为什么?
weixin_45906870 2020-06-03
  • 打赏
  • 举报
回复
引用 5 楼 自信男孩 的回复:
#include <stdio.h>
#include <stdlib.h>

struct Node
{
int value;
struct Node *next;
};

void insertNode(struct Node **head, int value)
{
struct Node *previous;
struct Node *current;
//struct Node *new;
struct Node *new_node;

current = *head;
previous = NULL;

while (current != NULL && current->value < value)
{
previous = current;
current = current->next;

}

new_node = (struct Node *)malloc(sizeof(struct Node));
if (new_node == NULL)
{
printf("内存分配失败!\n");
exit(1);
}

new_node->value = value;
new_node->next = current;

if (previous == NULL)
{
*head = new_node;
}
else
{
previous->next = new_node;
}
}

void printNode(struct Node *head)
{
struct Node *current;

current = head;
while (current != NULL)
{
printf("%d ", current->value);
current = current->next;
}

putchar('\n');
}

void deleteNode(struct Node **head, int value)
{
struct Node *previous;
struct Node *current;

current = *head;
previous = NULL;

while (current != NULL && current->value != value)
{
previous = current;
current = current->next;
}

if (current == NULL)
{
printf("找不到匹配的节点!\n");
return ; //为什么这里写成return 0会出错,写成return不会出错?这里的return有什么用?
}
else
{
if (previous == NULL)
{
*head = current->next;
}
else
{
previous->next = current->next;
}

free(current);
}
}
int main()
{
struct Node *head = NULL;
int input;

printf("开始测试插入整数...\n");
while (1)
{
printf("请输入一个整数(输入-1表示结束):");
scanf("%d", &input);
if (input == -1)
{
break;
}
insertNode(&head, input);
printNode(head);
}

printf("开始测试删除整数...\n");
while (1)
{
printf("请输入一个整数(输入-1表示结束):");
scanf("%d", &input);
if (input == -1)
{
break;
}
deleteNode(&head, input);
printNode(head);
}

return 0;
}

供参考~

函数返回值是值函数定义的数据类型,如果没有返回值,即定义的是void也是可以使用return的,只是return什么也不返回,直接跟分号。
return是退出函数并返回对应类型的数据。
那为什么void类型的main函数可以用return返回零?零不是int类型的吗?
自信男孩 2020-06-02
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>

struct Node
{
int value;
struct Node *next;
};

void insertNode(struct Node **head, int value)
{
struct Node *previous;
struct Node *current;
//struct Node *new;
struct Node *new_node;

current = *head;
previous = NULL;

while (current != NULL && current->value < value)
{
previous = current;
current = current->next;

}

new_node = (struct Node *)malloc(sizeof(struct Node));
if (new_node == NULL)
{
printf("内存分配失败!\n");
exit(1);
}

new_node->value = value;
new_node->next = current;

if (previous == NULL)
{
*head = new_node;
}
else
{
previous->next = new_node;
}
}

void printNode(struct Node *head)
{
struct Node *current;

current = head;
while (current != NULL)
{
printf("%d ", current->value);
current = current->next;
}

putchar('\n');
}

void deleteNode(struct Node **head, int value)
{
struct Node *previous;
struct Node *current;

current = *head;
previous = NULL;

while (current != NULL && current->value != value)
{
previous = current;
current = current->next;
}

if (current == NULL)
{
printf("找不到匹配的节点!\n");
return ; //为什么这里写成return 0会出错,写成return不会出错?这里的return有什么用?
}
else
{
if (previous == NULL)
{
*head = current->next;
}
else
{
previous->next = current->next;
}

free(current);
}
}
int main()
{
struct Node *head = NULL;
int input;

printf("开始测试插入整数...\n");
while (1)
{
printf("请输入一个整数(输入-1表示结束):");
scanf("%d", &input);
if (input == -1)
{
break;
}
insertNode(&head, input);
printNode(head);
}

printf("开始测试删除整数...\n");
while (1)
{
printf("请输入一个整数(输入-1表示结束):");
scanf("%d", &input);
if (input == -1)
{
break;
}
deleteNode(&head, input);
printNode(head);
}

return 0;
}

供参考~

函数返回值是值函数定义的数据类型,如果没有返回值,即定义的是void也是可以使用return的,只是return什么也不返回,直接跟分号。
return是退出函数并返回对应类型的数据。
暁暁暁 2020-06-02
  • 打赏
  • 举报
回复
你写成 return 0会出错是因为你的函数是void
暁暁暁 2020-06-02
  • 打赏
  • 举报
回复
引用 2 楼 weixin_45906870 的回复:
引用 1 楼 Simple-Soft 的回复:
因为这个函数没有返回值
那这个return是结束这个函数的意思吗
对的,return就是结束本函数的意思,同时返回一个值。
weixin_45906870 2020-06-02
  • 打赏
  • 举报
回复
引用 1 楼 Simple-Soft 的回复:
因为这个函数没有返回值
那这个return是结束这个函数的意思吗
Simple-Soft 2020-06-02
  • 打赏
  • 举报
回复
因为这个函数没有返回值

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧