#pragma once
#include <stdio.h>
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;
size_t size;
size_t capacity;
}SeqList;
void SeqListInit(SeqList* ps);
void SeqListDestory(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);
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x);
void SeqListErase(SeqList* ps, size_t pos);
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <assert.h>
#include "SeqList.h"
#if 0
typedef int SLDateType;
typedef struct SeqList
{
SLDateType* a;
size_t size;
size_t capacity;
}SeqList;
#endif
void SeqListInit(SeqList* ps)
{
assert(NULL != ps);
ps->a = (SLDateType*)malloc(sizeof(SLDateType)* 10);
ps->capacity = 10;
ps->size = 0;
}
void SeqListDestory(SeqList* ps)
{
assert(NULL != ps);
free(ps->a);
ps->capacity = 0;
ps->size = 0;
}
void SeqListPrint(SeqList* ps)
{
assert(NULL != ps);
for (int i = 0; i < ps->size; i++)
{
printf("%d ", ps->a[i]);
}
printf("\n");
}
void CheckCacpity(SeqList* ps)
{
if (ps->capacity == ps->size)
{
ps->a = (SLDateType*)realloc(ps->a, sizeof(SLDateType)* ps->capacity * 2);
ps->capacity *= 2;
}
}
void SeqListPushBack(SeqList* ps, SLDateType x)
{
assert(NULL != ps);
CheckCacpity(ps);
ps->a[ps->size] = x;
ps->size++;
}
void SeqListPushFront(SeqList* ps, SLDateType x)
{
assert(NULL != ps);
CheckCacpity(ps);
memmove(ps->a + 1, ps->a, sizeof(SLDateType)* ps->size++);
ps->a[0] = x;
}
void SeqListPopFront(SeqList* ps)
{
assert(NULL != ps);
if (0 == ps->size)
{
return;
}
memmove(ps->a, ps->a + 1, sizeof(SLDateType)* (--ps->size));
}
void SeqListPopBack(SeqList* ps)
{
assert(NULL != ps);
if (0 == ps->size)
{
return;
}
ps->size--;
}
int SeqListFind(SeqList* ps, SLDateType x)
{
assert(NULL != ps);
for (int i = 0; i < ps->size; i++)
{
if (ps->a[i] == x)
{
return i;
}
}
return -1;
}
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x)
{
assert(NULL != ps);
CheckCacpity(ps);
memmove(ps->a + pos + 1, ps->a + pos, sizeof(SLDateType)*(ps->size - pos));
ps->a[pos] = x;
ps->size++;
}
void SeqListErase(SeqList* ps, size_t pos)
{
assert(NULL != ps);
CheckCacpity(ps);
memmove(ps->a + pos, ps->a + pos + 1, sizeof(SLDateType)*(ps->size - pos));
ps->size--;
}