以下是我在TC2.0下编的程序,问题出在不能插入结构体(插入的结构体位于链表的开头)。每次插入一个结构体,输出的链表数据除了插入的那个结构体(位于链表开头)数据准确无误以外,之后的数据就都变了.........
本人还是一个菜鸟,只学了C语言3个月,还望诸位大侠能多多指教!
#include<stdio.h>
#include<string.h>
#include<alloc.h>
#include<stdlib.h>
#include<ctype.h>
#define PR printf
#define SC scanf
#define NULL 0
struct jijin
{int money;
float sum;
struct jijin *next;
};
struct jijin *head_j;
struct data
{long time;
int n;
struct jijin *ps;
struct data *next;
};
struct data *head_d;
int n_d=0;
menu_data()
{
char s[80];
int c;
printf("press any key enter menu......\n");/*提示压任意键继续*/
getch(); /*读入任意字符*/
clrscr(); /*清屏*/
printf("*************************************MENU**************************************\n\n");
printf(" 1.Read\n");
printf(" 2.Creat .\n");
printf(" 3.Insert.\n");
printf(" 4.save.\n");
printf(" 5.Quit.\n");
printf("********************************************************************************\n");
do{
printf("\n Enter you choice(1~4):"); /*提示输入选项*/
scanf("%s",s); /*输入选择项*/
c=atoi(s); /*将输入的字符串转化为整型数*/
}while(c<1||c>5); /*选择项不在1~4之间重输*/
return c; /*返回选择项,主程序根据该数调用相应的函数*/
}
void print_d(struct data *head_d)
{struct data *p;
struct jijin *j;
p=head_d;
j=p->ps;
if(head_d!=NULL)
printf("**************************************DATA**************************************\n");
printf("================================================================================\n");
printf("NAME DATE MONEY NET VALUE LOT \n");
do
{printf("%ld\n",p->time);
do
{
printf("%d\n",j->money);
printf("%f\n",j->sum);
j=j->next;
}while(j!=NULL);
p=p->next;
}while(p!=NULL);
printf("================================================================================\n");
printf("********************************************************************************\n");
}
void save_d(struct data *head_d)
{struct data *p;struct jijin *j;
FILE *fp;
printf("Saving.............\n");
getch();
fp=fopen("d:\\data","w"); /*为输出打开一个二进制文件,为只写方式*/
p=head_d; /*移动指针从头指针开始*/
while(p!=NULL) /*如p不为空*/
{j=p->ps;
fwrite(p,sizeof(struct data),1,fp);
while(j!=NULL)
{
fwrite(j,sizeof(struct jijin),1,fp);
j=j->next;
}
p=p->next;
}
fclose(fp); /*关闭文件*/
printf("Success!\n");
}
struct data *input_d()
{int a;
struct data *p;
struct jijin *p1,*p2;
p=(struct data*)malloc(sizeof(struct data));
p->ps=p1=p2=(struct jijin*)malloc(sizeof(struct jijin)); /*head为setting初始地址*/
printf("Please input the totol number \n");
scanf("%d",&(*p).n);
printf("please tell me wen you bought this jijin\n");
scanf("%ld",&(*p).time);
for(a=1;a<=p->n;a++)
{printf("money\n");
scanf("%d",&(*p1).money);
printf("sum\n");
scanf("%f",&(*p1).sum);
if(a==1)head_j=p1;
else p2->next=p1;
p2=p1;
p1=(struct jijin*)malloc(sizeof(struct jijin));
}
p2->next=NULL;
return(p);
}
struct data *creat_d()/*建立一个新的结构体*/
{
head_d=input_d();
head_d->next=NULL;
print_d(head_d);
getch();
return(head_d);
}
struct data *insert_d(struct data *head_d)
{
struct data *p,*p0;
p=head_d;
p0=input_d();
head_d=p0;
p0->next=p;
return(head_d);
}
main()
{
for(;;)
{
switch(menu_data())
{
case 1:print_d(head_d);break;
case 2:head_d=creat_d();save_d(head_d);break;
case 3:head_d=insert_d(head_d);print_d(head_d);getch();save_d(head_d);break;
case 4:save_d(head_d);break;
case 5:exit(0);
}
}
}