1、Data.h
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDatetype;
typedef struct SeqList
{
SLDatetype* a;
int size;
int capacity;
}SL;
//动态顺序表的创建和销毁
void SLInit(SL* s1);
void SLDestory(SL* s1);
//扩容
void SLCheckCapacity(SL* s1);
//顺序表数据的增删查改插入删除
void SLPushBack(SL* s1, SLDatetype x);
void SLPushBFront(SL* s1, SLDatetype x);
void SLPopBack(SL* s1);
void SLPopFront(SL* s1);
void SLInsert(SL* s1, int pos, SLDatetype x);
void SLErase(SL* s1, int pos);
void SLModify(SL* s1, int pos, SLDatetype x);
int SLFind(SL* s1, SLDatetype x);
void print1(SL* s1);
2、main.cpp
#include"Data.h"
int main()
{
SL s;
s.a = NULL;
SLInit(&s);
SLPushBack(&s, 1);
SLPushBack(&s, 2);
SLPushBack(&s, 3);
SLPushBack(&s, 4);
SLPushBack(&s, 5);
SLPushBack(&s, 5);
SLPushBFront(&s, 234);
SLPushBack(&s, 5);
SLPushBack(&s, 5);
SLPushBack(&s, 5);
print1(&s);
SLPopFront(&s);
print1(&s);
SLPopBack(&s);
SLPopBack(&s);
SLPopBack(&s);
print1(&s);
SLPopBack(&s);
SLPopBack(&s);
SLPopBack(&s);
print1(&s);
SLPopBack(&s);
SLPopBack(&s);
SLPopBack(&s);
print1(&s);
SLPopBack(&s);
SLPopBack(&s);
SLPopBack(&s);
print1(&s);
SLPushBack(&s, 1);
SLPushBack(&s, 2);
SLPushBack(&s, 3);
SLPushBack(&s, 5);
print1(&s);
SLInsert(&s, 2, 456);
print1(&s);
SLInsert(&s, 2, 46);
print1(&s);
SLErase(&s, 4);
print1(&s);
printf("%d ",SLFind(&s, 4));
printf("%d \n",SLFind(&s, 2));
print1(&s);
SLModify(&s, 3, 21345);
print1(&s);
return 0;
}
3、Data.cpp
#include"Data.h"
void SLInit(SL* s1)
{
s1->a = (SLDatetype*)malloc(sizeof(SLDatetype) * 4);
if (s1->a == NULL)
{
perror("malloc fail");
}
s1->capacity = 4;
s1->size = 0;
}
void SLDestory(SL* s1)
{
free(s1->a);
s1->a = NULL;
s1->size = 0;
s1->capacity = 0;
}
void SLCheckCapacity(SL* s1)
{
if (s1->size == s1->capacity)
{
SLDatetype* temp = (SLDatetype*)realloc(s1->a, sizeof(SLDatetype) * s1->capacity * 2);
if (temp == NULL)
{
perror("realloc fail");
}
s1->a = temp;
s1->capacity *= 2;
}
}
void SLPushBack(SL* s1, SLDatetype x)
{
SLCheckCapacity(s1);
s1->a[s1->size++] = x;
}
void SLPushBFront(SL* s1, SLDatetype x)
{
SLInsert(s1, 0, x);
/*
SLCheckCapacity(s1);
int end = s1->size-1;
while (end >= 0)
{
s1->a[end + 1] = s1->a[end];
end--;
}
s1->a[0] = x;
s1->size++;
*/
}
void SLPopBack(SL* s1)
{
//暴力检查
//assert(s1->size > 0);
//温柔的检查
if(s1->size>0)
s1->size--;
}
void SLPopFront(SL* s1)
{
assert(s1->size > 0);
SLErase(s1, 0);
/*int start = 0;
while (start < s1->size - 1)
{
s1->a[start] = s1->a[start + 1];
start++;
}
s1->size--;*/
}
void SLInsert(SL* s1, int pos, SLDatetype x)
{
assert(pos >= 0 && pos <= s1->size);
SLCheckCapacity(s1);
int end = s1->size-1;
while (pos<=end)
{
s1->a[end + 1] = s1->a[end];
end--;
}
s1->a[pos] = x;
s1->size++;
}
void SLErase(SL* s1, int pos)
{
assert(pos >= 0 && pos < s1->size);
int start = pos + 1;
while (start < s1->size)
{
s1->a[start - 1] = s1->a[start];
start++;
}
s1->size--;
}
int SLFind(SL* s1, SLDatetype x)
{
for (int i = 0; i < s1->size; i++)
{
if (s1->a[i] == x)
{
return i;
}
}
return -1;
}
void SLModify(SL* s1, int pos, SLDatetype x)
{
assert(pos >= 0 && pos < s1->size);
s1->a[pos] = x;
}
void print1(SL* s1)
{
for (int i = 0; i < s1->size; i++)
{
printf("%d ", s1->a[i]);
}
printf("\n");
}