城市链表

#include<cstdio>
#include<stack>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define MAXN 100
struct p{
int x,y;
char name[20];
p* next;
p* prec;
}head,cend;
int n,x[MAXN+2],y[MAXN+2];
char name[MAXN][20];
void build(p* now,int pos){//建造链表
	p* next=(p*)malloc(sizeof(p));
	now->next=next;
	next->prec=now;
	next->x=x[pos];
	next->y=y[pos];
	 memcpy(next->name,name[pos], sizeof(name[pos])); 
	if(pos==n){
		next->next=&cend;
		(&cend)->prec=next;
		return;
	}
	build(next,pos+1);
	return;
}

void init(){//数据输入
	(&head)->prec=NULL;
	(&cend)->next=NULL;
	for(int i=1;i<=n;++i){
		cin>>x[i];
		cin>>y[i];
		cin>>name[i];
	}
	build(&head,1);

}
int dis(int x1,int y1,int x2,int y2){//计算2点距离
	return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}
void select(int x1,int y1,int D){
	p* now=&head;
	p* next;
	int countt=0;
	while(now->next!=NULL){
		next=now->next;
		if(dis(next->x,next->y,x1,y1)<=D*D){//判断距离
			if(next->x==0&&next->y==0&&strcmp(next->name,"")==0){//cend链尾情况不考虑,因为cend初始默认为x=0,y=0
				now=next;	
				continue;
			}
			cout<<"找到城市:"<<next->x<<' '<<next->y<<' '<<next->name<<endl;
			countt++;
		}
		now=next;	
	}
	if(countt==0)//没有符合条件的城市
	cout<<"没有这个城市"<<endl;
	return;
}

void selectc(char* cn){
	p* now=&head;
	p* next;
	int countt=0;
	while(now->next!=NULL){
		next=now->next;
		if(strcmp(next->name,cn)==0){//找到指定城市
			cout<<"找到城市:"<<next->x<<' '<<next->y<<' '<<next->name<<endl;
			return;
			countt++;
		}
		now=next;
	}
	if(countt==0)
	cout<<"没有这个城市"<<endl;
	return;
}

void insert(int x1,int y1,char* cn,int len){//插入一个城市信息
	p* now=(p*)malloc(sizeof(p));
	now->x=x1;
	now->y=y1;
	 memcpy(now->name,cn, len); 
	 now->name[len]='\0';
	p* prec=(&cend)->prec;
	now->prec=prec;
	now->next=(&cend);
	prec->next=now;
	(&cend)->prec=now;
	cout<<"已插入"<<now->x<<' '<<now->y<<' '<<now->name<<endl;
	return;
}

void delete1(char* cn){//删除一个城市信息
	p* now=&head;
	p* next;
	while(now->next!=NULL){
		next=now->next;
		if(strcmp(next->name,cn)==0){
			p* prec=next->prec;
			p* n=next->next;
			prec->next=n;
			n->prec=prec;
			cout<<"已删除"<<next->x<<' '<<next->y<<' '<<next->name<<endl;
			free(next);//释放内存
			return;
		}
		now=next;
	
	}
	cout<<"没有这个城市"<<endl;
	return;

}

void updata(int x1,int y1,char* cn){//更新一个城市信息
	p* now=&head;
	p* next;
	while(now->next!=NULL){
		next=now->next;
		if(strcmp(next->name,cn)==0){
			next->x=x1;
			next->y=y1;
			cout<<"已更新"<<next->x<<' '<<next->y<<' '<<next->name<<endl;
			return;
		}
		now=next;
	}
	cout<<"没有这个城市"<<endl;
	return;
}
int main(){
	cout<<"请输入你要设置的城市个数:之后(x+y+城市名)\n";
	cin>>n;
    int m,t,i;
	init();
   while(true){
	   cout<<endl;
    	cout<<"请选择操作类型 1:查找一个点范围内的城市;2:查找一个城市;3: 插入;4:删除;5:更新 6:退出\n";
		int type;//操作类型
		scanf("%d",&type);
		if(type==1){
			cout<<"请输入x+y+D\n";
			int x1,y1,D;
			cin>>x1>>y1>>D;
		select(x1,y1,D);
		}
		else if(type==2){
			char cn1[20];
			cout<<"请输入城市名\n";
			cin>>cn1;
		selectc(cn1);
		
		}
		else if(type==3){
			cout<<"请输入x+y+城市名\n";
			int x1,y1;
			char cn1[20];
			cin>>x1>>y1>>cn1;
		insert(x1,y1,cn1,sizeof(cn1));
		}
		else if(type==4){
			char cn1[20];
			cout<<"请输入城市名\n";
			cin>>cn1;
		delete1(cn1);
		}
		else if(type==5){
		cout<<"请输入x+y+城市名\n";
			int x1,y1;
			char cn1[20];
			cin>>x1>>y1>>cn1;
		updata(x1,y1,cn1);//根据一个城市名字找
		}
		else if(type==6)
			break;
   }
return 0;
}

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值