一. 线性表
^ 线性表是 n 个具有相同特性的数据元素的有限序列。线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表,链表,栈,队列,字符串......
^ 线性表在逻辑上是线性结构 ,也就是说是连续的一条直线,但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。
二. 顺序表
^ 顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储(底层逻辑)。
^ 顺序表的特性:逻辑结构是连续的,物理结构也一定是连续的
1> 顺序表的分类
cs
静态顺序表的定义
struct SeqList
{
int arr[100]; // 定长数组
int size; // 顺序表当前有效的数据个数
};
动态顺序表的定义
struct SeqList
{
int* arr;
int size; //有效的数据个数
int capacity; //空间大小
};
动态增容一般采用 2 倍增长
2> 初始化
cpp
// 顺序表的初始化
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
3> 销毁
cpp
// 顺序表的销毁
void SLDestroy(SL* ps)
{
if (ps->arr)
{
free(ps->arr);
}
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
4> 尾插
cpp
// 尾插
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
ps->arr[ps->size++] = x;
}
5> 头插
cpp
// 头插
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
// 先让顺序表中以有的数据整体往后移动一位
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1]; // arr[0]=arr[1]
}
ps->arr[0] = x;
ps->size++;
}
6> 尾删
cpp
// 尾删
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);
--ps->size;
}
7> 头删
cpp
// 头删
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
// 数据整体往前挪动一位
for (int i = 0; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1]; // arr[size-2]=arr[size-1];
}
ps->size -- ;
}
8> 在指定位置之前插入数据
cpp
// 在指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos>=0 && pos<=ps->size);
for (int i = ps->size; i > pos; i--)
{
ps->arr[i] = ps->arr[i - 1]; // arr[pos+1]=arr[pos]
}
ps->arr[pos] = x;
ps->size++;
}
9> 删除指定位置的数据
cpp
// 删除指定位置的数据
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
for (int i = pos; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
10> 查找
cpp
// 查找
int SLFind(SL* ps, SLDataType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->arr[i] == x)
{
// 找到了
return i;
}
}
// 没有找到
return -1;
}
11. SeqList.h
cpp
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
// 定义顺序表的结构
typedef int SLDataType;
// 动态顺序表
typedef struct SepList
{
SLDataType* arr;
int size; // 有效数据个数
int capacity; // 空间大小
}SL;
// 顺序表初始化
void SLInit(SL* ps);
// 顺序表的销毁
void SLDestroy(SL* ps);
// 打印
void SLPrint(SL s);
// 头部插入
void SLPushFront(SL* ps, SLDataType x);
// 尾部插入
void SLPushBack(SL* ps, SLDataType x);
// 头部删除
void SLPopFront(SL* ps);
// 尾部删除
void SLPopBack(SL* ps);
// 指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x);
// 指定位置之前删除数据
void SLErase(SL* ps, int pos);
// 查找数据
int SLFind(SL* ps, SLDataType x);
12. SepList.c
cpp
#include"SeqList.h";
// 顺序表的初始化
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
// 顺序表的销毁
void SLDestroy(SL* ps)
{
if (ps->arr)
{
free(ps->arr);
}
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
void SLCheckCapacity(SL* ps)
{
// 插入数据之前先看空间够不够
if (ps->capacity == ps->size)
{
// 申请空间
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
// 要申请多大的空间
SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc fail!");
exit(1); // 直接退出程序,不在继续执行
}
// 空间申请成功
ps->arr = tmp;
ps->capacity = newCapacity;
}
}
void SLPrint(SL s)
{
for (int i = 0; i < s.size; i++)
{
printf("%d ", s.arr[i]);
}
printf("\n");
}
// 尾插
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
ps->arr[ps->size++] = x;
}
// 头插
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
// 先让顺序表中以有的数据整体往后移动一位
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1]; // arr[0]=arr[1]
}
ps->arr[0] = x;
ps->size++;
}
// 尾删
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);
--ps->size;
}
// 头删
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
// 数据整体往前挪动一位
for (int i = 0; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1]; // arr[size-2]=arr[size-1];
}
ps->size -- ;
}
// 在指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos>=0 && pos<=ps->size);
for (int i = ps->size; i > pos; i--)
{
ps->arr[i] = ps->arr[i - 1]; // arr[pos+1]=arr[pos]
}
ps->arr[pos] = x;
ps->size++;
}
// 删除指定位置的数据
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
for (int i = pos; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
// 查找
int SLFind(SL* ps, SLDataType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->arr[i] == x)
{
// 找到了
return i;
}
}
// 没有找到
return -1;
}
13. test.c
cpp
#include"SeqList.h"
void SLTest01()
{
SL sl;
SLInit(&sl);
// 尾插
SLPushBack(&sl, 1);
SLPushBack(&sl, 2);
SLPushBack(&sl, 3);
SLPushBack(&sl, 4);
SLPrint(sl); // 1 2 3 4
// 头插
SLPushFront(&sl, 6);
SLPushFront(&sl, 5);
SLPrint(sl); // 5 6 1 2 3 4
// 尾删
SLPopBack(&sl);
SLPrint(sl); // 5 6 1 2 3
// 头删
SLPopFront(&sl);
SLPrint(sl); // 6 1 2 3
// 指定位置之前插入数据
SLInsert(&sl, 1, 99);
SLInsert(&sl, sl.size, 88);
SLPrint(sl); // 6 99 1 2 3 88
// 删除指定位置的数据
SLErase(&sl, 1);
SLPrint(sl); // 6 1 2 3 88
// 查找
int find = SLFind(&sl, 88);
if (find < 0)
{
printf("没有找到");
}
else
{
printf("找到了!下标为%d\n", find);
}
SLDestroy(&sl);
}
int main()
{
SLTest01();
return 0;
}
三. 顺序表的应用(通讯录)
1. SepList.h
cpp
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Contact.h"
// 定义顺序表的结构
//typedef int SLDataType;
typedef peoInfo SLDataType;
// 动态顺序表
typedef struct SeqList
{
SLDataType* arr;
int size; // 有效数据个数
int capacity; // 空间大小
}SL;
// 顺序表初始化
void SLInit(SL* ps);
// 顺序表的销毁
void SLDestroy(SL* ps);
// 打印
void SLPrint(SL s);
// 头部插入
void SLPushFront(SL* ps, SLDataType x);
// 尾部插入
void SLPushBack(SL* ps, SLDataType x);
// 头部删除
void SLPopFront(SL* ps);
// 尾部删除
void SLPopBack(SL* ps);
// 指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x);
// 指定位置之前删除数据
void SLErase(SL* ps, int pos);
// 查找数据
int SLFind(SL* ps, SLDataType x);
2. SepList.c
cpp
#include"SeqList.h"
// 顺序表的初始化
void SLInit(SL* ps)
{
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
// 顺序表的销毁
void SLDestroy(SL* ps)
{
if (ps->arr)
{
free(ps->arr);
}
ps->arr = NULL;
ps->size = ps->capacity = 0;
}
void SLCheckCapacity(SL* ps)
{
// 插入数据之前先看空间够不够
if (ps->capacity == ps->size)
{
// 申请空间
int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
// 要申请多大的空间
SLDataType* tmp = (SLDataType*)realloc(ps->arr, newCapacity * sizeof(SLDataType));
if (tmp == NULL)
{
perror("realloc fail!");
exit(1); // 直接退出程序,不在继续执行
}
// 空间申请成功
ps->arr = tmp;
ps->capacity = newCapacity;
}
}
//// 打印
//void SLPrint(SL s)
//{
// for (int i = 0; i < s.size; i++)
// {
// printf("%d ", s.arr[i]);
// }
// printf("\n");
//}
// 尾插
void SLPushBack(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
ps->arr[ps->size++] = x;
}
// 头插
void SLPushFront(SL* ps, SLDataType x)
{
assert(ps);
SLCheckCapacity(ps);
// 先让顺序表中以有的数据整体往后移动一位
for (int i = ps->size; i > 0; i--)
{
ps->arr[i] = ps->arr[i - 1]; // arr[0]=arr[1]
}
ps->arr[0] = x;
ps->size++;
}
// 尾删
void SLPopBack(SL* ps)
{
assert(ps);
assert(ps->size);
--ps->size;
}
// 头删
void SLPopFront(SL* ps)
{
assert(ps);
assert(ps->size);
// 数据整体往前挪动一位
for (int i = 0; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1]; // arr[size-2]=arr[size-1];
}
ps->size -- ;
}
// 在指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos>=0 && pos<=ps->size);
for (int i = ps->size; i > pos; i--)
{
ps->arr[i] = ps->arr[i - 1]; // arr[pos+1]=arr[pos]
}
ps->arr[pos] = x;
ps->size++;
}
// 删除指定位置的数据
void SLErase(SL* ps, int pos)
{
assert(ps);
assert(pos >= 0 && pos < ps->size);
for (int i = pos; i < ps->size - 1; i++)
{
ps->arr[i] = ps->arr[i + 1];
}
ps->size--;
}
//// 查找
//int SLFind(SL* ps, SLDataType x)
//{
// assert(ps);
//
// for (int i = 0; i < ps->size; i++)
// {
// if (ps->arr[i] == x)
// {
// // 找到了
// return i;
// }
// }
// // 没有找到
// return -1;
//}
3. Contact.h
cpp
#pragma once
#define NAME_MAX 20
#define GENDER_MAX 10
#define TEL_MAX 20
#define ADDR_MAX 100
// 定义联系人数据 结构
// 姓名 性别 年龄 电话 地址
typedef struct personInfo
{
char name[NAME_MAX];
char gender[GENDER_MAX];
int age;
char tel[TEL_MAX];
char addr[ADDR_MAX];
}peoInfo;
// 要用到顺序表相关的方法,对通讯录的操作实际就是对顺序表进行操作
// 给顺序表改个名字,叫做通讯录
typedef struct SeqList Contact;
// 通讯录相关的方法
// 通讯录的初始化
void ContactInit(Contact* con);
// 通讯录的销毁
void ContactDesTroy(Contact* con);
// 通讯录添加数据
void ContactAdd(Contact* con);
// 通讯录删除数据
void ContactDel(Contact* con);
// 通讯录的修改
void ContactModify(Contact* con);
// 通讯录查找
void ContactFind(Contact* con);
// 展示通讯录数据
void ContactShow(Contact* con);
4. Contact.c
cpp
#include"Contact.h"
#include"SeqList.h"
// 通讯录的初始化
void ContactInit(Contact* con)
{
// 实际上要进行的是顺序表的初始化
// 顺序表的初始化已经实现好了
SLInit(con);
}
// 通讯录的销毁
void ContactDesTroy(Contact* con)
{
SLDestroy(con);
}
// 通讯录添加数据
void ContactAdd(Contact* con)
{
// 获取用户输入的内容:姓名 性别 年龄 电话 地址
peoInfo info;
printf("请输入添加的联系人姓名:\n");
scanf("%s", info.name);
printf("请输入添加的联系人性别:\n");
scanf("%s", info.gender);
printf("请输入添加的联系人年龄:\n");
scanf("%d", &info.age);
printf("请输入添加的联系人电话:\n");
scanf("%s", info.tel);
printf("请输入添加的联系人地址:\n");
scanf("%s", info.addr);
// 往通讯录中添加联系人数据
SLPushBack(con, info);
}
int FindByName(Contact* con, char name[])
{
for (int i = 0; i < con->size; i++)
{
if (0 == strcmp(con->arr[i].name, name))
{
// 找到了
return i;
}
}
// 没有找到
return -1;
}
// 通讯录删除数据
void ContactDel(Contact* con)
{
// 要删除的数据必须要存在,才能执行删除操作
// 查找
char name[NAME_MAX];
printf("请输入要删除的联系人姓名:\n");
scanf("%s", name);
int find = FindByName(con, name);
if (find < 0)
{
printf("要删除的联系人不存在\n");
return;
}
// 要删除的联系人数据存在-->知道了要删除的联系人数据对应的下标
SLErase(con, find);
printf("删除成功\n");
}
// 展示通讯录数据
void ContactShow(Contact* con)
{
// 表头:姓名 性别 年龄 电话 地址
printf("%s %s %s %s %s \n", "姓名", "性别", "年龄", "电话", "地址");
// 遍历通讯录,按照格式打印每一个联系人数据
for (int i = 0; i < con->size; i++)
{
printf("%3s %3s %3d %3s %3s \n",
con->arr[i].name,
con->arr[i].gender,
con->arr[i].age,
con->arr[i].tel,
con->arr[i].addr);
}
}
// 通讯录的修改
void ContactModify(Contact* con)
{
// 要修改的联系人数据存在
char name[NAME_MAX];
printf("请输入要修改的用户姓名:\n");
scanf("%s", name);
int find = FindByName(con, name);
if (find < 0)
{
printf("要修改的联系人数据不存在\n");
return;
}
// 直接修改
printf("请输入新的姓名:\n");
scanf("%s", con->arr[find].name);
printf("请输入新的性别:\n");
scanf("%s", con->arr[find].gender);
printf("请输入新的年龄:\n");
scanf("%d", &con->arr[find].age);
printf("请输入新的电话:\n");
scanf("%s", con->arr[find].tel);
printf("请输入新的地址:\n");
scanf("%s", con->arr[find].addr);
printf("修改成功\n");
}
// 通讯录查找
void ContactFind(Contact* con)
{
char name[NAME_MAX];
printf("请输入要查找的联系人姓名\n");
scanf("%s", name);
int find = FindByName(con, name);
if (find < 0)
{
printf("要查找的联系人数据不存在\n");
return;
}
printf("%s %s %s %s %s\n", "姓名", "性别", "年龄", "电话", "地址");
printf("%3s %3s %3d %3s %3s\n", //手动调整一下格式
con->arr[find].name,
con->arr[find].gender,
con->arr[find].age,
con->arr[find].tel,
con->arr[find].addr
);
}
5. test.c
cpp
#include"SeqList.h"
//void SLTest01()
//{
// SL sl;
// SLInit(&sl);
//
// // 尾插
// SLPushBack(&sl, 1);
// SLPushBack(&sl, 2);
// SLPushBack(&sl, 3);
// SLPushBack(&sl, 4);
//
// SLPrint(sl); // 1 2 3 4
//
//
// // 头插
// SLPushFront(&sl, 6);
// SLPushFront(&sl, 5);
//
// SLPrint(sl); // 5 6 1 2 3 4
//
//
// // 尾删
// SLPopBack(&sl);
//
// SLPrint(sl); // 5 6 1 2 3
//
//
// // 头删
// SLPopFront(&sl);
//
// SLPrint(sl); // 6 1 2 3
//
//
// // 指定位置之前插入数据
// SLInsert(&sl, 1, 99);
// SLInsert(&sl, sl.size, 88);
//
// SLPrint(sl); // 6 99 1 2 3 88
//
//
// // 删除指定位置的数据
// SLErase(&sl, 1);
//
// SLPrint(sl); // 6 1 2 3 88
//
//
// // 查找
// int find = SLFind(&sl, 88);
//
// if (find < 0)
// {
// printf("没有找到");
// }
// else
// {
// printf("找到了!下标为%d\n", find);
// }
// SLDestroy(&sl);
//}
void menu()
{
printf("******************通讯录******************\n");
printf("*******1.增加联系人 2.删除联系人********\n");
printf("*******3.修改联系人 4.查找联系人********\n");
printf("*******5.展示联系人 0. 退出 *********\n");
printf("******************************************\n");
}
int main()
{
int op = -1;
Contact con;
ContactInit(&con);
do {
menu();
printf("请选择您的操作:\n");
scanf_s("%d", &op);
//要根据对应的op执行不同的操作
switch (op)
{
case 1:
ContactAdd(&con);
break;
case 2:
ContactDel(&con);
break;
case 3:
ContactModify(&con);
break;
case 4:
ContactFind(&con);
break;
case 5:
ContactShow(&con);
break;
case 0:
printf("退出通讯录....\n");
break;
default:
printf("输入错误,请重新选择您的操作!\n");
break;
}
} while (op != 0);
ContactDesTroy(&con);
return 0;
}
