背景
动态顺序表代码构建,方便以后复习。
1、SeqList.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* array; //指向动态开辟的数组
int size; //存储有效数组的个数
int capacity; //动态开辟数组的容量大小
}SeqList;
void SeqListInit(SeqList* ps); //初始化顺序表
void SeqListDestory(SeqList* ps); //销毁顺序表
void SLCheckCapacity(SeqList* ps); //检查容量
void SLPushBack(SeqList* ps, SLDataType x); //尾插
void SLPopBack(SeqList* ps); //尾删
void SLPushfront(SeqList* ps, SLDataType x); //头插
void SLPopfront(SeqList* ps); //头删
//顺序表查找:找到的话返回该数组对应的下标,没找到返回-1
int SeqListFind(SeqList* ps, SLDataType x);
//顺序表指定位置插入:在ps指向的数组中的第pos个位置插入数据x
//用户指定的pos位置实际上对应数组中的pos-1位置
void SeqListInsert(SeqList* ps, int pos, SLDataType x);
//顺序表指定位置删除
void SeqListErase(SeqList* ps, int pos);
2、SeqList.c
#include"SeqList.h"
void SeqListInit(SeqList* ps)
{
assert(ps);
ps->array = (SLDataType*)malloc(sizeof(SLDataType) * 4);
if (ps->array == NULL) //是等于不是赋值
{
perror("malloc failed");
exit(-1);
}
ps->size = 0;
ps->capacity = 4;
}
void SeqListDestory(SeqList* ps)
{
free(ps->array); //释放数组
ps->array = NULL;
ps->size = 0;
ps->capacity = 0;
}
void SLCheckCapacity(SeqList* ps)
{
//printf("size: %d\n", ps->size);
//printf("size: %d\n", ps->capacity);
if (ps->size == ps->capacity)
{
SLDataType* tmp = (SLDataType*)realloc(ps->array,(ps->capacity) * sizeof(SLDataType) * 2); //将扩容后的首地址放入tmp;
if (tmp == NULL)
{
perror("realloc failed");
exit(-1);
}
ps->array = tmp; //更新为扩容后的首地址
ps->capacity *= 2; //更新扩容后的容量大小
}
}
void SLPushBack(SeqList* ps, SLDataType x)
{
SLCheckCapacity(ps); //你在干什么这里要看清楚呀 ps为一个指针 你传一个&ps给SLCheckCapacity()肯定是一个错误的呀
ps->array[ps->size] = x; //将x放入数组中
ps->size++; //更新数组大小
}
void SLPopBack(SeqList* ps)
{
//温柔的检查
//if (ps->size == 0)
//{
// return;
//}
//暴力检查
assert(ps->size > 0);
ps->size--;
}
void SLPushfront(SeqList* ps, SLDataType x) //头插
{
SLCheckCapacity(ps);
int end = ps->size -1;
while (end>=0)
{
ps->array[end + 1] = ps->array[end];
--end;
}
ps->array[0] = x;
ps->size++;
}
void SLPopfront(SeqList* ps)
{
SLCheckCapacity(ps);
int begin = 0;
while (begin < ps->size)
{
ps->array[begin] = ps->array[begin + 1];
begin++;
}
ps->size--;
}
int SeqListFind(SeqList* ps, SLDataType x)
{
for (int i = 0; i < ps->size; i++)
{
if (ps->array[i] == x)
{
return i;
}
}
return -1;
}
void SeqListInsert(SeqList* ps, int pos, SLDataType x)
{
assert(pos >= 0 && pos <= ps->size);
SLCheckCapacity(ps);
int end = ps->size - 1;
while (end >= pos-1)
{
ps->array[end + 1] = ps->array[end];
--end;
}
ps->array[pos-1] = x;
ps->size++;
}
void SeqListErase(SeqList* ps, int pos) //用户指定的pos位置实际上对应数组中的pos-1位置
{
pos = pos - 1;
assert(pos >= 0 && pos < (ps->size));
int begin = pos;
while (begin <= ps->size - 1)
{
ps->array[begin] = ps->array[begin + 1];
begin++;
}
ps->size--;
}
3、Test_SeqList
#include"SeqList.h"
void print(SeqList* s1)
{
for (int i = 0; i < s1->size; i++)
{
printf("%d ", s1->array[i]);
}
printf("\n");
}
//分组测试
void testSL1() //测试尾删 尾插
{
SeqList s1;
SeqListInit(&s1);
SLPushBack(&s1, 10);
SLPushBack(&s1, 20);
SLPushBack(&s1, 30);
SLPushBack(&s1, 40);
SLPushBack(&s1, 50);
SLPushBack(&s1, 60);
SLPushBack(&s1, 70);
SLPushBack(&s1, 80);
SLPushBack(&s1, 90);
SLPushBack(&s1, 100);
SLPushBack(&s1, 110);
SLPushBack(&s1, 120);
print(&s1);
SLPopBack(&s1);
SLPopBack(&s1);
SLPopBack(&s1);
print(&s1);
SeqListDestory(&s1);
}
void testSL2()
{
SeqList s1;
SeqListInit(&s1);
SLPushBack(&s1, 10);
SLPushBack(&s1, 20);
SLPushBack(&s1, 30);
SLPushBack(&s1, 40);
SLPushBack(&s1, 50);
SLPushBack(&s1, 60);
SLPushBack(&s1, 70);
print(&s1);
SLPushfront(&s1, 1);
SLPushfront(&s1, 2);
SLPushfront(&s1, 3);
print(&s1);
SLPopfront(&s1);
SLPopfront(&s1);
SLPopfront(&s1);
SLPopfront(&s1);
print(&s1);
SeqListFind(&s1, 50);
printf("%d", SeqListFind(&s1, 50));
SeqListDestory(&s1);
}
void testSL3()
{
SeqList s1;
SeqListInit(&s1);
SLPushBack(&s1, 10);
SLPushBack(&s1, 20);
SLPushBack(&s1, 30);
SLPushBack(&s1, 40);
SLPushBack(&s1, 50);
SLPushBack(&s1, 60);
SLPushBack(&s1, 70);
print(&s1);
SeqListInsert(&s1, 4, 1);
SeqListInsert(&s1, 6, 4);
print(&s1);
SeqListErase(&s1, 5);
SeqListErase(&s1, 2);
SeqListErase(&s1, 1);
SeqListErase(&s1, 4);
print(&s1);
SeqListDestory(&s1);
}
int main()
{
//testSL1();
//testSL2();
testSL3();
return 0;
}