1.在win32x下新建项目,且勾选点掉SLD选项(作用是检查更严格,scanf变为scanf_s).
2.新建头文件SeqList.h,再在SeqList.h中新建SeqList.c文件
3.SeqList.h中放接口,函数和结构的声明,SeqList.c中是具体的实现程序,text.c中放的测试文件。
1,SeqList.c
在这里插入代码片
```#include "SeqList.h"
void SeqListInit(SL* ps)
{
/*s.size = 0;
s.a = NULL;
s.capacity = 0;*/
ps->a = (SLDataType*)malloc(sizeof(SLDataType) * 4);
if (ps->a == NULL)
{
printf("申请内存失败\n");
exit(-1);
}
ps->size = 0;
ps->capacity = 4;
}
void SeqListDestory(SL* ps)
{
free(ps->a);
ps->a = NULL;
ps->size = ps->capacity = 0;
}
void SeqListPrint(SL* ps)
{
assert(ps);
for (int i = 0; i < ps->size; ++i)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SeqListCheckCapacity(SL* ps)
{
// 如果满了需要增容
if (ps->size == ps->capacity)
{
ps->capacity *= 2;
ps->a = (SLDataType*)realloc(ps->a, sizeof(SLDataType)*ps->capacity);
if (ps->a == NULL)
{
printf("扩容失败\n");
exit(-1);
}
}
}
// 尾插尾删 头插头删
void SeqListPushBack(SL* ps, SLDataType x)
{
//assert(ps);
//SeqListCheckCapacity(ps);
//ps->a[ps->size] = x;
//ps->size++;
SeqListInsert(ps, ps->size, x);
}
void SeqListPopBack(SL* ps)
{
assert(ps);
//ps->a[ps->size - 1] = 0;
//ps->size--;
SeqListErase(ps, ps->size - 1);
}
void SeqListPushFront(SL* ps, SLDataType x)
{
//assert(ps);
//SeqListCheckCapacity(ps);
//int end = ps->size - 1;
//while (end >= 0)
//{
// ps->a[end + 1] = ps->a[end];
// --end;
//}
//ps->a[0] = x;
//ps->size++;
SeqListInsert(ps, 0, x);
}
void SeqListPopFront(SL* ps)
{
/*assert(ps);
int start = 0;
while (start < ps->size-1)
{
ps->a[start] = ps->a[start+1];
++start;
}
ps->size--;*/
SeqListErase(ps, 0);
}
// 任意位置的插入删除
// size_t 就是 unsigned int
void SeqListInsert(SL* ps, int pos, SLDataType x)
{
assert(ps);
assert(pos <= ps->size && pos >= 0);
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(ps);
assert(pos < ps->size && pos >= 0);
int start = pos;
while (start < ps->size - 1)
{
ps->a[start] = ps->a[start + 1];
++start;
}
ps->size--;
}
int SeqListFind(SL* ps, SLDataType x)
{
assert(ps);
int i = 0;
while (i < ps->size)
{
if (ps->a[i] == x)
{
return i;
}
++i;
}
return -1;
}
**
## 2.Seqlist.h
```cpp
#pragma once
#include<stdio.h>
#include <stdlib.h>
#include <assert.h>
// 顺序表,有效数组在数组中必须是连续
// 静态顺序表设计 (固定大小)
//typedef int SLDataType;
//#define N 10
//
vector
//struct SeqList
//{
// SLDataType a[N];
// int size;
//};
//
//void SeqListPushBack(struct SeqList* ps, SLDataType x);
//void SeqListPopBack(struct SeqList* ps);
//void SeqListPushFront(struct SeqList* ps, SLDataType x);
//void SeqListPopFront(struct SeqList* ps);
// 动态顺序表设计 (大小可变)
typedef int SLDataType;
// vector
typedef struct SeqList
{
SLDataType* a;
int size; // 有效数据的个数
int capacity; // 容量
}SL, SeqList;
// 尾插尾删 头插头删
void SeqListInit(SL* ps);
void SeqListDestory(SL* ps);
void SeqListPrint(SL* ps);
void SeqListCheckCapacity(SL* ps);
void SeqListPushBack(SL* ps, SLDataType x);
void SeqListPopBack(SL* ps);
void SeqListPushFront(SL* ps, SLDataType x);
void SeqListPopFront(SL* ps);
// 任意位置的插入删除
void SeqListInsert(SL* ps, int pos, SLDataType x);
void SeqListErase(SL* ps, int pos);
// 顺序表查找
int SeqListFind(SL* psl, SLDataType x);
// ...
//int SeqListSort(SL* psl);
//int SeqListBinaryFind(SL* psl, SLDataType x);
## 3.测试文件
#include "SeqList.h"
// 测试头尾插入删除
void TestSeqList1()
{
SeqList s;
SeqListInit(&s);
SeqListPushBack(&s, 1);
SeqListPushBack(&s, 2);
SeqListPushBack(&s, 3);
SeqListPushBack(&s, 4);
SeqListPushBack(&s, 5);
SeqListPushBack(&s, 6);
SeqListPushBack(&s, 7);
SeqListPushBack(&s, 8);
//SeqListPushBack(&s, 9);
SeqListPrint(&s);
SeqListPopBack(&s);
SeqListPopBack(&s);
SeqListPrint(&s);
SeqListPushFront(&s, -1);
SeqListPushFront(&s, -2);
SeqListPushFront(&s, -3);
SeqListPrint(&s);
SeqListPopFront(&s);
SeqListPopFront(&s);
SeqListPrint(&s);
int pos = SeqListFind(&s, 5);
if (pos != -1)
{
SeqListErase(&s, pos);
}
SeqListPrint(&s);
SeqListDestory(&s);
}
int main()
{
TestSeqList1();
return 0;
}
*