#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct Node{
char data1[20];
int data2,data3;
struct Node *next;
}Node,*LinkList;
void Initlinklist(LinkList *l)
{
*l=(LinkList)malloc(sizeof(Node));
(*l)->next=NULL;
}
void CreateFromTail(LinkList L)
{
Node *r, *city;
char a[10][20]={'\0'},*b;
int i=0,flag=1,c,d;
r=L;
while(flag)
{
printf("input the City:\n");
gets(a[i]);
printf("input the site:\n");
scanf("%d%d",&c,&d);
if(strcmp(a[i],"$"))
{
city=(Node*)malloc(sizeof(Node));
strcpy(city->data1,a[i]);
city->data2=c;
city->data3=d;
r->next=city;
r=city;
}
else{
flag=0;
r->next=NULL;
}
if(i>9){//跳出
flag=0;
r->next=NULL;
}
i++;
}
}
int search()
{
LinkList L;
Node *p;
int flag=0;
int i;
char c,a[20];
Initlinklist(&L);
printf("input the element,end by '$'\n");
CreateFromTail(L);
p =L->next;
printf("Which city are you want to search:\n");
gets(a);
while(p!=NULL)
{
if(!strcmp(p->data1,a)){
printf("%s's site is:(%d,%d)",p->data1,p->data2,p->data3);
p=NULL;
break;
}
p=p->next;
}
}
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;
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;
}
printf("There are %d cities to meet the requirements\n",h);
}
void main()
{
int a;
printf("search or count:\n1.search\t2.count\n");
scanf("%d",&a);
if(a==1)
search();
else if(a==2)
count();
else
printf("error program exit!");
getch();
}