头文件
#ifndef __TEST_H__
#define __TEST_H__
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<malloc.h>
#define MAX 3 //方便测试给一个小空间
typedef int datatype;
typedef struct Seqlist
{
datatype *_Array;
size_t _size;
size_t _capasize;
}Seqlist;
void init(Seqlist *pSeq)//初始化
{
pSeq->_Array = (datatype*)malloc(sizeof(datatype)*MAX);
memset(pSeq->_Array, 0,sizeof(datatype)*MAX);
pSeq->_size = 0;
pSeq->_capasize = MAX;
}
void capacity(Seqlist *pSeq)//空间不足开辟新空间
{
datatype *tmp;
tmp = (datatype*)malloc(sizeof(datatype)*(pSeq->_capasize)*2);
memcpy(tmp, (pSeq->_Array),sizeof(datatype)*(pSeq->_capasize));
free(pSeq->_Array);
pSeq->_Array = tmp;
pSeq->_capasize = (pSeq->_capasize) * 2;
}
void PrintfSqelist(Seqlist *pSeq)//打印数据
{
assert(pSeq);
int i;
for (i = 0; i<(int)(pSeq->_size); i++)
{
printf("%d", (pSeq->_Array)[i]);
}
printf("\n");
}
void pushback(Seqlist *pSeq,datatype x)//添加尾部数据
{
assert(pSeq);
if (pSeq->_size == pSeq->_capasize)
{
capacity(pSeq);
}
(pSeq->_Array)[pSeq->_size] = x;
pSeq->_size++;
}
void pushfront(Seqlist *pSeq, datatype x)//添加头部数据
{
assert(pSeq);
if (pSeq->_size == pSeq->_capasize)
{
capacity(pSeq);
}
int i;
for (i = (int)(pSeq->_size - 1); i >= 0; i--)
{
(pSeq->_Array)[i + 1] = (pSeq->_Array)[i];
}
(pSeq->_Array)[0] = x;
pSeq->_size++;
}
void popback(Seqlist *pSeq) //删除尾部数据
{
assert(pSeq);
pSeq->_size--;
}
void popfront(Seqlist *pSeq) //删除头部数据
{
assert(pSeq);
int i;
for (i = 0; i<(int)(pSeq->_size - 1); i++)
{
pSeq->_Array[i] = pSeq->_Array[i + 1];
}
pSeq->_size--;
}
int findsign(Seqlist *pSeq, int x) //寻找一个数据找到返回1,否则-1
{
assert(pSeq);
int i;
for (i = 0; i <= (int)(pSeq->_size - 1); i++)
{
if (pSeq->_Array[i] == x)
{
return 1;
}
}
return -1;
}
void erase(Seqlist *pSeq, size_t num) //消除一个位置上面的数据
{
assert(pSeq);
size_t i;
for (i = num-1; i <= (int)(pSeq->_size - 1); i++)
{
pSeq->_Array[i] = pSeq->_Array[i + 1];
}
pSeq->_size--;
}
void removesign(Seqlist *pSeq, datatype x) //消除第一个找到的x
{
assert(pSeq);
int i, k;
for (i = 0; i <=(int)(pSeq->_size - 1); i++)
{
if (pSeq->_Array[i] == x)
{
for (k = i; k < (int)(pSeq->_size - 1); k++)
{
pSeq->_Array[k] = pSeq->_Array[k + 1];
}
pSeq->_size--;
return;
}
}
printf("没有此数据");
}
void removeallsign(Seqlist *pSeq, datatype x) //消除所有找到的x
{
assert(pSeq);
size_t start=0;
size_t sign=0;
int count=0;
int i;
for (i = 0; i < (int)pSeq->_size; i++)
{
if (pSeq->_Array[i] != x)
{
pSeq->_Array[start] = pSeq->_Array[sign];
sign++;
start++;
}
else
{
count++;
sign++;
}
}
pSeq->_size -= count;
}
void Modify(Seqlist *pSeq, size_t num, datatype x) //修改某一个位置上的数据
{
assert(pSeq);
pSeq->_Array[num-1] = x;
}
#endif
主函数;
#include"test.h"
int main()
{
Seqlist pSeq;
init(&pSeq); //初始化
pushback(&pSeq, 1);//尾部添加1
pushback(&pSeq, 2);//尾部添加2
pushback(&pSeq, 3);//尾部添加3
pushback(&pSeq, 3);//尾部添加3
pushback(&pSeq, 4);//尾部添加4
pushfront(&pSeq, 0);//头部添加0
Modify(&pSeq, 3,2); //修改第三个数据为2
removesign(&pSeq, 3);//删除第一个找到的数据3
removeallsign(&pSeq, 2);//删除所有2
popback(&pSeq); //尾删除
popfront(&pSeq); //头删除
PrintfSqelist(&pSeq);//打印
return 0;
}
转载于:https://blog.51cto.com/wpfbcr/1712276