简体   繁体   English

链接列表突然更改C中的值

[英]Linked list suddenly changes value in C

I am making a simple linked list the problem is as it goes to class after class, the value of the first node changes. 我正在制作一个简单的链表,问题在于上课时,第一个节点的值发生了变化。 This is my insert code: 这是我的插入代码:

int adjob(int job,int time, int size,JOBS **list)
{
    JOBS *temp,*new;
    temp=*list;
    if(temp==NULL)
        *list=create(job,time,size);

    else
    {
        new=(JOBS*)malloc(sizeof(JOBS));
        new->job=job;
        new->size=size;
        new->duration=time;
        new->next=NULL;
        while(temp->next!=NULL)
            temp=temp->next;
        temp->next=new; 
    }
        return 0;
}

And this is the main: 这是主要的:

MEM mmem[10];
int main()
{
    int i=1,jtime,jsize,status;
    char option;
    JOBS *joblist;
    joblist=NULL;
    status=initmem(mmem);
    for(;;)
    {
        clrscr();
        printer(mmem,&joblist);
        scanf("%s",&option);
        switch(option)
        {
            case 'I':       if(i<16)
                            {
                                printf("Input size of job %i: ",i);
                                scanf("%i",&jsize);
                                printf("Input duration: ");
                                scanf("%i",&jtime);
                                status=adjob(i,jtime,jsize,&joblist);
                            }
                                i++;
                                break;
            case 'S':       break;
            case 'R':       break;
            case 'E':       exit(0);
        }
    }
    free(joblist);
    joblist=NULL;
    return 0;
}

These are my structures: 这些是我的结构:

struct node
{
    int job;
    int size;
    int duration;
    struct node *next;
};

struct mainmem
{
    int memblock;
    int size;
    int jobnum;
    int jobsize;
    int timer;
};
typedef struct mainmem MEM;
typedef struct node JOBS;

Then this is my printer: 然后这是我的打印机:

int printer(MEM *mymem,JOBS **list)
{
    JOBS *temp;
    int i=0,pr=3;
    temp=*list;

    /*MEM LIST*/
    printf("\t\tMEMORY LIST\t\t\t\tJOB LIST\nMem Block    Size    Job    Job Size    Timer\n");
    while(i!=10)
    {
        printf("   %i\t     %i\n",i+1,mymem[i].size);
        i++;
    }
    /*JOB LIST*/
    gotoxy(50,2);
    printf("Job no.\tTime\tJob Size");
    if(temp==NULL)
    {
        gotoxy(50,pr);
        printf("No jobs on queue.");
    }
    else
    {
        while(temp!=NULL)
        {   
            gotoxy(50,pr);
            printf("%i\t%i\t%i",temp->job,temp->duration,temp->size);
            pr++;
            temp=temp->next;
        }
    }
    gotoxy(1,20);
    printf("[I]-INPUT   [S]-START   [R]-RESULT  [E]-EXIT\n");
    return 0;
}

The first node inserts with no problem. 第一个节点插入没有问题。 When I insert a second node, the first node changes. 当我插入第二个节点时,第一个节点会更改。 And when I try to insert beyond 2, the program functions as it should be. 当我尝试插入超过2时,程序将按原样运行。 I tried debugging it, the value changes when the second input is inserted int he JOBS LL, but I can't figure that out from my code. 我尝试调试它,当在JOBS LL中插入第二个输入时,该值会更改,但是我无法从代码中弄清楚。 What could have possibly gone wrong? 可能出了什么问题?

The problems you have is probably because you have undefined behavior in your code: The variable option is a single char , but you read into it as a string which will write two characters (the character you read and the string terminator). 您遇到的问题可能是因为您的代码中存在未定义的行为 :variable option是一个char ,但是您将其读为一个字符串,该字符串将写入两个字符(读取的字符字符串终止符)。 There's no way of telling what that will overwrite, but it will likely overwrite parts of the stack where the other local variables are stored. 没有办法知道将覆盖哪些内容,但是可能会覆盖存储其他局部变量的堆栈部分。

You should change the input handling to read only a single character: 您应该将输入处理更改为仅读取单个字符:

scanf(" %c", &option);

Note the leading space, which will tell scanf to read and discard any white-space (including newlines) before the character. 请注意前导空格,它将告诉scanf读取并丢弃字符前面的任何空格(包括换行符)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM