【发布时间】:2019-07-28 21:46:24
【问题描述】:
我的任务是用 C 语言创建一个将用户输入插入简单链表的程序,但有以下限制:
- 节点必须按降序排序;
- 只允许使用头节点监控(不允许监控尾节点)。
这是我的插入函数的当前代码:
void insert(struct snode *node, struct snode *aux) {
if (aux) {
if (node->n >= monitor.head->n) {
node->next = monitor.head;
monitor.head = node;
return;
}
if (node->n < aux->n) {
node->next = aux->next;
aux->next = node;
return;
} else
insert(node, aux->next);
}
}
我遇到的问题是:如果我输入,例如:5 然后9 然后1,列表最终将被排序为9 -> 1 -> @987654327 @ -> NULL,应该是9 -> 5 -> 1 -> NULL。我在这里错过了什么?因为我尝试了我能想到的一切。
这是完整的程序,以防有帮助:
#include <stdio.h>
#include <stdlib.h>
/* GLOBAL VARS */
struct snode {
int n;
struct snode *next;
};
struct smonitor {
struct snode *head;
} monitor;
/* FUNCTION PROTOTYPING */
void insert(struct snode *node, struct snode *aux);
void search(int s, struct snode *aux);
struct snode *aloc(int p);
void print(struct snode *aux);
void readInputFile();
/* MAIN LOOP */
int main() {
int p, s;
int opt;
_Bool endMain = 1;
while (endMain == 1) {
struct snode *aux = monitor.head;
printf("Define Option:\n0-Exit\n1-Insert\n2-Search\n3-Print\n");
scanf("%d", &opt);
switch (opt) {
case 0:
endMain = 0;
break;
case 1:
printf("Define node:\n");
scanf("%d", &p);
struct snode *node = aloc(p);
if (monitor.head == NULL)
monitor.head = node;
else
insert(node, aux);
break;
case 2:
printf("Define search term:\n");
scanf("%d", &s);
search(s, aux);
break;
case 3:
printf("List is:\n");
print(aux);
printf("[NULL]\n");
break;
case 4:
readInputFile();
break;
default:
printf("INVALID OPTION\n");
}
}
return 0;
}
/* FUNCTIONS */
void insert(struct snode *node, struct snode *aux) {
if (aux) {
if (node->n >= monitor.head->n) {
node->next = monitor.head;
monitor.head = node;
return;
}
if (node->n < aux->n) {
node->next = aux->next;
aux->next = node;
return;
} else
insert(node, aux->next);
}
}
void search(int s, struct snode *aux) {
if (aux) {
if (s == aux->n)
printf("HIT - Node %d found\n", aux->n);
else
search(s, aux->next);
} else
printf("NO HIT - Node not found\n");
}
struct snode *aloc(int p) {
struct snode *node;
node = (struct snode *)malloc(sizeof(struct snode));
node->n = p;
node->next = NULL;
return node;
}
void print(struct snode *aux) {
if (aux) {
printf("[%d]-", aux->n);
print(aux->next);
}
}
void readInputFile() {
FILE *fp;
int input;
struct snode *p;
struct snode *aux;
fp = fopen("/home/user/inputFile.txt", "r");
printf("Nodes added:\n");
while (fscanf(fp, "%d", &input) != EOF) {
p = aloc(input);
aux = monitor.head;
if (monitor.head == NULL)
monitor.head = p;
else
insert(p, aux);
printf("[%d]-", input);
}
printf("\n");
fclose(fp);
}
提前感谢你们提供的任何帮助! :D
/------------------编辑----------- -------------------------/ 在你们的反馈和一些测试之后,我设法找到了一个解决方案,可能不是它的最佳实现,并且还有其他不同实现的答案(非常感谢!:))在那里,但这是该插入的新版本功能,再次感谢大家的提示! :D
void insert (struct snode *node, struct snode *aux, struct snode *pre){
if(aux){
if(node->n >= monitor.head->n){
node->next = monitor.head;
monitor.head = node;
return;
}
if((pre->n >= node->n) & (node->n >= aux->n)){
pre->next = node;
node->next = aux;
return;
}
if((pre->n >= node->n) & (aux->next == NULL)){
aux->next = node;
return;
}
insert(node, aux->next, aux);
}
}
【问题讨论】:
-
“我想尽了所有办法。”:你有没有想过用调试器单步调试你的代码?
标签: c linked-list singly-linked-list