今天我们试着创建一个顺序表,并实现对顺序表的增删查改
创建头文件用于声明函数,创建两个源文件(test.c和Seqlist.c),一个用来定义函数,一个用来测试。
Seqlist.h
#pragma once//防止文件被重复的包含
#ifndef __SEQLIST__H__
#define __SEQLIST__H__
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<assert.h>
//增强程序的可维护性
//#define MAX_SIZE 10
//typedef int SQDataType;
静态顺序表
问题:给少了不够用,给多了用不完浪费,不能灵活控制
//typedef struct SeqList
//{
// SQDataType a[MAX_SIZE];
// int size;
//}SL;
typedef struct SeqList SL;
//
增删查改等接口函数
//void SeqListInit(SL* ps);
尾插
//void SeqListPushBack(SL* ps, SQDataType x);
头插
//void SeqListPushFront(SL* ps, SQDataType x);
尾删
//void SeqListPopBack(SL* ps);
头删
//void SeqListPopFront(SL* ps);
//
//对int重新命名,便于维护代码
typedef int SQDataType;
typedef struct SeqList
{
SQDataType* a;
int size;
int capacity;
}SL;
//typedef struct SeqList SL;
//增删查改等接口函数
void SeqListInit(SL* ps);
//打印
void SeqlistPrint(SL* ps);
//尾插
void SeqListPushBack(SL* ps, SQDataType x);
//头插
void SeqListPushFront(SL* ps, SQDataType x);
//尾删
void SeqListPopBack(SL* ps);
//头删
void SeqListPopFront(SL* ps);
//中间插入
void SeqListInsert(SL* ps, int pos, SQDataType x);
//中间某个位置删除
void SeqListErase(SL* ps, int pos);
//销毁空间
void SeqListDestory(SL* ps);
//查
int SeqListFind(SL* ps, SQDataType x);
//改
void SeqListModity(SL* ps, int pos, SQDataType x);
#endif
Seqlist.c
#define _CRT_SECURE_NO_WARNINGS
#include "Seqlist.h"
//增删查改等接口函数
//初始化
void SeqListInit(SL* ps)//实参(形参的拷贝),实参的改变不会影响形参
{
ps->a = NULL;
ps->size = 0;
ps->capacity = 0;
//用字节对其进行初始化
//memset(ps->a,0,sizeof(SQDataType)*MAX_SIZE);
}
//给capacity扩容
void SeqlistCheckCapacity(SL* ps)
{
//满了就要扩容
if (ps->size == ps->capacity)
{
//开辟空间只能用指针,不可用数组
//realloc(原来的空间名字(数组指针),最终需要的空间(加上原来的空间)单位为字节),该函数扩容时根据要扩容的空间来定,后面空间足够时,直接扩容,不够的话再找一个空间重新开辟足够的空间(加上之前的空间)
//realloc(void* ptr,size_t size)
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
SQDataType* tmp = (SQDataType*)realloc(ps->a, ps->capacity * 2 * sizeof(SQDataType));
//malloc申请新的空间,realloc可以直接扩容
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);//非零状态表示异常退出零表示正常退出
}
else
{
ps->a = tmp;
ps->capacity = newcapacity;
}
}
}
//尾插
void SeqListPushBack(SL* ps, SQDataType x)
{
SeqlistCheckCapacity(ps);
//动态插入
ps->a[ps->size] = x;
ps->size++;
//静态插入
/*if (ps->size >= MAX_SIZE)
{
printf("SeqList is Full\n");
return ;
}
ps->a[ps->size] = x;
ps->size++;*/
}
//头插
void SeqListPushFront(SL* ps, SQDataType x)
{
SeqlistCheckCapacity(ps);
//1. 初始条件
//2. 结束条件
//3. 迭代过程
int end = ps->size - 1;
while (end >= 0)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[0] = x;
ps->size++;
}
//尾删
void SeqListPopBack(SL* ps)
{
assert(ps->size > 0);
ps->size--;
}
//头删
void SeqListPopFront(SL* ps)
{
int start = 1;
//start!=ps->size的原因是为了防止数组越界
while (start < ps->size)
{
ps->a[start - 1] = ps->a[start];
start++;
}
ps->size--;
}
//中间插入
void SeqListInsert(SL* ps, int pos, SQDataType x)
{
assert(pos < ps->size);
SeqlistCheckCapacity(ps);
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
end--;
}
ps->a[pos] = x;
ps->size++;
}
//中间某个位置删除
void SeqListErase(SL* ps, int pos)
{
assert(pos < ps->size);
int start = pos + 1;
while (start < ps->size)
{
ps->a[start - 1] = ps->a[start];
start++;
}
ps->size--;
}
void SeqlistPrint(SL* ps)
{
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
}
//查
int SeqListFind(SL* ps, SQDataType x)
{
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
return i;
}
return -1;
}
//改
void SeqListModity(SL* ps, int pos, SQDataType x)
{
assert(pos < ps->size);
ps->a[pos] = x;
}
//销毁空间
void SeqListDestory(SL* ps)
{
free(ps->a);
ps->a = NULL;
ps->capacity = ps->size = 0;
}