C 在链表中插入
在构建链表时,插入包含字符串中所有字符的完整句子有点困难 我希望能够插入如下字符串:C 在链表中插入,c,linked-list,C,Linked List,在构建链表时,插入包含字符串中所有字符的完整句子有点困难 我希望能够插入如下字符串:word#u 2003#u#u definition 但是,当我在main()方法中运行代码时,它会继续重复选择,就像它从未停止要求我输入选项一样。希望一切都清楚 这是我的结构: struct node { char data[100]; struct node *previous; // Points to the previous node struct node *next;
word#u 2003#u#u definition
但是,当我在main()
方法中运行代码时,它会继续重复选择,就像它从未停止要求我输入选项一样。希望一切都清楚
这是我的结构
:
struct node
{
char data[100];
struct node *previous; // Points to the previous node
struct node *next; // Points out to the next node
} *head, *last;
以下是插入节点的函数:
void insert_beginning(char words[99])
{
struct node *var, *temp;
var=(struct node *)malloc(sizeof(struct node)); //explination about the (node *)
strncpy(var->data, words,99);
if (head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
last=head;
}
else
{
temp=var;
temp->previous=NULL;
temp->next=head;
head->previous=temp;
head=temp;
}
}
这是在我的main()
方法中:
int main()
{
char loc[99];
char words[99];
int i, dat;
head=NULL;
printf("Select the choice of operation on link list");
printf("\n1.) Insert At Begning\n2.) Insert At End\n3.) Insert At Middle");
printf("\n4.) Delete From End\n5.) Reverse The Link List\n6.) Display List\n7.)Exit");
while(1)
{
printf("\n\n Enter the choice of operation you want to do ");
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("Enter a word you want to insert in the 1st node ");
scanf(" %s",words);
insert_beginning(words);
display();
break;
}
有什么办法吗?代码很可疑:
- 返回代码从不检查。您必须检查返回代码, 尤其是在使用scanf时
- 您需要使用scanf清空整个缓冲区,否则将继续 正在为下一个命令读取旧内容
- 更好的选择是使用sscanf
- 您必须初始化您的变量,例如最后一个,但是还有其他的变量 未初始化的变量的情况
- 您的数据类型定义不一致,这将造成安全问题 有时是99个字符,有时是100个字符
- insert_开头不应返回void,内存分配可能会失败
main()
中的代码应该看起来更像:
int main()
{
char loc[99];
char words[99];
int i, dat;
head = NULL;
printf("Select the choice of operation on link list");
printf("\n1.) Insert At Beginning\n2.) Insert At End\n3.) Insert At Middle");
printf("\n4.) Delete From End\n5.) Reverse The Link List\n6.) Display List\n7.) Exit\n");
while(1)
{
printf("\nEnter the choice of operation you want to do: ");
if (scanf("%d", &i) != 1)
{
fprintf(stderr, "Failed to read a number: exiting\n");
return 1;
}
switch(i)
{
case 1:
{
printf("Enter a word you want to insert in the 1st node: ");
if (scanf("%98s", words) != 1)
{
fprintf(stderr, "Failed to read words; exiting\n");
return 1;
}
insert_beginning(words);
display();
break;
}
...
}
...
}
...
}
return 0;
}
正如在注释中所讨论的,您没有从scanf()
检查返回状态,因此您不知道它何时失败,当它读取数字失败时,它只保留参数(i
),因此您再次返回同一选项,然后冲洗并重复
基本调试技术(未显示):
scanf()
错误检查后,打印i
的值。在执行scanf()
错误检查后,打印单词的值
static void dump_data_structure(FILE *fp, char const *tag, data_structure const *data)
{
...code to dump the structure to the specified file stream,
...identified by tag (so you can tell which call it is you are looking at)
}
你的代码不完整!可能是因为while循环?while循环的逻辑在哪里?@keshlam他没有包括全部代码!你看不到开关和while注释关闭了吗?你的问题是
scanf()
只读取一个空格分隔的单词,并留下第二个,然后数字的scanf()
无法转换(但你没有检查scanf()
的返回,所以你不知道这一点),然后返回到开关/case
读取下一个单词,等等。这不是你想的,而是你写的。这段代码仍然有问题。如果用户在一行中输入了多个单词,它将忽略其他单词,或者如果不走运,则使用字符串作为命令。@史蒂夫:是的——修改后的代码并不完美,但它将检查scanf()
的返回值,并提前排除故障。可以修改代码以使用fgets()
和sscanf()
,以及其他替代方法,但目前这可能对他没有多大帮助(他仍然需要检查输入函数是否存在错误)。他会打字(在一行1 Word 2 Alter 3 Ego 4 7
上,程序在退出之前将经历一系列操作,所有操作都来自一行输入。这是代码当前的设计方式;我没有修改它以其他方式进行。由于输入代码有99个,而loner term存储结构有100个,因此在输入期间没有问题操作。如果将值从数据结构提取到99个字符的缓冲区中,如果额外的字节被利用而使字符串过长,则可能会出现问题。请不要误会:一致性在一般编程中非常重要,但风险很低。