| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
 跟大牛学C++学算法数据结构 程序代码：
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct student
{
char name;
char num;
int age;
struct student *next;
};

int main(void)
{
struct student *creat(void);
void myprint(const struct student *);
void save(FILE *, const struct student *);
struct student *read_bin(FILE *);

FILE *fp;

fp = fopen("file", "wb");
fclose(fp);

fp = fopen("file", "rb");
fclose(fp);

system("pause");

return 0;
}

//建立单向链表
struct student *creat(void)
{
struct student *head, *p1, *p2;
int flag;

do
{
p2 = (struct student *)malloc(sizeof(struct student));

printf("input student's name,number and age:\n");
scanf("%s%s%d", p2->name, p2->num, &p2->age);

if (head == NULL)
else
p1->next = p2;
p1 = p2;

printf("\ninput 1 to keep on or input 0 to end:");
scanf("%d", &flag);
} while (flag);
p1->next = NULL;

}

//输出单向链表
void myprint(const struct student *head)
{
struct student *p = (struct student *)head;

while (p)
{
printf("%-10s\t%12s\t%2d\n", p->name, p->num, p->age);
p = p->next;
}
}

//保存链表至二进制文件
void save(FILE *fp, const struct student *head)
{
struct student *p = (struct student *)head;

while (p)
{
fwrite(p, sizeof(p->next) + sizeof(p->num) + sizeof(p->age), 1u, fp);
p = p->next;
}
}

//创建链表读取二进制文件中的信息
struct student *read_bin(FILE *fp)
{
struct student *head, *p1, *p2;

while (!feof(fp))
{
p2 = (struct student *)malloc(sizeof(struct student));

fread(p2, sizeof(p2->name) + sizeof(p2->num) + sizeof(p2->age), 1u, fp);

if (head == NULL)
else
p1->next = p2;
p1 = p2;
}
p1->next = NULL;

}

[此贴子已经被作者于2019-1-12 20:07编辑过]             程序代码：
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct student
{
char name;
char num;
int age;
struct student *next;
};

int main(void)
{
struct student *creat(void);
void myprint(const struct student *);
void save(FILE *, const struct student *);
struct student *read_bin(FILE *);

FILE *fp;

printf("\n\n\n");

fp = fopen("file", "wb");
fclose(fp);

fp = fopen("file", "rb");
fclose(fp);

system("pause");

return 0;
}

//建立单向链表
struct student *creat(void)
{
struct student *head, *p1, *p2;
int flag;

do
{
p2 = (struct student *)malloc(sizeof(struct student));

printf("input student's name,number and age:\n");
scanf("%s%s%d", p2->name, p2->num, &p2->age);

if (head == NULL)
else
p1->next = p2;
p1 = p2;

printf("\ninput 1 to keep on or input 0 to end:");
scanf("%d", &flag);
} while (flag);
p1->next = NULL;

}

//输出单向链表
void myprint(const struct student *head)
{
struct student *p = (struct student *)head;

while (p)
{
printf("%-10s\t%12s\t%2d\n", p->name, p->num, p->age);
p = p->next;
}
}

//链表信息写入二进制文件
void save(FILE *fp, const struct student *head)
{
struct student *p = (struct student *)head;

while (p)
{
fwrite(p, sizeof(struct student) - sizeof(struct student *) , 1u, fp);
p = p->next;
}
}

//二进制文件信息读取至链表
struct student *read_bin(FILE *fp)
{
struct student *head, *p1, *p2;

while (1)
{
p2 = (struct student *)malloc(sizeof(struct student));

if (fread(p2, sizeof(struct student) - sizeof(struct student *), 1u, fp) != 1)
{
free(p2);
break;
}

if (head == NULL)
else
p1->next = p2;
p1 = p2;
}
p1->next = NULL;

}     程序代码：
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct student
{
char name;
char num;
int age;
struct student *next;
};

int main(void)
{
struct student *creat(void);
void myprint(const struct student *);
void save(FILE *, const struct student *);
struct student *read_bin(FILE *);

FILE *fp;

printf("\n\n\n");

fp = fopen("file", "wb");
fclose(fp);

fp = fopen("file", "rb");
fclose(fp);

system("pause");

return 0;
}

//建立单向链表
struct student *creat(void)
{
struct student *head, *p1, *p2;
int flag;

do
{
p2 = (struct student *)malloc(sizeof(struct student));

printf("input student's name,number and age:\n");
scanf("%s%s%d", p2->name, p2->num, &p2->age);

if (head == NULL)
else
p1->next = p2;
p1 = p2;

printf("\ninput 1 to keep on or input 0 to end:");
scanf("%d", &flag);
} while (flag);
p1->next = NULL;

}

//输出单向链表
void myprint(const struct student *head)
{
struct student *p = (struct student *)head;

while (p)
{
printf("%-10s\t%12s\t%2d\n", p->name, p->num, p->age);
p = p->next;
}
}

//链表信息写入二进制文件
void save(FILE *fp, const struct student *head)
{
struct student *p = (struct student *)head;

while (p)
{
fwrite(p, sizeof(struct student) - sizeof(struct student *) , 1u, fp);
p = p->next;
}
}

//二进制文件信息读取至链表
struct student *read_bin(FILE *fp)
{
struct student *head, *p1, *p2;

while (1)
{
p2 = (struct student *)malloc(sizeof(struct student));

if (fread(p2, sizeof(struct student) - sizeof(struct student *), 1u, fp) != 1)
{
free(p2);
break;
}

if (head == NULL)
else
p1->next = p2;
p1 = p2;
}
p1->next = NULL;

}    • 5
• 1/1页
• • 1
• Powered by Discuz, Processed in 0.143970 second(s), 9 queries.