关于创建动态链表的问题
本人创建了一个动态链表并输出,模拟PCB(进程控制器),源程序如下:
我的问题是输出的时候多了两个莫名其妙的数,可能是指针问题,但我实在找不到什么地方出了问题
恳请各位高手指点:
#include <conio.h>
#include <stdio.h>
#define NULL 0
#define LEN sizeof(struct PCB)
struct PCB
{char name[9];
int runtime;
int power;
struct PCB *next;
};
int n;
struct PCB *creat(void)
{struct PCB *head;
struct PCB *p1,*p2;
n=0;
printf("input the 1 PCB name,runtime and power:\n");
p1=p2=(struct PCB *)malloc(LEN);
scanf("%s,%d,%d",&p1->name,&p1->runtime,&p1->power);
head=NULL;
while(strcmp(p1->name,"finish")!=0)
{n=n+1;
if(n==1) head=p1;
else{
p2->next=p1;
p2=p1;
p1=(struct PCB *)malloc(LEN);
printf("input the %d PCB name,runtime and power:\n",n);
scanf("%s,%d,%d",&p1->name,&p1->runtime,&p1->power);
}
}
p2->next=NULL;
return(head);
}
main()
{
char ch;
struct PCB *p;
clrscr();
p=creat();
printf("\nNow,These %d records are:\n",n-1);
do{
printf("%s,%d,%d\n",p->name,p->runtime,p->power);
p=p->next;
}while(p!=NULL);
ch=getch();
}