数据结构课程设计 - 城市链表

这篇博客介绍了使用C语言进行数据结构课程设计,重点是实现城市链表。程序具备基本功能,但存在坐标对应城市唯一性的限制,无法处理城市重复的情况。作者鼓励读者深入学习数据结构。
摘要由CSDN通过智能技术生成
  • C语言数据结构课程设计-城市链表。基本功能已经完成
  • 此程序还有一些BUG:一个坐标只对应一个城市,不能重复。
  • 希望大家好好学习数据结构
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 构建城市链表
/**
 * 城市的属性:
 *  name:城市名
 *  area:城市的面积
 *  peopleCount:城市人口的数量
 *  top:城市的排行
 *  x:x轴距离
 *  y:y轴距离
 */
typedef struct City{
   
    char name[30];
    float area;
    int peopleCount;
    int top;
    int x;
    int y;
    char feature[50];
    struct City *next;
}City,*CityList;


// 初始化城市表(over)
CityList init(){
   
    City* head;
    head = (CityList)malloc(sizeof(City));
    if(head == NULL){
   
        printf("开辟失败");
        return NULL;
    }
    head->next = NULL;
//    printf("%d", sizeof(City));
//    printf("%d", sizeof(head));
    return head;
}

//添加城市(over)
void addCity(CityList head){
   
    CityList newCity = (City *)malloc(sizeof(City));
//    找到最后一个
    while (head->next != NULL){
   
        head = head->next;
    }
    printf("\n----------- 添加一个城市 ----------\n");
    printf("请输入城市名:");
    scanf("%s",&newCity->name);
    printf("请输入面积(平方千米):");
    scanf("%f",&newCity->area);
    printf("请输入人口数(万人):");
    scanf("%d",&newCity->peopleCount);
    printf("请输入城市X坐标:");
    scanf("%d",&newCity->x);
    printf("请输入城市y坐标:");
    scanf("%d",&newCity->y);
    printf("请输入城市等级:[1-5]:");
    scanf("%d",&newCity->top);
    while (newCity->top>5 || newCity->top <1)
/*带头结点头文件 hlinklist.h*/ #include <stdio.h> typedef int datatype; typedef struct link_node { datatype data; struct link_node *next; }node; /*初始化链表*/ node *init() { node *head; head=(node *)malloc(sizeof(node)); head->next=0; return head; } /*尾插法创建一个带头结点链表*/ node *creat(node *head) { node *r,*s; int x; r=head; printf("在新链表中输入数据以0结束:"); scanf("%d",&x); while(x) { s=(node*)malloc(sizeof(node)); s->data=x; r->next=s; r=s; scanf("%d",&x); } r->next=0; return head; } /*打印链表的结点值*/ void print(node *head) { node *p; p=head->next; if(!p) printf("链表内容为空!"); else while(p) { printf("%5d",p->data); p=p->next; } printf("\n"); } /*在单链表中查找第i个结点的地址*/ node *find(node *head,int i) { node *p=head; int j=0; if(i<0) {printf("不存在!");return 0;} if(i==0) return head; while(p&&i!=j) { p=p->next; j++; } return p; } /*在带头结点的单链表第i个位置后插入一个数*/ node *insert(node *head,int i,datatype x) { node *p,*q; q=find(head,i); if(!q) { printf("插入的位置不存在!\n");return head;} else { p=(node *)malloc(sizeof(node)); p->data=x; p->next=q->next; q->next=p; } return head; } /*在带头结点的单链表中删除一个为x的值*/ node *dele(node *head,datatype x) { node *pre=head,*p; p=head; while(p&&p->data!=x) { pre=p;p=p->next; } if(p) { pre->next=p->next; free(p); } return head; } /*把带头结点的单链表倒置(以结点形式 )*/ node *Dao_zhi(node *head) { node *p,*s; p=head->next; head->next=NULL; while(p) { s=p; p=p->next; s->next=head->next; head->next=s; } return head; } /*删除链表中重复的结点 */ node *dele1(node *head) { node *pre,*p,*q; if(head->next==0||!head->next->next) { printf("链表为空!"); return head; } //pre=head->next; q=head->next; while(q) { pre=q; p=q->next; while(p) { while(p&&q->data!=p->data) { pre=p;p=p->next; } if(p) { pre->next=p->next; free(p); } p=pre->next; } q=q->next; } return head; }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值