【数据结构入门实验C语言版】城市链表

实验内容描述:

2 .城市链表(设计性实验)
问题描述
将若干城市的信息存入一个带头结点的单向链表。结点中的城市信息包括城市名、城市的位置坐
标。要求能够利用城市名和位置坐标进行有关查找、插入、删除、更新等操作。
基本要求
(1) 给定一个城市名,返回其位置坐标。
(2) 给定一个位置坐标 P 和一个距离 D ,返回所有与 P 的距离小于等于 D 的城市。
测试数据
由读者依据软件工程的测试技术自己确定。注意测试边界数据

简述:

此次实验实际上就是要用到上一篇写的单链表,但实际上比那个简单,因为要求的操作很少,在此仅说一下一些需要注意的问题:

第一个是结构体内容的变化,这里的数据应该包括浮点类型的xyz三坐标,还应该包括字符类型的城市名字,在这里我用了一个char指针去实现

第二个是字符数组是可以装汉字的,只不过,一个汉字相当于原来的两个字符

第三个是在往里面输入城市名字的时候,必须先给字符指针用malloc分配内存

其它基本上没什么问题,跟普通单链表差不多

下面是代码and测试图:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
#define OK 2
#define ERROR 0
typedef char CityName;
typedef int Status;
typedef struct Cityinfo{
	CityName *name;
	double x;
	double y;
	double z;
    struct	Cityinfo *next;
}CityInfo,*CityPtr;
CityPtr CreatList(void);//创造城市链表 
void FindCity(CityPtr);//查找城市 
Status InsertCity(CityPtr);// 插入城市 
Status DeleteCity(CityPtr);//删除城市 
void CheckCity(CityPtr);//遍历城市 
void ReturnCity(CityPtr,double,double,double,double);//寻找距离xyz小于distance的所有城市 
int main()
{
	CityPtr L;
	L=CreatList();
	CheckCity(L);
	FindCity(L);
    InsertCity(L); 
    CheckCity(L);
    DeleteCity(L);
    CheckCity(L);
    printf("下面开始寻找离坐标xyz的距离小于distance的所有城市:\n");
	printf("请分别输入x y z distance:");
	double x,y,z;
	double distance;
	scanf("%lf%lf%lf%lf",&x,&y,&z,&distance);
	ReturnCity(L,x,y,z,distance); 
	return 0;
}
CityPtr CreatList(void)
{
	CityPtr L=(CityPtr)malloc(sizeof(CityInfo));
	L->next=NULL;
	CityPtr p,Tail;
	Tail=L;
	int n,i;
	CityName *s;
	printf("请输入您想创建的城市个数:");
	scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    	p=(CityPtr)malloc(sizeof(CityInfo));
    	printf("请输入第%d个城市的名称:",i);
    	s=(CityName*)malloc(sizeof(CityName)*20);
    	scanf("%s",s);
    	p->name=s;
    	printf("请分别输入第%d个城市的x坐标,y坐标,z坐标:",i);
		scanf("%lf%lf%lf",&p->x,&p->y,&p->z); 
    	Tail->next=p;
    	Tail=p;
    	Tail->next=NULL;
	}
	return L;
}
void CheckCity(CityPtr L)
{
	CityPtr p=L->next;
	int cnt=0;
	while(p)
	{
		cnt++;
		printf("第%d个城市的名字为%s,x坐标为%lf,y坐标为%lf,z坐标为%lf\n",cnt,p->name,p->x,p->y,p->z);
		p=p->next;
	}
}
void FindCity(CityPtr L)
{
	int choose;
	double x,y,z;
	CityName *s;
	s=(CityName*)malloc(sizeof(CityName)*20);
	int cnt=0;
	CityPtr p=L->next;
	printf("选择查找城市的方式,输入1为利用名称查找,输入2为利用坐标查找:");
	scanf("%d",&choose);
	switch(choose)
	{
		case 1:
			printf("请输入你要查找的城市的名字:");
			scanf("%s",s);
            while(p)
			{
				cnt++;
				if(!(strcmp(s,p->name)))
				{
				printf("成功找到!,该城市在链表第%d个位置\n",cnt);	
				return;
				}
				p=p->next;
			}	
			printf("没有查找到相关城市\n");
	        return;
		case 2:
			printf("清分别输入你要查找的城市的x,y,z坐标:");
			scanf("%lf%lf%lf",&x,&y,&z);
			while(p)
			{
				cnt++;
				if((x==p->x)&&(y==p->y)&&(z==p->z))
				{
				printf("成功找到!,该城市在链表第%d个位置\n",cnt);	
				return;
				}
				p=p->next;
		    } 
		    printf("没有查找到相关城市\n");
	        return;
        default:
        	printf("输入的内容有误!!\n");
			break; 
    } 
    return;
}
Status InsertCity(CityPtr L)
{
	CityPtr p;
	CityName *s;
	s=(CityName*)malloc(sizeof(CityName)*20);
	p=(CityPtr)malloc(sizeof(CityInfo));
	printf("输入你想插入的城市的名字:");
	scanf("%s",s);
	p->name=s;
	double x,y,z;
    printf("清分别输入你要添加的城市的x,y,z坐标:");
    scanf("%lf%lf%lf",&x,&y,&z);
    p->x=x;
	p->y=y;
	p->z=z;
	printf("你要插到第几个城市的前面?输入数字以表示:"); 
	int i;
	scanf("%d",&i);
	CityPtr q=L;
	while(--i)
	{
		q=q->next;
	}
	p->next=q->next;
	q->next=p;
	return OK;
}
Status DeleteCity(CityPtr L)
{
	CityPtr p=L;
	CityName *s;
	CityPtr q;
	s=(CityName*)malloc(sizeof(CityName)*20);
	printf("输入你想删除的城市的名字:");
	scanf("%s",s);
	while(p->next)
	{
		if(!(strcmp(s,p->next->name)))
		{
			q=p->next;
			p->next=q->next;
			free(q);
			printf("删除成功!\n");
			return OK;
		}
		p=p->next;
    } 
    printf("删除失败,没有发现对应的城市!\n");
    return ERROR;
}
void ReturnCity(CityPtr L,double x,double y,double z,double distance)
{
	CityPtr p=L->next;
	while(p)
	{
		if((p->x-x)*(p->x-x)+(p->y-y)*(p->y-y)+(p->z-z)*(p->z-z)<distance*distance)
		printf("%s\n",p->name);
		p=p->next;
	}
}

测试图:

 

 

  • 9
    点赞
  • 83
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值