#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define N 3
struct Node
{
int num; /*学号*/
char name[10]; /*姓名*/
int age; /*年龄*/
int score; /*成绩*/
char addr[15]; /*数据域*/
struct Node *next; /*指针域*/
};
/*建立带头结点的链表*/
struct Node *creat_inf()
{
struct Node *head,*r,*stu;
int i=0;
char choice;
head=(struct Node *)malloc(sizeof(struct Node)); /*创建头文件结点*/
head->next=NULL;
r=head;
do
{
stu=(struct Node *)malloc(sizeof(struct Node));
printf("\n\n第%d个人的信息:\n",++i);
printf("\n学号 姓名 年龄 成绩 住址:");
scanf("%d %s %d %d %s",&stu->num,&stu->name,&stu->age,&stu->score,&stu->addr);
r->next=stu; /*尾插新结点*/
r=stu;
printf("contiune?(Y/N)");
choice=getche();
}while(choice=='Y'||choice=='y');
r->next=NULL;
return(head);
}
/*保存基本信息到指定文件*/
save_inf(struct Node *h)
{
struct Node *stu;
FILE *fp;
char filename[40];
printf("\n请输入要保存的文件名:");
scanf("%s",filename);
if((fp=fopen(filename,"wt"))==NULL)
printf("写文件出错,按任意键退出!");
for(stu=h->next;stu!=NULL;stu=stu->next)
fprintf(fp,"%d %s %d %d %s\n",stu->num,stu->name,stu->age,stu->score,stu->addr);
printf("\n文件以保存成功,按任意键返回!");
getch();
fclose(fp);
}
/*从指定的磁盘文件中读取信息并存入链表中*/
struct Node *read_inf()
{
struct Node *head,*r,*stu;
FILE *fp;
char filename[40];
printf("\n请输入要打开的文件名:");
scanf("%s",filename);
if((fp=fopen(filename,"rt"))==NULL)
{
printf("读文件出错,按任意键退出!");
getch();
exit(1);
}
head=(struct Node *)malloc(sizeof(struct Node));
head->next=NULL;
r=head;
stu=(struct Node *)malloc(sizeof(struct Node));
/*存放读取信息*/
fscanf(fp,"%d %s %d %d %s",&stu->num,stu->name,&stu->age,&stu->score,stu->addr);
while(!feof(fp)) /*文件未结束*/
{
r->next=stu;
r=stu;
/*开辟空间,以存放读取信息*/
stu=(struct Node *)malloc(sizeof(struct Node));
/*存放读取信息*/
fscanf(fp,"%d %s %d %d %s",&stu->num,stu->name,&stu->age,&stu->score,stu->addr);
}
r->next=NULL;
fclose(fp);
printf("\n文件中信息以正确读出,按任意键返回!");
getch();
return head;
}
/*将链表中的信息打印输出*/
print_inf(struct Node *h)
{
struct Node *stu;
printf("\n该班数据为: \n");
printf("学号 姓名 年龄 成绩 住址\n");
for(stu=h->next;stu!=NULL;stu=stu->next)
printf("%d %s %d %d %s\n",stu->num,stu->name,stu->age,stu->score,stu->addr);
}
/*输出*/
void print(struct Node*p)
{
p=p->next;
while(p!=NULL)
{
printf("学号:%d 姓名:%s 年龄:%d 成绩:%d 住址:%s\n",p->num,p->name,p->age,p
->score,p->addr);
p=p->next;
}
}
/*链表的查找*/
struct Node * find(struct Node *p)/*链表的查找*/
{
long num;
printf("请输入要查找的学号:");
scanf("%d",&num);
while(p->next!=NULL)
{
p=p->next; /*指针后移,比较下一结点*/
if(p->num==num) /*找到则返回指向该结点的指针P*/
return p;
}
return NULL; /*未找到则返回空指针NULL*/
}
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define N 3
struct Node
{
int num; /*学号*/
char name[10]; /*姓名*/
int age; /*年龄*/
int score; /*成绩*/
char addr[15]; /*数据域*/
struct Node *next; /*指针域*/
};
/*建立带头结点的链表*/
struct Node *creat_inf()
{
struct Node *head,*r,*stu;
int i=0;
char choice;
head=(struct Node *)malloc(sizeof(struct Node)); /*创建头文件结点*/
head->next=NULL;
r=head;
do
{
stu=(struct Node *)malloc(sizeof(struct Node));
printf("\n\n第%d个人的信息:\n",++i);
printf("\n学号 姓名 年龄 成绩 住址:");
scanf("%d %s %d %d %s",&stu->num,&stu->name,&stu->age,&stu->score,&stu->addr);
r->next=stu; /*尾插新结点*/
r=stu;
printf("contiune?(Y/N)");
choice=getche();
}while(choice=='Y'||choice=='y');
r->next=NULL;
return(head);
}
/*保存基本信息到指定文件*/
save_inf(struct Node *h)
{
struct Node *stu;
FILE *fp;
char filename[40];
printf("\n请输入要保存的文件名:");
scanf("%s",filename);
if((fp=fopen(filename,"wt"))==NULL)
printf("写文件出错,按任意键退出!");
for(stu=h->next;stu!=NULL;stu=stu->next)
fprintf(fp,"%d %s %d %d %s\n",stu->num,stu->name,stu->age,stu->score,stu->addr);
printf("\n文件以保存成功,按任意键返回!");
getch();
fclose(fp);
}
/*从指定的磁盘文件中读取信息并存入链表中*/
struct Node *read_inf()
{
struct Node *head,*r,*stu;
FILE *fp;
char filename[40];
printf("\n请输入要打开的文件名:");
scanf("%s",filename);
if((fp=fopen(filename,"rt"))==NULL)
{
printf("读文件出错,按任意键退出!");
getch();
exit(1);
}
head=(struct Node *)malloc(sizeof(struct Node));
head->next=NULL;
r=head;
stu=(struct Node *)malloc(sizeof(struct Node));
/*存放读取信息*/
fscanf(fp,"%d %s %d %d %s",&stu->num,stu->name,&stu->age,&stu->score,stu->addr);
while(!feof(fp)) /*文件未结束*/
{
r->next=stu;
r=stu;
/*开辟空间,以存放读取信息*/
stu=(struct Node *)malloc(sizeof(struct Node));
/*存放读取信息*/
fscanf(fp,"%d %s %d %d %s",&stu->num,stu->name,&stu->age,&stu->score,stu->addr);
}
r->next=NULL;
fclose(fp);
printf("\n文件中信息以正确读出,按任意键返回!");
getch();
return head;
}
/*将链表中的信息打印输出*/
print_inf(struct Node *h)
{
struct Node *stu;
printf("\n该班数据为: \n");
printf("学号 姓名 年龄 成绩 住址\n");
for(stu=h->next;stu!=NULL;stu=stu->next)
printf("%d %s %d %d %s\n",stu->num,stu->name,stu->age,stu->score,stu->addr);
}
/*输出*/
void print(struct Node*p)
{
p=p->next;
while(p!=NULL)
{
printf("学号:%d 姓名:%s 年龄:%d 成绩:%d 住址:%s\n",p->num,p->name,p->age,p
->score,p->addr);
p=p->next;
}
}
/*链表的查找*/
struct Node * find(struct Node *p)/*链表的查找*/
{
long num;
printf("请输入要查找的学号:");
scanf("%d",&num);
while(p->next!=NULL)
{
p=p->next; /*指针后移,比较下一结点*/
if(p->num==num) /*找到则返回指向该结点的指针P*/
return p;
}
return NULL; /*未找到则返回空指针NULL*/
}