今天,话不多说,直接上代码
//这里对函数进行测试
#include"Seqlist.h"
#include<assert.h>
#include<stdlib.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);
SeqlistPrint(&s);
SeqlistPopBack(&s);
SeqlistPopBack(&s);
SeqlistPopBack(&s);
SeqlistPrint(&s);
SeqlistPushFront(&s, -1);
SeqlistPrint(&s);
SeqlistPopFront(&s);
SeqlistPopFront(&s);
SeqlistPrint(&s);
SeqlistDestory(&s);
int pos = SeqlistFind(&s, 5);
if (pos != -1)
{
SeqlistErase(&s, pos);
}
SeqlistPrint(&s);
SeqlistDestory(&s);
}
int main()
{
TestSeqlist1();
return 0;
}
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
//这个文件中写的是函数的申明
//顺序表其实就是数组,且有效数组在数组中必须是连续的
//动态顺序表实现
typedef int SlDATAType;
typedef struct Seqlist
{
SlDATAType* p;
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 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);
#include"Seqlist.h"//这里写函数的定义
typedef int SlDATAType;
void SeqlistInit(SL* ps)
{
ps->p = (SlDATAType*)malloc(sizeof(SlDATAType) * 4);
if (ps->p == NULL)
{
printf("申请内存失败\n");
exit(-1);
}
ps->size= 0;
ps->capacity = 4;
}
void SeqlistDestory(SL* ps)
{
free(ps->p);
ps->p = NULL;
ps->size = ps->capacity = 0;
}
void SeqlistPrint(SL* ps)
{
assert(ps);
for (int i = 0; i < ps->size; ++i)
{
printf("%d", ps->p[i]);
}
printf("\n");
}
void SeqlistCheckCapacity(SL* ps)
{
//如果满了需要增容
if (ps->size == ps->capacity)
{
ps->capacity *= 2;
ps->p = (SlDATAType*)realloc(ps->p, sizeof(SlDATAType) * ps->capacity);
if (ps->p == NULL)
{
printf("扩容失败");
exit(-1);
}
}
}
void SeqlistPushBack( SL* ps, SlDATAType x)
{
assert(ps);
SeqlistCheckCapacity(ps);
ps->p[ps->size] = x;
ps->size++;
}
void SeqlistPopBack(SL* ps)
{
assert(ps);
//ps->p[ps->size - 1] = 0;
ps->size--;
}
void SeqlistPushFront(SL* ps, SlDATAType x)
{
assert(ps);
SeqlistCheckCapacity(ps);
int end = ps->size - 1;
while(end >= 0)
{
ps->p[end + 1] = ps->p[end];
--end;
}
ps->p[0] = x;
ps->size++;
}
void SeqlistPopFront(SL* ps)
{
assert(ps);
int start = 0;
while (start < ps->size - 1)
{
ps->p[start] = ps->p[start + 1];
++start;
}
ps->size--;
}
//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->p[end + 1] = ps->p[end];
--end;
}
ps->p[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->p[start] = ps->p[start + 1];
++start;
}
ps->size--;
}
//顺序表中的查找
int SeqlistFind(SL* ps, SlDATAType x)
{
assert(ps);
int i = 0;
while (i < ps->size)
{
if (ps->p[i] == x)
{
return i;
}
++i;
}
return -1;
}
今天就分享到这里,我们下期再见啦