test.c
#include"seqlist.h"
int main()
{
SeqList* sl;
SeqListInit(&sl);
SeqListPushFront(&sl, 1);
SeqListPushFront(&sl, 2);
SeqListPushFront(&sl, 3);
SeqListPushBack(&sl, 4);
SeqListPopFront(&sl);
SeqListPopBack(&sl);
return 0;
}
#pragma once
// SeqList.h
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;
int size;
int capacity;
}SeqList;
// 对数据的管理:增删查改
void SeqListInit(SeqList* ps);
void SeqListDestroy(SeqList* ps);
void SeqListPrint(SeqList* ps);
void SeqListPushBack(SeqList* ps, SLDateType x);
void SeqListPushFront(SeqList* ps, SLDateType x);
void SeqListPopFront(SeqList* ps);
void SeqListPopBack(SeqList* ps);
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos);
#include"seqlist.h"
void SeqListInit(SeqList* ps)
{
assert(ps);
ps->a = malloc(sizeof(SeqList)*4);
if (ps->a == NULL)
{
perror("malloc fail");
}
ps->capacity = 4;
ps->size = 0;
}
void SeqListDestroy(SeqList* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->capacity = 0;
ps->size = 0;
}
void SeqListPrint(SeqList* ps)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void SLCheckCapacity(SeqList* ps)
{
assert(ps);
if (ps->size == ps->capacity)
{
SeqList* tmp = (SeqList*)realloc(ps->a, sizeof(SeqList) * ps->capacity * 2);
if (tmp == NULL)
{
perror("realloc fail");
return;
}
ps->a = tmp;
ps->capacity *= 2;
}
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(ps);
//psl->a[psl->size] = x;
//psl->size++;
//SLCheckCapacity(psl);
//psl->a[psl->size++] = x;
SeqListInsert(ps, ps->size, x);
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(ps);
//SLCheckCapacity(psl);
挪动数据
//int end = psl->size - 1;
//while (end >= 0)
//{
// psl->a[end + 1] = psl->a[end];
// --end;
//}
//psl->a[0] = x;
//psl->size++;
SeqListInsert(ps, 0, x);
}
void SeqListPopFront(SeqList* ps)
{
assert(ps);
// 暴力检查
//assert(psl->size > 0);
温柔的检查
if (psl->size == 0)
return;
psl->a[psl->size - 1] = 0;
//psl->size--;
SeqListErase(ps, ps->size - 1);
}
void SeqListPopBack(SeqList* ps)
{
assert(ps);
// 暴力检查
//assert(psl->size > 0);
///*int start = 0;
//while (start < psl->size-1)
//{
// psl->a[start] = psl->a[start + 1];
// start++;
//}*/
//int start = 1;
//while (start < psl->size)
//{
// psl->a[start-1] = psl->a[start];
// start++;
//}
//psl->size--;
SeqListErase(ps, 0);
}
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x)
{
assert(ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDateType x)
{
assert(ps);
//assert(0 <= pos <= psl->size);
assert(0 <= pos && pos <= ps->size);
SLCheckCapacity(ps);
int end = ps->size - 1;
while (end >= pos)
{
ps->a[end + 1] = ps->a[end];
--end;
}
ps->a[pos] = x;
ps->size++;
}
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos)
{
assert(ps);
assert(0 <= pos && pos < ps->size);
//assert(psl->size > 0);
int start = pos + 1;
while (start < ps->size)
{
ps->a[start - 1] = ps->a[start];
++start;
}
ps->size--;
}