我如何能够连接这些城市 [英] How I Can Connect These Cities
问题描述
typedef struct city
{
char名称[25];
int高度;
城市* Connection1,* Connection2,
* Connection3;
城市*下一个;
}城市;
现在我们有我们可以使用的城市类型:
折叠|复制代码
City * London =(City *)malloc(sizeof
(City));
London-> Height = 24;
好的 - 有些白痴关闭了你以前的问题而没看我们怎么做......
所以:你有结构现在:
所有你需要做的就是写几个功能:
1)创建一个城市
2)将它添加到链接城市列表。
create函数调用malloc,填写详细信息并返回指针:
// 创建城市
City * CreateCity( char * name, int height)
{
City * newCity =(City *)malloc(的sizeof 跨度>(城市));
...
return newCity;
}你应该可以在中间填写!
我如何为10个城市做到这一点
和应返回的值
城市* addcity( char * name, int height)
{
City * c1 =(City *)malloc
( sizeof (City));
C1->名称= 伦敦;
C1->高度= 400 ;
return c1;
// 我可以为10个城市执行此操作
嗯,不太完美:它是一个函数,所以你的想法是用不同的参数调用它十次:
< pre lang =c ++> AddCity(CreateCity( London, 24 跨度>));
AddCity(CreateCity( New York, 124 跨度>));
AddCity(CreateCity( Wenquan, 5019 跨度>));
PrintCities(城市);
CreateCity创建实际城市并将其返回,AddCity将其添加到您的链接列表中。然后PrintCities遍历列表并打印所有城市。 (你需要写这些,但它们非常简单)
在CreateCity中,你将名称和高度复制到创建的城市结构中(为安全起见所有链接为零):
// 创建城市
City * CreateCity( char * name, int height)
{
char c;
int i;
City * newCity =(City *)malloc( sizeof (City));
// 复制城市名称
for (i = 0 ; i< 25 ; i ++)
{
c = *(name + i);
newCity->名称[i] = c;
if (c == ' \ 0') break ;
}
newCity->身高=身高;
newCity-> Connection1 = 0 ;
newCity-> Connection2 = 0 ;
newCity-> Connection3 = 0 ;
newCity-> Next = 0 ;
返回 newCity;
}
一旦你完成所有工作,你可以添加城市之间的连接并开始添加你的功能原作业问题。
这有意义吗?
typedef struct city
{
char Name[ 25 ];
int Height;
City *Connection1, *Connection2,
*Connection3;
City *Next;
} City;
Now we have a City type we can use:
Collapse | Copy Code
City *London = (City*) malloc( sizeof
(City));
London->Height = 24 ;
OK - some moron closed your previous question without looking to see how we were doing...
So: you have the structure now:
All you need to do is write a couple of fucntions:
1) Create a city
2) Add it to a linked list of cities.
The create function calls malloc, fills in the details and returns the pointer:
// Create a city City *CreateCity(char* name, int height) { City* newCity = (City*) malloc(sizeof(City)); ... return newCity; }You should be able to fill in the middle!
"how i can do this for 10 cities
and what value should be return"
City *addcity( char * name, int height) { City* c1= (City*) malloc ( sizeof (City)); C1->Name="London"; C1->Height=400; return c1; // can i do this for 10 cities
Well, not quite: it's a function, so the idea is that you call it ten times with different parameters:
AddCity(CreateCity("London", 24)); AddCity(CreateCity("New York", 124)); AddCity(CreateCity("Wenquan", 5019)); PrintCities(Cities);
CreateCity creates the actual city and returns it, AddCity adds it into your linked list. PrintCities then walks the list and prints all the cities. (You will need to write these, but they are pretty simple)
In CreateCity you copy the Name and Height into the created city structure (and zero all the links for safety):
// Create a city City *CreateCity(char* name, int height) { char c; int i; City* newCity = (City*) malloc(sizeof(City)); // Copy city name for (i = 0; i < 25; i++) { c = *(name + i); newCity->Name[i] = c; if (c == '\0') break; } newCity->Height = height; newCity->Connection1 = 0; newCity->Connection2 = 0; newCity->Connection3 = 0; newCity->Next = 0; return newCity; }
Once you have that all working, you can add the connections between the cities and start adding the functions from your original homework question.
Does that make sense?
这篇关于我如何能够连接这些城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!