链表赢了';不打印?C
因此,我的任务是接收文本文件中的字母(每行6个),将其保存到链接列表中,然后打印出特定“die”(在本例中)的信息,即每组6个字母 我已经在链表赢了';不打印?C,c,function,linked-list,C,Function,Linked List,因此,我的任务是接收文本文件中的字母(每行6个),将其保存到链接列表中,然后打印出特定“die”(在本例中)的信息,即每组6个字母 我已经在readData函数中正确读取了(我想)数据,但我不确定链表是否正确传递。当我试图在main(第40-41行)中打印链接列表时,我一无所获。我是否将函数错误地传递给readData 这是我的密码: #include <string.h> #include <stdlib.h> #include <stdio.h> #de
readData
函数中正确读取了(我想)数据,但我不确定链表是否正确传递。当我试图在main
(第40-41行)中打印链接列表时,我一无所获。我是否将函数错误地传递给readData
这是我的密码:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define LENGTH 80
struct boggleDataNode {
char data[3];
struct boggleDataNode *nextData;
};
struct boggleDieSideNode{
char dieSideData[3];
struct boggleDieSideNode *nextSide;
};
void readData(struct boggleDataNode *temp);
void printData(struct boggleDataNode *temp);
int main() {
int counter = 0;
struct boggleDataNode *head;
struct boggleDieSideNode *head1;
struct boggleDataNode *temp;
head = NULL;
head1 = NULL;
temp = head;
printf("test\n\n");
readData(&temp);
// printData(temp);
while(counter = 0, counter<100, counter++)
printf("%s ", temp->data[counter]);
system("pause");
return 0;
}
void readData(struct boggleDataNode *temp) {
//initializing variables including
//opening the input file
FILE *input = fopen("BoggleData.txt","r");
char data[96] = {};
struct boggleDataNode *new;
int counter = 0;
temp = new;
//error checking that the file opened
if (input == NULL) {
fprintf(stderr, "Can't open input file!\n");
exit(1);
}
new = (struct boggleDataNode *)malloc(sizeof(struct boggleDataNode));
while (fscanf(input, "%s", data) != EOF) {
printf("%s ", data);
strcpy(temp->data, data);
printf("Data number %d %s \n",counter,temp->data);
temp->nextData = NULL;
counter++;
}
}
#包括
#包括
#包括
#定义长度80
结构boggleDataNode{
字符数据[3];
结构boggleDataNode*nextData;
};
结构boggleDieSideNode{
chardiesidedata[3];
结构boggleDieSideNode*nextSide;
};
void readData(struct boggleDataNode*temp);
void printData(struct boggleDataNode*temp);
int main(){
int计数器=0;
结构boggleDataNode*head;
结构boggleDieSideNode*head1;
结构boggleDataNode*temp;
head=NULL;
head1=NULL;
温度=水头;
printf(“测试\n\n”);
读取数据(&temp);
//打印数据(temp);
而(计数器=0,计数器数据[计数器]);
系统(“暂停”);
返回0;
}
void readData(结构boggleDataNode*temp){
//初始化变量,包括
//打开输入文件
FILE*input=fopen(“BoggleData.txt”、“r”);
字符数据[96]={};
结构boggleDataNode*新建;
int计数器=0;
温度=新的;
//检查文件是否已打开时出错
如果(输入==NULL){
fprintf(stderr,“无法打开输入文件!\n”);
出口(1);
}
new=(struct boggleDataNode*)malloc(sizeof(struct boggleDataNode));
while(fscanf(输入,“%s”,数据)!=EOF){
printf(“%s”,数据);
strcpy(临时->数据,数据);
printf(“数据编号%d%s\n”,计数器,临时->数据);
temp->nextData=NULL;
计数器++;
}
}
您将结构错误地传递给了readData
。您可以这样声明变量:
struct boggleDataNode *temp;
通过调用readData
:
readData(&temp);
您将readData
声明为:
void readData(struct boggleDataNode *temp){
因此,您将temp
和readData
的输入声明为struct boggleDataNode*
,但将&temp
传递给readData
&
表示您正在发送以下内容的地址,因此&temp
是指向struct boggleDataNode
的指针,它是struct boggleDataNode**
代码还有其他问题,但这超出了问题的范围。我要指出,您的输出循环是非常非常错误的。我看不出您在
main
中迭代链接列表的位置-非常确定应该存在某种while(temp->next!=NULL){temp=temp->next;}
@M.Shaw这里是我尝试的地方:while(counter=0,counterdata[counter])代码>您正在打印temp->data[0]
,temp->data[1]
。。。到temp->data[99]
那里;我看不到您要移动到下一个链表节点的位置。@bravesainttemp->data
范围是0,1,2
,因为char data[3]代码>。另外,temp->data[counter]
的类型是char
@M.Shaw crap。。。我才明白你的意思。。。我刚刚开始学习链表,完全是把它们当作一个数组。我假设我在readData
文件函数中正确地保存了它?我对链表还是非常、非常陌生的,并且被教导将结构声明为*name
,并且被告知将结构作为指针传递。。。我想一切都让我很困惑。。。谢谢你的回复,我会试着找出到底是什么让我如此困惑并付诸实施。