⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list_sq2.cpp

📁 vc的数据结构链表应用
💻 CPP
字号:
//采用动态顺序表结构的人事信息管理系统,可以在任何位置插入、删除元素
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<conio.h>

typedef enum _SEXS {MALE, FEMALE} SEXS;

typedef struct _CPerson
{
	int			id;
	char		name[9];//欧阳奋强
	SEXS		sex;
	char		birthday[11];//2000.12.31
	char		address[101];
} CPerson;

typedef CPerson ElemType;//注意该语句必须在该位置。一个问题:如果在一个程序中需要用到两个顺序表,就无法处理

#include "list_sq.h"//注意该语句必须在该位置

void print_personnel(SqList personnel)//输入输出功能必须单独定义
{
	printf("Total/Max: %d/%d\n\n", personnel.length, personnel.listsize);
	printf("No.	id	name	sex	birthday	address\n");
	for(int i=0; i<personnel.length; i++)
		printf("%d:	%d	%s	%d	%s	%s\n", i+1,	personnel.elements[i].id,//注意这里必须访问elements成员,感觉意义有点不符
												personnel.elements[i].name,
												personnel.elements[i].sex,
												personnel.elements[i].birthday,
												personnel.elements[i].address);
	printf("\n");
}

int load_personnel(SqList& personnel)//不必判断是否越界,源文件不必含总数,可以直接插入元素
{
	init_list(personnel);
	FILE* file;
	file=fopen("personnel.txt", "r");
	if(file==NULL)
	{
		printf("Personnel file is not exist.\n");
		return false;
	}
	else
	{
		CPerson person; int length=0;
		while(!feof(file))
		{
			fscanf(file, "%d	%s	%d	%s	%s\n",	&person.id, person.name, &person.sex, person.birthday, person.address);
			insert_element(personnel, ++length, person);
		}
		fclose(file);
		return true;
	}
}

void main()
{
	SqList personnel={NULL,0,0};
	printf("-----print new personnel:-----\n");
	print_personnel(personnel);

	init_list(personnel);//注意这里必须使用名称init_list而不是init_personnel,感觉意义非常不符。insert_element等函数也有类似情况。改进办法:只使用动词,如init(),insert()
	printf("-----init_list:-----\n");
	print_personnel(personnel);

	CPerson person1={1,"1111",MALE,"2003,1,1","safasaaaafd"};
	if(!insert_element(personnel, 1, person1)) printf("Cannot insert person.\n");
	printf("-----Result of insert_person1 at 1:-----\n");
	print_personnel(personnel);

	CPerson person2={2,"2222",FEMALE,"2002,1,1","safsssasfd"};
	if(!insert_element(personnel, 1, person2)) printf("Cannot insert person.\n");
	printf("-----Result of insert_person2 at 1:-----\n");
	print_personnel(personnel);

	CPerson person3={3,"3333",FEMALE,"2001,1,1","safffffffasfd"};
	if(!insert_element(personnel, 2, person3)) printf("Cannot insert person.\n");
	printf("-----Result of insert_person3 at 2:-----\n");
	print_personnel(personnel);

	CPerson person4={4,"4444",MALE,"2002,11,1","4jnkrejs"};
	if(!insert_element(personnel, 4, person4)) printf("Cannot insert person.\n");
	printf("-----Result of insert_person4 at 4:-----\n");
	print_personnel(personnel);

	if(!delete_element(personnel, 3)) printf("Cannot delete person at 3.\n");//提问:删除元素是否需要释放空间?
	printf("-----Result of delete_person at 3:-----\n");
	print_personnel(personnel);

	if(!delete_element(personnel, 3)) printf("Cannot delete person at 3.\n");
	printf("-----Result of delete_person at 3:-----\n");
	print_personnel(personnel);

	if(!delete_element(personnel, 3)) printf("Cannot delete person at 3.\n");
	printf("-----Result of delete_person at 3:-----\n");
	print_personnel(personnel);

	SqList personnel2;
	if(!load_personnel(personnel2)) return;
	printf("-----Result of load_personnel2:-----\n");
	print_personnel(personnel2);

	union_list(personnel, personnel2);
	printf("-----Result of union personnel & personnel2 into personnel:-----\n");
	print_personnel(personnel);

	SqList personnel3;
	merge_list(personnel2, personnel, personnel3);
	printf("-----Result of merge personnel2 & personnel into personnel3:-----\n");
	print_personnel(personnel3);
	
	destroy_list(personnel);
	destroy_list(personnel2);
	destroy_list(personnel3);//提问:personnel是局部变量,不是指针,为何还有free空间?如果使用静态数组呢?
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -