一、Contact.h
1.1.作用 :定义个人信息结构体 peoInfo 和常量。
1.2.代码:
代码如下:
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#define MAX_NAME 20
#define MAX_PHONE 20
#define MAX_GENDER 10
#define MAX_ADDR 20
typedef struct peoInfo{
char name[MAX_NAME];
int age;
char gender[MAX_GENDER];
char addr[MAX_ADDR];
char phone[MAX_PHONE];
}peoInfo;
struct SeqList;
二、SeqList.h
2.1.作用 :定义顺序表结构,设置数据类型为 peoInfo,并将顺序表别名为 Contact。
2.2.代码
代码如下:
#pragma once
#include"Contact.h"
typedef peoInfo SLDataType;
typedef struct SeqList {
SLDataType* a;
int size;
int capacity;
}SL;
typedef struct SeqList Contact; // 前置声明:告诉编译器有一个叫 SeqList 的结构体,因为 Contact.h 需要被 SeqList.h 包含,而 SeqList 的定义在 SeqList.h 中 为了避免循环依赖,这里只声明,具体定义在 SeqList.h
void SLCheckCapacity(SL* sl);
void SLprint(SL* sl);
void SLInit(SL* sl);
void SLPushFront(SL* sl, SLDataType x);
void SLPushBack(SL* sl, SLDataType x);
void SLInsert(SL* sl, int pos, SLDataType x);
void SLRemoveAll(SL* sl, SLDataType x);
void SLPopFront(SL* sl);
void SLPopBack(SL* sl);
void SLErase(SL* sl, int pos);
//查找元素
int SLFind(SL* sl, SLDataType x);
//修改指定位置的数据
void SLModify(SL* sl, int pos, SLDataType);
三、3-23.c
3.1.作用:实现顺序表的底层增删查改逻辑。
3.2.代码
代码如下:
// 打印单个
void PrintOne(const peoInfo* p) {
printf("%-10s %3d %-6s %-15s %-20s\n", p->name, p->age, p->gender, p->tel, p->addr);
}
// 打印所有
void SLPrint(SL* sl) {
assert(sl);
if (sl->size == 0) {
printf("通讯录为空。\n");
return;
}
printf("\n%-10s %-5s %-6s %-15s %-20s\n", "姓名", "年龄", "性别", "电话", "地址");
printf("------------------------------------------------------------\n");
for (int i = 0; i < sl->size; i++) {
PrintOne(&sl->a[i]);
}
printf("------------------------------------------------------------\n");
}