我在使用链表操作某些内容时遇到了困难,
问题的第一件事就是代码的这一部分,我从文件中读取数据,并用它填充节点。但是我在代码中指示的地方不断收到警告,分别将指针类型int与int和不兼容的指针类型进行比较:
void readfile(node *curr, node *newlist, int *quanta, int *readincrement, int *newincrement, int *procnumb)
{
int i;
FILE *myFile;
myFile = fopen("data.txt", "r");
fscanf(myFile,"%d",quanta);
fscanf(myFile,"%d",newincrement);
fscanf(myFile,"%d",readincrement);
fscanf(myFile,"%d",procnumb);
newlist = malloc(sizeof(node));
curr = newlist;
for(i=0;i<procnumb;i++)//pointer error
{
curr->process = 'A' + i;
printf("%s",curr->process);
curr->state = '-';
printf("%s",curr->state);
fscanf(myFile,"%d",&curr->NUT);
printf("%d",curr->NUT);
fscanf(myFile,"%d",&curr->AT);
printf("%d", curr->AT);
curr->Prio = '0';
printf("%d",curr->Prio);
curr->next = malloc(sizeof (node));
curr->next = curr;//pointer error.
}
}
第二个问题是具有将节点从
newlist
的顶部移到readylist
的底部的功能,我在代码中指示的位置不断收到错误,我也不知道为什么://move node from new list, to ready list.
void move(node *newlist, node *readylist)
{
readylist->next = malloc(sizeof(node));
while(readylist->next != NULL)
{
if(readylist->next == newlist)
readylist->next = readylist->next->next;//pointer error here.
readylist = readylist->next;
}
readylist->next = newlist;
newlist->next = NULL;
return readylist;
}
在您的回答中,我希望能简要解释它们为什么会发生,目前我确实在努力解决这个问题,并努力解决这个问题。
编辑:忘记在这里添加结构部分:
struct record//too contain several data types.
{
char process;
char state;
int NUT;
int AT;
int Prio;
struct reord *next;
};
typedef struct record node;
void move(node *newlist, node *readylist);
void readfile(node *reading,node *newlist, int *quanta, int *readincrement, int *newincrement, int *procnumb);
main()
{
int quanta, readincrement, newincrement, procnumb;
node *readylist = {NULL};//pointing at nothing.
node *newlist = {NULL};
node *curr = {NULL};
node *next = {NULL};
node *Rcurr = {NULL};
node *Rnext = {NULL};
}
最佳答案
对于问题1:
更换for(i=0;i<procnumb;i++)
\这里您正在比较一个整数和一个指向整数的地址
与for(i=0;i<*procnumb;i++)
\现在您正在比较一个整数和一个整数
关于c - 链接列表,操作困难,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28629227/