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?