#include<stdio.h>
#include<malloc.h>
typedef struct Node{
char data1;
int data2,data3;
struct Node *next;
}Node,*LinkList L;
void Initlinklist(LinkList *l)
{
*l=(LinkList)malloc(sizeof(Node));
(*l)->next=NULL;
}
void CreateFromTail(LinkList L)
{
Node *r, *city;
char *b;
int flag=1,c,d;
r=L;
while(flag)
{
printf("input the City:\n");
b=gets();//获取城市名
printf("input the site:\n");
scanf("%d%d",&c,&d);//获取城市坐标
if(!strcmp(b,"$"))//跳出条件
{
city=(Node*)malloc(sizeof(Node));
city->data1=*b;
city->data2=c;
city->data3=d;
r->next=city;
r=city;
}
else
{
flag=0;
r->next=NULL;
}
}//while
}
int search()//查找城市坐标
{
LinkList L;
Node *p,*q;
int flag=0;
int i;
char c;
Initlinklist(&L);
printf("input the element,end by '$'\n");
CreateFromTail(L);//尾插建立单链表
p =L->next;
printf("Which city are you want to search:\n");
q=gets();//获取城市名
while(p!=NULL)
{
if(strcmp(p->data1,q)){//比较输入的城市名是否存在
printf("%s's site is:(%d,%d)",p->data1,p->data2,p->data3);//输出名字于坐标
p=NULL;//跳出循环
break;
}
p=p->next;//下一位
}//while
}
int count()//计算符合要求的城市
{
LinkList L;
Node *p,*q;
int flag=0;
int i,e,f,g,h;
char c;
Initlinklist(&L);
printf("input the element,end by '$'\n");
CreateFromTail(L);//尾插建立单链表
p =L->next;
printf("Input the site:\n");
scanf("%d%d",&e,&f);//获取坐标
printf("Input the distance:\n");
scanf("%d",&g);//获取距离
while(p!=NULL)
{
f=e+f+g;//条件2的满足条件
g=p->data2+p->data3;
if(g<=f){
printf("%s's site is:(%d,%d)\n",p->data1,p->data2,p->data3);
h++;//计算符合要求的城市个数
}
p=p->next;
}//while
printf("There are %d cities to meet the requirements\n",h);
}