求具体的数据结构实验报告: 将若干城市的信息存入一个带头结点的单链表,结点中的城市信息包括城市名、
发布网友
发布时间:2022-04-30 15:19
我来回答
共2个回答
热心网友
时间:2023-10-08 17:18
#include <stdio.h>
#include <string.h>
#include <math.h>
typedef int Length;
typedef struct {
int x;
int y;
}coordinate;
typedef struct cityInfo {
char cityName[10];
coordinate cityCoor;
struct cityInfo* next;
}citylink;
void addCity(citylink*);
void insertCity(citylink*);
void delCity(citylink);
void amendCity(char*);
void list(void);
void coordinateSureCityname(void);
void nearerCity(coordinate,Length);
void shortestCitys(void);
citylink *head;
citylink *end;
void addCity(citylink* newCity)
{
end-> next = newCity;
end = newCity;
newCity-> next = NULL;
}
void insertCity(citylink* newcity)
{
int number,i;
citylink *p,*q;
p = head;
printf( "insert where\n ");
scanf( "%d ",&number);
for(i = 1;i < number;i++)
{
p = p-> next;
}
q = p-> next;
p-> next = newcity;
newcity-> next = q;
}
void delCity()
{
int number,i;
citylink *p;
p = head;
printf( "please input the number of city you want to del\n ");
scanf( "%d ",&number);
if(1 == number)
{
head = head-> next;
}
for(i = 2;i < number;i++)
{
p = p-> next;
}
p-> next = p-> next-> next;
}
void amendCity()
{
int x,y,result;
citylink *p;
char cityName[10];
printf( "please in put city ’s old name\n ");
scanf( "%s ",cityName);
fflush(stdin);
p = head;
while (p)
{
result = strcmp(p-> cityName,cityName);
if (0 == result)
{
printf( "please input city ’s new name!!\n ");
scanf( "%s ",&p-> cityName);
fflush(stdin);
printf( "please input city ’s new coordinate like this 88,99!!\n ");
scanf( "%d,%d ",&x,&y);
p-> cityCoor.x = x;
p-> cityCoor.y = y;
break;
}
p = p-> next;
}
if (result != 0)
{
printf( "There have not this city ");
}
}
void coordinateSureCityname(void)
{
int result;
citylink *p;
char cityName[10];
p = head;
printf( "please in put city ’s name\n ");
scanf( "%s ",cityName);
fflush(stdin);
while (p)
{
result = strcmp(p-> cityName,cityName);
if (0 == result)
{
printf( "city ’s coordinate is %d,%d ",p-> cityCoor.x,p-> cityCoor.y);
}
p = p-> next;
}
if (result != 0)
{
printf( "There has not such city ");
}
}
void nearerCity()
{
citylink* p;
int temp,x,y;
coordinate coord;
int Length;
printf( "please input coordinate like this 88,99\n ");
scanf( "%d,%d ",&x,&y);
coord.x = x;
coord.y = y;
fflush(stdin);
printf( "please input length\n ");
scanf( "%d ",&Length);
p = head;
while(p)
{
temp = sqrt(pow((coord.x - p-> cityCoor.x),2) + pow((coord.y - p-> cityCoor.y),2));
if (temp < Length)
{
printf( "city name : %s\n ",p-> cityName);
}
p = p-> next;
}
}
void list(void)
{
citylink *p;
p = head;
while(p)
{
printf( "city ’s name = %s,citys coordinate = %d , %d\n ",p-> cityName,p-> cityCoor.x,p-> cityCoor.y);
p = p-> next;
}
}
void main(void)
{
citylink city1;
citylink city2;
citylink city3;
citylink city4;
citylink city5;
citylink city6;
citylink city7
head = &city1;
city1.cityCoor.x = 12;
city1.cityCoor.y = 22;
strcpy(city1.cityName, "city1 " );
city1.next = &city2;
city2.cityCoor.x = 28;
city2.cityCoor.y = 95;
strcpy(city2.cityName, "city2 " );
city2.next = &city3;
city3.cityCoor.x = 32;
city3.cityCoor.y = 17;
strcpy(city3.cityName, "city3 " );
city3.next = &city4;
city4.cityCoor.x = 58;
city4.cityCoor.y = 98;
strcpy(city4.cityName, "city4 " );
city4.next = &city5;
city5.cityCoor.x = 31;
city5.cityCoor.y = 67;
strcpy(city5.cityName, "city5 " );
city5.next = &city6;
city6.cityCoor.x = 21;
city6.cityCoor.y = 99;
strcpy(city6.cityName, "city6 " );
city6.next = NULL;
head = &city1;
end = &city6;
city7.cityCoor.x = 19;
city7.cityCoor.y = 84;
strcpy(city7.cityName, "city7 " );
list();
printf( "\n ");
}
热心网友
时间:2023-10-08 17:18
多谢楼主及上位仁兄~~~