主题:请高手帮我看看我这个城市链表的删除和插入部分那里出问题了啊
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define MAXSIZE 20
typedef struct city
{
char name[MAXSIZE];
float x;
float y;
struct city *next;
}
City;
int Menu()
{
int choice;
printf("************************\n");
printf(" 1.请输入城市的名称和坐标等资料\n");
printf(" 2.根据城市名查找\n");
printf(" 3.根据离中心坐标距离查找\n");
printf(" 4.插入\n");
printf(" 5.删除\n");
printf(" 6.退出\n");
printf("============================\n");
printf("请选择:");
scanf("%d", &choice);
return choice;
}
float Distance(float x,float y,float x0,float y0)
{
float dis;
dis=(x-x0)*(x-x0)+(y-y0)*(y-y0);
return dis;
}
City *Creat()
{
City *L, *r, *s;
r=L=(City *)malloc(sizeof(City));
while (strcmp(r->name,"-1")!=0)
{
s=(City *)malloc(sizeof(City));
if (s==NULL)
{
printf("错误:内存不足!");
break;
} else {
printf("城市名(输入“-1”结束):");
scanf("%s", s->name);
printf("坐标x,y:"); scanf("%f,%f",&s->x,&s->y);
r->next=s;
r=s;
}
}
r->next=NULL;
return L;
}
City *Locate(City *L, char *name)
{
City *p=L;
while ((p!=NULL) && (strcmp(p->name,name)!=0))
p=p->next;
return p;
}
void GetCity(City *L, float x,float y,float d)
{
City *p=L->next;
while (p!=NULL)
{
if (Distance(p->x,p->y,x,y)<d*d)
printf("城市名:%s\t\t坐标:(%.2f,%.2f)\n",p->name,p->x,p->y);
p=p->next;
}
}
void DeleteCity(City *L,char *name)
{
City *p=L;City *q;
while ((p!=NULL) && (strcmp(p->name,name)!=0))
p=p->next;
q=p->next;p->next=q->next;
free(q);
}
int main()
{
int ch=0,i,c;
float d,x,y;
char name[MAXSIZE];
City *L,*p;
while (ch!=6)
{
ch=Menu();
switch(ch)
{
case 1:
L=Creat();
break;
case 2:
printf("请输入要查找的城市名:"); scanf("%s",name);
p=Locate(L,name);
if (p!=NULL)
printf("城市名:%s\t\t坐标:(%.2f,%.2f)",p->name,p->x,p->y);
break;
case 3:
printf("请输入中心坐标:"); scanf("%f,%f",&x,&y);
printf("请输入距离:"); scanf("%f",&d);
GetCity(L,x,y,d);
break;
case 4:
printf("请输入要插入的城市名称和坐标资料\n"); scanf("%s",name);
L=Creat();
break;
case 5:
printf("请输入要删除的城市名称\n");scanf("%s",name);
DeleteCity(L,name);
if (p!=NULL)
case 6:
printf("结束程序。\n");
return 0;
break;
default:
printf("输入错误!请重新输入!\n\n");break;
}
printf("单击空格键继续...\n");
while ((c=getch())!=' ');
}
return 0;
}
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define MAXSIZE 20
typedef struct city
{
char name[MAXSIZE];
float x;
float y;
struct city *next;
}
City;
int Menu()
{
int choice;
printf("************************\n");
printf(" 1.请输入城市的名称和坐标等资料\n");
printf(" 2.根据城市名查找\n");
printf(" 3.根据离中心坐标距离查找\n");
printf(" 4.插入\n");
printf(" 5.删除\n");
printf(" 6.退出\n");
printf("============================\n");
printf("请选择:");
scanf("%d", &choice);
return choice;
}
float Distance(float x,float y,float x0,float y0)
{
float dis;
dis=(x-x0)*(x-x0)+(y-y0)*(y-y0);
return dis;
}
City *Creat()
{
City *L, *r, *s;
r=L=(City *)malloc(sizeof(City));
while (strcmp(r->name,"-1")!=0)
{
s=(City *)malloc(sizeof(City));
if (s==NULL)
{
printf("错误:内存不足!");
break;
} else {
printf("城市名(输入“-1”结束):");
scanf("%s", s->name);
printf("坐标x,y:"); scanf("%f,%f",&s->x,&s->y);
r->next=s;
r=s;
}
}
r->next=NULL;
return L;
}
City *Locate(City *L, char *name)
{
City *p=L;
while ((p!=NULL) && (strcmp(p->name,name)!=0))
p=p->next;
return p;
}
void GetCity(City *L, float x,float y,float d)
{
City *p=L->next;
while (p!=NULL)
{
if (Distance(p->x,p->y,x,y)<d*d)
printf("城市名:%s\t\t坐标:(%.2f,%.2f)\n",p->name,p->x,p->y);
p=p->next;
}
}
void DeleteCity(City *L,char *name)
{
City *p=L;City *q;
while ((p!=NULL) && (strcmp(p->name,name)!=0))
p=p->next;
q=p->next;p->next=q->next;
free(q);
}
int main()
{
int ch=0,i,c;
float d,x,y;
char name[MAXSIZE];
City *L,*p;
while (ch!=6)
{
ch=Menu();
switch(ch)
{
case 1:
L=Creat();
break;
case 2:
printf("请输入要查找的城市名:"); scanf("%s",name);
p=Locate(L,name);
if (p!=NULL)
printf("城市名:%s\t\t坐标:(%.2f,%.2f)",p->name,p->x,p->y);
break;
case 3:
printf("请输入中心坐标:"); scanf("%f,%f",&x,&y);
printf("请输入距离:"); scanf("%f",&d);
GetCity(L,x,y,d);
break;
case 4:
printf("请输入要插入的城市名称和坐标资料\n"); scanf("%s",name);
L=Creat();
break;
case 5:
printf("请输入要删除的城市名称\n");scanf("%s",name);
DeleteCity(L,name);
if (p!=NULL)
case 6:
printf("结束程序。\n");
return 0;
break;
default:
printf("输入错误!请重新输入!\n\n");break;
}
printf("单击空格键继续...\n");
while ((c=getch())!=' ');
}
return 0;
}