链接列表,操纵困难

时间:2015-02-20 12:38:23

标签: c error-handling linked-list

我在使用链接列表操作一些东西时遇到了困难,

我遇到问题的第一件事就是我的代码的这一部分,我从文件中读取数据,并用它填充我的节点。但是我不断收到警告,我在代码上指出了将指针类型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 个答案:

答案 0 :(得分:1)

问题1:

替换

for(i=0;i<procnumb;i++) \这里您要比较整数和指向整数

的地址

for(i=0;i<*procnumb;i++) \现在您要将整数与整数进行比较