C语言程序链表出现问题
1 #include<stdio.h>2 #include<stdlib.h>
3 typedef struct
4 {
5 int year;
6 int month;
7 int day;
8 }DATE;
9 struct stud
10 {
11 int num;
12 char name[20];
13 char kind[20];
14 DATE time;
15 int number;
16 float money;
17 struct stud *next;
18 };
19 struct stud *creat()
20 {
21 FILE *fp;
22 fp=fopen("店家.txt","wt");
23 if(fp==NULL)
24 {
25 printf("error!");
26 getchar();
27 exit(1);
28 }
29 else
30 {
31 int max,i=1;
32 printf("please input the number of dianjia:");
33 scanf("%d",&max);
34 struct stud *p,*p1,*p2,*head;
35 head=p2=(struct stud *)malloc(sizeof(struct stud));
36 p1=(struct stud *)malloc(sizeof(struct stud));
37 printf("please input num:");
38 scanf("%d",&p1->num);
39 printf("please input name:");
40 scanf("%s",p1->name);
41 printf("please input kind:");
42 scanf("%s",p1->kind);
43 printf("please input year:");
44 scanf("%d",&p1->time.year);
45 printf("please input month:");
46 scanf("%d",&p1->time.month);
47 printf("please input day:");
48 scanf("%d",&p1->time.day);
49 printf("please input number:");
50 scanf("%d",&p1->number);
51 printf("please input money:");
52 scanf("%f",&p1->money);
53 head=p1;
54 for(;i<max;i++)
55 {
56 p2->next=p1;
57 p2=p1;
58 p1=(struct stud *)malloc(sizeof(struct stud));
59 printf("please input num:");
60 scanf("%d",&p1->num);
61 printf("please input name:");
62 scanf("%s",p1->name);
63 printf("please input kind:");
64 scanf("%s",p1->kind);
65 printf("please input year:");
66 scanf("%d",&p1->time.year);
67 printf("please input month:");
68 scanf("%d",&p1->time.month);
69 printf("please input day:");
70 scanf("%d",&p1->time.day);
71 printf("please input number:");
72 scanf("%d",&p1->number);
73 printf("please input money:");
74 scanf("%f",&p1->money);
75 }
76 p=head;
77 while(p!=NULL)
78 {
79 fprintf(fp,"序号:%d 名称:%s 类别:%s 注册时间:%d-%d-%d 许可证
编号:%d 营业额:%0.2f\n",p->num,p->name,p->kind,p->time.year,p->time.month,p ->time.day,p->number,p->money);
80 p=p->next;
81 }
82 p2->next=NULL;
83 free(p);
84 free(p1);
85 fclose(fp);
86 return head;
87 }
88 }
89 add()
90 {
91 ;
92 }
93 change()
94 {
95 ;
96 }
97 del()
98 {
99 ;
100 }
101 output()
102 {
103 FILE *fp;
104 // int x,i;
105 // printf("please input the number:");
106 // scanf("%d",&x);
107 struct stud *p,*n;
108 fp=fopen("店家.txt","r");
109 if(fp==NULL)
110 {
111 printf("error!");
112 getchar();
113 exit(1);
114 }
115 printf("\n\n\n 报表如下:\n\n\n");
116 // for(i=1;i<=x;i++)
117 // {
118 // printf("序号:%d 名称:%s 类别:%s 注册时间:%d-%d-%d 许可证编号:%d 营业额:%0.2f\n",p->num,p->name,p->kind,p->time.year,p->time.month,p->time.day,p->number,p->money);
119 // p=p->next;
120 // }
121 p=(struct stud *)malloc(sizeof(struct stud));
122 while(fscanf(fp,"序号:%d 名称:%s 类别:%s 注册时间:%d-%d-%d 许可证编号:%d 营业额:%0.2f\n",&p->num,p->name,p->kind,&p->time.year,&p- >time.month,&p->time.day,&p->number,&p->money)!=0)
123 {
124 fscanf(fp,"序号:%d 名称:%s 类别:%s 注册时间:%d-%d-%d 许可证编号:%d 营业额:%0.2f\n",&p->num,p->name,p->kind,&p->time.year,&p->time.month,&p->time.day,&p->number,&p->money);
125 printf("序号:%d 名称:%s 类别:%s 注册时间:%d-%d-%d 许可证编号:%d 营
业额:%0.2f\n",p->num,p->name,p->kind,p->time.year,p->time.month,p->time.day,p->number,p->money);
126 }
127 }
128 tongji()
129 {
130 ;
131 }
132 find()
133 {
134 ;
135 }
136 paihang()
137 {
138 ;
139 }
140
141 void main()
142 {
143 int choice;
144 struct stud *head;
145 head=creat();
146 do{
147 printf("\n\n\n *****商场店面管理系统*****\n\n");
148 printf(" 1.添加店家信息\n\n");
149 printf(" 2.修改店家信息\n\n");
150 printf(" 3.删除店家信息\n\n");
151 printf(" 4.输出信息报表\n\n");
152 printf(" 5.查询店家信息\n\n");
153 printf(" 6.统计店家信息\n\n");
154 printf(" 7.店家信息排行\n\n");
155 scanf("%d",&choice);
156 switch(choice)
157 {
158 case 1:add(); break;
159 case 2:change(); break;
160 case 3:del(); break;
161 case 4:output(); break;
162 case 5:find(); break;
163 case 6:tongji(); break;
164 case 7:paihang(); break;
165 case 0: break;
166 }
167 }while(choice!=0);
168 }
169
跪求大神们帮我看看这个程序错在哪,编译没有问题,就是链表输出和输入不符合,应该是在链表的建立部分出现了问题,这种情况下输出链表总是少一个结点(好像是尾结点)
19 struct stud *creat()
20 {
21 FILE *fp;
22 fp=fopen("店家.txt","wt");
23 if(fp==NULL)
24 {
25 printf("error!");
26 getchar();
27 exit(1);
28 }
29 else
30 {
31 int max,i=1;
32 printf("please input the number of dianjia:");
33 scanf("%d",&max);
34 struct stud *p,*p1,*p2,*head;
35 head=p2=(struct stud *)malloc(sizeof(struct stud));
36 p1=(struct stud *)malloc(sizeof(struct stud));
37 printf("please input num:");
38 scanf("%d",&p1->num);
39 printf("please input name:");
40 scanf("%s",p1->name);
41 printf("please input kind:");
42 scanf("%s",p1->kind);
43 printf("please input year:");
44 scanf("%d",&p1->time.year);
45 printf("please input month:");
46 scanf("%d",&p1->time.month);
47 printf("please input day:");
48 scanf("%d",&p1->time.day);
49 printf("please input number:");
50 scanf("%d",&p1->number);
51 printf("please input money:");
52 scanf("%f",&p1->money);
54 for(;i<max;i++)
55 {
58 p1=(struct stud *)malloc(sizeof(struct stud));
59 printf("please input num:");
60 scanf("%d",&p1->num);
61 printf("please input name:");
62 scanf("%s",p1->name);
63 printf("please input kind:");
64 scanf("%s",p1->kind);
65 printf("please input year:");
66 scanf("%d",&p1->time.year);
67 printf("please input month:");
68 scanf("%d",&p1->time.month);
69 printf("please input day:");
70 scanf("%d",&p1->time.day);
71 printf("please input number:");
72 scanf("%d",&p1->number);
73 printf("please input money:");
74 scanf("%f",&p1->money);
p2->next=p1;
p2=p1;
75 }
76 p=head;
77 while(p!=NULL)
78 {
79 fprintf(fp,"序号:%d 名称:%s 类别:%s 注册时间:%d-%d-%d 许可证
编号:%d 营业额:%0.2f\n",p->num,p->name,p->kind,p->time.year,p->time.month,p->time.day,p->number,p->money);
80 p=p->next;
81 }
82 p2->next=NULL;
83 free(p);
84 free(p1);
85 fclose(fp);
86 return head;
87 }
88 }
之后我对建立部分作了如上的修改(FOR语句前的head=p1删除,将FOR语句中的 p2->next=p1;p2=p1;移到后面)这样修改后链表的结点数量是没有问题了,可是头结点中输出的数据不是空的,就是0,尾结点的num和name是空的,不知道是读不到数据还是没有赋值进去。求各位大神帮忙看看
(程序中部分空格是复制的时候出现的,本身编译没有问题)