0
我想提出一個簡單的鏈表的問題是,因爲它下課後去上課,第一節點的值更改值。這是我的插入代碼:鏈表突然改變用C
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;
}
這是主要的:
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;
}
這是我的結構:
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;
然後,這是我的打印機:
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;
}
第一個節點插入時沒有問題。當我插入第二個節點時,第一個節點會改變。當我嘗試插入超過2時,該程序的功能應該如此。我試着調試它,當第二個輸入插入JOBS LL時,值發生變化,但我無法從我的代碼中找出它。可能會出現什麼問題?
請包括一個完整的,簡單的,獨立的程序,與實際輸出與預期輸出沿。 –
[在C,你不應該投的'malloc'結果](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)。 –
至於你的問題,但步的代碼,一行行,在一個調試器,同時密切關注的變量和它們的值。 –