GDPU 数据结构 天码行空2

实验内容

用顺序表实现病历信息的管理与查询功能。具体要求如下:

  1. 利用教材中定义顺序表类型存储病人病历信息(病历号,姓名,症状);要求使用头文件。

  2. 设计顺序表定位查找算法,写成一个函数,完成的功能为:在线性表L中查找数据元素x,如果存在则返回线性表中和x值相等的第1个数据元素的序号;如果不存在,则返回-1。

    函数定义为 int ListFind(SequenceList L,char *x)

请在主函数中测试查找是否存在姓名为x的病人,并根据返回的序号打印出病人信息。


注意 头文件的名称 和 c程序的名称 需要一致,并且两文件放在同一目录下。

头文件 hello.h

#define MaxSize 10000

typedef struct  {
	int id;//病历号
	char* name;//病人名称
	char* symptoms;//症状
}patient;

typedef struct 
{
	patient list[MaxSize];
	int size;
}SequenceList;

void ListInitialize(SequenceList *L);

int ListLength(SequenceList L);

int ListInsert(SequenceList *L,int i,patient p);

int ListFind(SequenceList L,char *name);

void ListPrint(SequenceList L);

//打印出指定序号的病人信息
void PrintById(SequenceList L,int id);

hello.c

#include 
#include 
#include "hello.h" 

//初始化一个顺序表,长度为 0
void ListInitialize(SequenceList *L)
{
	L->size = 0;
}

//返回顺序表中的元素个数
int ListLength(SequenceList L)
{
	return L.size;
}

// 在顺序表 L 的位置 i 前插入 x 值
int ListInsert(SequenceList *L,int i,patient p)
{
	int j = 0;
	if(L -> size >= MaxSize)
	{
		printf("顺序表已满 \n");	 
		return 0;
	}
	if(i < 0 || i > L->size)
	{
		printf("参数i不合法 \n");
		return 0;
	}
	for(j = L -> size; j > i; j--)
		L -> list[j] = L -> list[j-1];
	L -> list[i] = p;
	L -> size++;
	return 1; 
	
} 

// 在顺序表 L 中查找姓名为 x 的下标
int ListFind(SequenceList L,char *name)
{
	int size = ListLength(L);
	for(int i = 0; i < size; i++)
	{
//		strcmp(s1,s2) 当s1 == s2 返回 0
		if(!strcmp(name,L.list[i].name))
			return i;
	}	
	return -1;//不存在则返回-1 
}

//打印顺序表所有内容
void ListPrint(SequenceList L)
{
	for(int i = 0; i < ListLength(L); i++)
	{
		printf("%d %s %s\n",L.list[i].id,L.list[i].name,L.list[i].symptoms);
	}
}

//打印出指定序号的病人信息
void PrintById(SequenceList L,int idx)
{
	printf("id: %d, 姓名:%s, 症状:%s\n",
		L.list[idx].id,
		L.list[idx].name,
		L.list[idx].symptoms);
}
int main()
{
	SequenceList L;
	ListInitialize(&L);
	patient p1 = {1,"张三","症状1"};
	patient p2 = {2,"李四","症状2"};
	patient p3 = {3,"王五","症状3"}; 
	ListInsert(&L,0,p1); 
	ListInsert(&L,0,p2);
	ListInsert(&L,0,p3);

//	printf("===病历表===\n");
//	ListPrint(L);

	char name[100];
	printf("请输入要查找的姓名:");
	scanf("%s",name);
	int res = ListFind(L,name);//res 为病人 x 在顺序表中的下标,x = -1说明不存在
	if(res == -1)
		printf("查无此人");
	else
		PrintById(L,res);
	
	return 0;
}



样例1

在这里插入图片描述


样例2

在这里插入图片描述

你可能感兴趣的:(数据结构实验,数据结构)