顺序表(数据结构)

1.顺序表的概念及结构

1.1线性表

线性表(linear list)是n个具有相同特性的数据元素的有限序列。 线性表是一种在实际中广泛使
用的数据结构,常见的线性表:顺序表、链表、栈、队列、字符串…
线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,
线性表在物理上存储时,通常以数组和链式结构的形式存储。

2.顺序表分类

顺序表与数组的区别:顺序表的底层结构是数组,对数组的封装,实现了常用的增删改查等接口
顺序表分类:静态顺序表、动态顺序表

3.动态顺序表的实现

#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);

4.顺序表的应用

4.1 基于动态顺序表实现通讯录

C语言基础要求:结构体、动态内存管理、顺序表、文件操作

4.2 功能要求

1)至少能够存储100个人的通讯信息
2)能够保存用户信息:名字、性别、年龄、电话、地址等
3)增加联系人信息
4)删除指定联系人
5)查找制定联系人
6)修改指定联系人
7)显示联系人信息

4.3 代码实现

//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  
#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);  
}
  • 31
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

写代码的大学生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值