0

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. 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. What could have possibly gone wrong?

  • 1
    Please include a complete, simple, self-contained program, along with the actual output and expected output. – David Grayson Jul 12 '14 at 16:20
  • [In C you should not cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Jul 12 '14 at 16:23
  • 1
    As for your problem, step though the code, line by line, in a debugger, while keeping an eye on variables and their values. – Some programmer dude Jul 12 '14 at 16:24
  • Only had a quick look but it would appear that you always pass through a pointer to the root of your list as the fourth argument to adjobs. You then dereference this and set it's next element to the job you are currently adding. So you on the second you'll point the first to the second. On the third you will point the first to the third - you should be pointing the second to the third. – ianharris Jul 12 '14 at 16:34

1 Answers1

1

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). 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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621