#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;
}
uj5u.com熱心網友回復:
因為這個函式沒有回傳值uj5u.com熱心網友回復:
那這個return是結束這個函式的意思嗎uj5u.com熱心網友回復:
對的,return就是結束本函式的意思,同時回傳一個值。
uj5u.com熱心網友回復:
你寫成 return 0會出錯是因為你的函式是voiduj5u.com熱心網友回復:
#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是退出函式并回傳對應型別的資料。
uj5u.com熱心網友回復:
#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是退出函式并回傳對應型別的資料。
uj5u.com熱心網友回復:
你寫成 return 0會出錯是因為你的函式是void
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/52063.html
標籤:C語言
上一篇:大佬們能幫忙分析下代碼嘛,(比如注釋什么的)萌新十分感謝,有償
下一篇:順序佇列某個部分寫法的疑惑,感謝
