和黛玉学编程呀
C语言基础知识也完成了,所以就来谈谈数据结构吧,这篇主要是为了连接数据结构和C语言
很高兴又和大家见面啦,这节我们就讲顺序表,一起加油>
目录
前言:数据结构相关概念
什么的数据:网页里面肉眼可见的信息都是数据
什么是结构:我们使用大量数据的时候,为了更清楚方便看,就需要借用结构
概念:数据结构是计算机存储、组织数据的方式。数据结构是指相互之间存在一种或多种特定关系
的数据元素的集合。数据结构反映数据的内部构成,即数据由那部分构成,以什么方式构成,以及数据元素之间呈现的结构
总结:
1.可以存储数据
2.存储的数据能够方便查找
我们之前所学的数组就是最基础的数据结构,但是仅仅学数组是不能完成复杂的结构算法的
顺序表
线性表
线性表是n个具有相同特性的数据元素的有限序列。线性表是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串..
顺序表分类
另外,我们需要知道顺序表的底层结构是数组,对数组的封装,实现了常用的增加改等
静态顺序表:使用定长数组存储元素
缺点:空间大了浪费,小了不够用
动态顺序表: 按需申请
一般我们使用的都是动态顺序表,数组长度是可以动态申请和扩容(2倍或者1.5倍)的
顺序表的增、删、查、改
#define _CRT_SECURE_NO_WARNINGS
#define INIT_CAPACITY 4
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* a;
int size; // 有效数据个数
int capacity; // 空间容量
}SL;
//初始化和销毁
void SLInit(SL* ps);
void SLDestroy(SL* ps);
void SLPrint(SL* ps);
//扩容
void SLCheckCapacity(SL* ps);
//头部插入删除 / 尾部插入删除
void SLPushBack(SL* ps, SLDataType x);
void SLPopBack(SL* ps);
void SLPushFront(SL* ps, SLDataType x);
void SLPopFront(SL* ps);
//指定位置之前插入/删除数据
void SLInsert(SL* ps, int pos, SLDataType x);
void SLErase(SL* ps, int pos);
int SLFind(SL* ps, SLDataType x);
void SLInit(SL* ps)
{
assert(ps);
ps->a = malloc(sizeof(SLDataType) * INIT_CAPACITY);
if (ps->a == NULL)
{
perror("malloc error\n");
exit(1);
}
ps->size = 0;
ps->capacity = INIT_CAPACITY;
}
void SLDestroy(SL* ps) {
assert(ps);
if (ps->a) {
free(ps->a);
}
ps->a = NULL;
ps->size = ps->capacity = 0;
}
void SLPrint(SL* ps) {
assert(ps);
for (int i = 0; i < ps->size; ++i) {
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SLCheckCapacity(SL* ps) {
assert(ps);
if (ps->size == ps->capacity) {
SLDataType* tmp = (SLDataType*)realloc(ps->a, sizeof(SLDataType) * ps->capacity * 2);
if (tmp == NULL) {
perror("realloc fail!\n");
exit(1);
}
ps->a = tmp;
ps->capacity *= 2;
}
}
void SLPushBack(SL* ps, SLDataType x) {
assert(ps);
// SLCheckCapacity(ps);
// ps->a[ps->size++] = x;
SLInsert(ps, ps->size, x);
}
void SLPopBack(SL* ps) {
assert(ps);
assert(ps->size > 0);
// --ps->size;
SLErase(ps, ps->size - 1);
}
void SLPushFront(SL* ps, SLDataType x) {
assert(ps);
// SLCheckCapacity(ps);
// for (int i = ps->size; i > 0; --i) {
// ps->a[i] = ps->a[i-1];
// }
// ps->a[0] = x;
// ++ps->size;
SLInsert(ps, 0, x);
}
void SLPopFront(SL* ps) {
assert(ps);
assert(ps->size > 0);
// for (int i = 0; i < ps->size-1; ++i) {
// ps->a[i] = ps->a[i+1];
// }
// --ps->size;
SLErase(ps, 0);
}
//指定位置之前插入数据
void SLInsert(SL* ps, int pos, SLDataType x) {
assert(ps);
assert(pos <= ps->size && pos >= 0);
SLCheckCapacity(ps);
for (int i = ps->size; i > pos; --i) {
ps->a[i] = ps->a[i - 1];
}
ps->a[pos] = x;
++ps->size;
}
void SLErase(SL* ps, int pos) {
assert(ps);
assert(pos < ps->size && pos >= 0);
for (int i = pos; i < ps->size - 1; ++i) {
ps->a[i] = ps->a[i + 1];
}
--ps->size;
}
int SLFind(SL* ps, SLDataType x) {
assert(ps);
for (int i = 0; i < ps->size; ++i) {
if (ps->a[i] == x) {
return i;
}
}
return -1;
}
通讯录项目
需要存的数据:姓名,性别,电话,家庭住址,年龄.....
姓名.. | 名姓... | 名姓... | 名姓... |
通讯录需要实现的:
1.增加联系人数据
2.删除联系人数据
3.查找联系人数据
4.修改联系人数据
5.查看联系人
#SeqList.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include"contact.h"
//数据类型为PersonInfo
typedef struct PersonInfo SQDataType;
//typedef int SQDataType;
//动态顺序表
typedef struct SeqList {
SQDataType* a;
int size; //保存有效数据个数
int capacity; //空间的⼤⼩
}SLT;
//初始化与销毁
void SeqListInit(SLT* psl);
void SeqListDesTroy(SLT* psl);
void SeqListPrint(SLT sl);
void CheckCapacity(SLT* psl);
// 头部插⼊删除 / 尾部插⼊删除
void SeqListPushBack(SLT* psl, SQDataType x);
void SeqListPushFront(SLT* psl, SQDataType x);
void SeqListPopBack(SLT* psl);
void SeqListPopFront(SLT* psl);
//查找
int SeqListFind(SLT* psl, SQDataType x);
// 在指定位置之前插⼊/删除
//void SeqListInsert(SLT* psl, int pos, SQDataType x);
void SeqListInsert(SLT* psl, size_t pos, SQDataType x);
void SeqListErase(SLT* psl, size_t pos);
size_t SeqListSize(SLT* psl);
//修改指定位置的值
void SeqListAt(SLT* psl, size_t pos, SQDataType x);
//contact.h
#pragma once
#define NAME_MAX 100
#define SEX_MAX 4
#define TEL_MAX 11
#define ADDR_MAX 100
//前置声明
typedef struct SeqList contact;
//⽤⼾数据
typedef struct PersonInfo
{
char name[NAME_MAX];
char sex[SEX_MAX];
int age;
char tel[TEL_MAX];
char addr[ADDR_MAX];
}PeoInfo;
//初始化通讯录
void InitContact(contact* con);
//添加通讯录数据
void AddContact(contact* con);
//删除通讯录数据
void DelContact(contact* con);
//展⽰通讯录数据
void ShowContact(contact* con);
//查找通讯录数据
void FindContact(contact* con);
//修改通讯录数据
void ModifyContact(contact* con);
//销毁通讯录数据
void DestroyContact(contact* con);
#contact.c
#define _CRT_SECURE_NO_WARNINGS
#include"contact.h"
#include"SeqList.h"
void LoadContact(contact* con) {
FILE* pf = fopen("contact.txt", "rb");
if (pf == NULL) {
perror("fopen error!\n");
return;
}
//循环读取⽂件数据
PeoInfo info;
while (fread(&info, sizeof(PeoInfo), 1, pf))
{
SeqListPushBack(con, info);
}
printf("历史数据导⼊通讯录成功!\n");
}
void InitContact(contact* con) {
SeqListInit(con);
LoadContact(con);
}
void AddContact(contact* con) {
PeoInfo info;
printf("请输⼊姓名:\n");
scanf("%s", &info.name);
printf("请输⼊性别:\n");
scanf("%s", &info.sex);
printf("请输⼊年龄:\n");
scanf("%d", &info.age);
printf("请输⼊联系电话:\n");
scanf("%s", &info.tel);
printf("请输⼊地址:\n");
scanf("%s", &info.addr);
SeqListPushBack(con, info);
printf("插⼊成功!\n");
}
int FindByName(contact* con, char name[]) {
for (int i = 0; i < con->size; i++)
{
if (0 == strcmp(con->a[i].name, name)) {
return i;
}
}
return -1;
}
void DelContact(contact* con) {
char name[NAME_MAX];
printf("请输⼊要删除的⽤⼾姓名:\n");
scanf("%s", name);
int pos = FindByName(con, name);
if (pos < 0) {
printf("要删除的⽤⼾不存在,删除失败!\n");
return;
}
SeqListErase(con, pos);
printf("删除成功!\n");
}
void ShowContact(contact* con) {
printf("%-10s %-4s %-4s %15s %-20s\n", "姓名", "性别", "年龄", "联系电话",
for (int i = 0; i < con->size; i++)
{
printf("%-10s %-4s %-4d %15s %-20s\n",
con->a[i].name,
con->a[i].sex,
con->a[i].age,
con->a[i].tel,
con->a[i].addr);
}
}
void FindContact(contact* con) {
char name[NAME_MAX];
printf("请输⼊要查找的⽤⼾姓名:\n");
scanf("%s", name);
int pos = FindByName(con, name);
if (pos < 0) {
printf("要查找的⽤⼾不存在,查找失败!\n");
return;
}
printf("查找成功!\n");
printf("%-10s %-4s %-4d %15s %-20s\n",
con->a[pos].name,
con->a[pos].sex,
con->a[pos].age,
con->a[pos].tel,
con->a[pos].addr);
}
void ModifyContact(contact* con) {
char name[NAME_MAX];
printf("请输⼊要修改的⽤⼾名称:\n");
scanf("%s", name);
int pos = FindByName(con, name);
if (pos < 0) {
printf("要查找的⽤⼾不存在,修改失败!\n");
return;
}
PeoInfo info;
printf("请输⼊要修改的姓名:\n");
scanf("%s", &con->a[pos].name);
printf("请输⼊要修改的性别:\n");
scanf("%s", &con->a[pos].sex);
printf("请输⼊要修改的年龄:\n");
scanf("%d", &con->a[pos].age);
printf("请输⼊要修改的联系电话:\n");
scanf("%s", &con->a[pos].tel);
printf("请输⼊要修改的地址:\n");
scanf("%s", &con->a[pos].addr);
printf("修改成功!\n");
}
void SaveContact(contact* con) {
FILE* pf = fopen("contact.txt", "wb");
if (pf == NULL) {
perror("fopen error!\n");
return;
}
//将通讯录数据写⼊⽂件
for (int i = 0; i < con->size; i++)
{
fwrite(con->a + i, sizeof(PeoInfo), 1, pf);
}
printf("通讯录数据保存成功!\n");
}
void DestroyContact(contact* con) {
SaveContact(con);
SeqListDesTroy(con);
}
#test.c
#define _CRT_SECURE_NO_WARNINGS
#include"SeqList.h"
#include"contact.h"
void menu() {
//通讯录初始化
contact con;
InitContact(&con);
int op = -1;
do {
printf("********************************\n");
printf("*****1、添加⽤⼾ 2、删除⽤⼾*****\n");
printf("*****3、查找⽤⼾ 4、修改⽤⼾*****\n");
printf("*****5、展⽰⽤⼾ 0、退出 *****\n");
printf("********************************\n");
printf("请选择您的操作:\n");
scanf("%d", &op);
switch (op)
{
case 1:
AddContact(&con);
break;
case 2:
DelContact(&con);
break;
case 3:
FindContact(&con);
break;
case 4:
ModifyContact(&con);
break;
case 5:
ShowContact(&con);
break;
default:
printf("输⼊有误,请重新输⼊\n");
break;
}
} while (op != 0);
//销毁通讯录
DestroyContact(&con);
}
//contact.h
#pragma once
#define NAME_MAX 100
#define SEX_MAX 4
#define TEL_MAX 11
#define ADDR_MAX 100
//前置声明
typedef struct SeqList contact;
//⽤⼾数据
typedef struct PersonInfo
{
char name[NAME_MAX];
char sex[SEX_MAX];
int age;
char tel[TEL_MAX];
char addr[ADDR_MAX];
}PeoInfo;
//初始化通讯录
void InitContact(contact* con);
//添加通讯录数据
void AddContact(contact* con);
//删除通讯录数据
void DelContact(contact* con);
//展⽰通讯录数据
void ShowContact(contact* con);
//查找通讯录数据
void FindContact(contact* con);
//修改通讯录数据
void ModifyContact(contact* con);
//销毁通讯录数据
void DestroyContact(contact* con);
代码有点小多,可能需要大家自己尝试打代码理解,如果有错误,请指出
希望大家多多关注*:..゙((ε(*´・ω・)っ†*゚¨゚゚・*:..☆(•̥́_•ૅू˳)