汽车牌照管理系统数据结构c语言代码
时间: 2023-10-16 21:06:41 浏览: 100
C语言车牌号信息管理系统
5星 · 资源好评率100%
以下是一个基于链表的汽车牌照管理系统的数据结构C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 20
// 定义一个车辆结构体
typedef struct Car {
char plate_num[MAX_LEN]; // 车牌号码
char owner[MAX_LEN]; // 车主姓名
int year; // 购买年份
struct Car *next; // 指向下一个车辆的指针
} Car;
// 插入新车辆记录
void insert_car(Car **head, char *plate_num, char *owner, int year) {
// 创建一个新车辆结构体
Car *new_car = (Car *) malloc(sizeof(Car));
strcpy(new_car->plate_num, plate_num);
strcpy(new_car->owner, owner);
new_car->year = year;
new_car->next = NULL;
// 如果链表为空,直接将新车辆作为头结点
if (*head == NULL) {
*head = new_car;
return;
}
// 找到链表末尾,并将新车辆插入链表尾部
Car *cur = *head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = new_car;
}
// 删除指定车辆记录
void delete_car(Car **head, char *plate_num) {
// 如果链表为空,则直接返回
if (*head == NULL) {
return;
}
// 如果要删除的车辆是头结点,直接将头结点指向下一个节点
if (strcmp((*head)->plate_num, plate_num) == 0) {
Car *temp = *head;
*head = (*head)->next;
free(temp);
return;
}
// 找到要删除的车辆节点,并将其从链表中删除
Car *cur = *head;
while (cur->next != NULL && strcmp(cur->next->plate_num, plate_num) != 0) {
cur = cur->next;
}
if (cur->next != NULL) {
Car *temp = cur->next;
cur->next = temp->next;
free(temp);
}
}
// 修改指定车辆记录
void modify_car(Car *head, char *plate_num, char *new_owner, int new_year) {
// 找到要修改的车辆节点,并更新车主姓名和购买年份
Car *cur = head;
while (cur != NULL && strcmp(cur->plate_num, plate_num) != 0) {
cur = cur->next;
}
if (cur != NULL) {
strcpy(cur->owner, new_owner);
cur->year = new_year;
}
}
// 查找指定车辆记录
void find_car(Car *head, char *plate_num) {
// 找到要查找的车辆节点,并输出车辆信息
Car *cur = head;
while (cur != NULL && strcmp(cur->plate_num, plate_num) != 0) {
cur = cur->next;
}
if (cur != NULL) {
printf("车牌号码:%s,车主姓名:%s,购买年份:%d\n", cur->plate_num, cur->owner, cur->year);
} else {
printf("未找到车牌号码为%s的车辆记录\n", plate_num);
}
}
// 输出所有车辆记录
void print_all_cars(Car *head) {
// 遍历链表,并输出每辆车的信息
Car *cur = head;
while (cur != NULL) {
printf("车牌号码:%s,车主姓名:%s,购买年份:%d\n", cur->plate_num, cur->owner, cur->year);
cur = cur->next;
}
}
int main() {
Car *head = NULL;
// 插入新车辆记录
insert_car(&head, "京A12345", "张三", 2020);
insert_car(&head, "京B67890", "李四", 2019);
insert_car(&head, "京C24680", "王五", 2018);
// 输出所有车辆记录
print_all_cars(head);
// 删除一辆车辆记录
delete_car(&head, "京B67890");
// 输出所有车辆记录
print_all_cars(head);
// 修改一辆车辆记录
modify_car(head, "京A12345", "赵六", 2021);
// 输出所有车辆记录
print_all_cars(head);
// 查找一辆车辆记录
find_car(head, "京C24680");
return 0;
}
```
阅读全文