//.h
#ifndef __SEQLISTD_H_
#define __SEQLISTD_H_
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<malloc.h>
#include<windows.h>
typedef int DataType;//定义数组类型
typedef unsigned int size_t;
typedef struct SeqListD
{
DataType* _array;//顺序表存储户(数据块指针)
size_t _capacity; // 底层空间的大小(容量)
size_t _size; // 当前有效元素的个数
}SeqListD, *PSeqListD;
void SeqListDInit(PSeqListD pSeq);//初始化
void SeqListDPushBack(PSeqListD pSeq, DataType data);//尾插
void SeqListDPopBack(PSeqListD pSeq);//尾删
int SeqListDEmpty(PSeqListD pSeq);//判断顺序表是否为空
int SeqListDSize(PSeqListD pSeq);//获取顺序表中元素的个数
void SeqListDClear(PSeqListD pSeq);// 清空顺序表中的所有元素
int SeqListDCapacity(PSeqListD pSeq);//获取顺序表的容量
void SeqListDDestroy(PSeqListD pSeq);// 销毁顺序表
int CheckCapacity(PSeqListD pSeq);// 检测顺序表是否需要增容
#endif
//.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqListD.h"
void SeqListDInit(PSeqListD pSeq)//初始化
{
assert(pSeq);
pSeq->_array = (DataType*)malloc(sizeof(DataType)* 3);
if (!pSeq->_array)//if(NULL==pSeq->_array)
{
printf("初始化顺序表失败\n");
exit(0);
}
pSeq->_capacity = 3;
pSeq->_size = 0;
}
void SeqListDPushBack(PSeqListD pSeq, DataType data)//尾插
{
assert(pSeq);
if (CheckCapacity(pSeq))
{
pSeq->_array[pSeq->_size] = data;
pSeq->_size++;
}
}
void SeqListDPopBack(PSeqListD pSeq)//尾删
{
assert(pSeq);
if (SeqListDEmpty(pSeq))
//if (pSeq->_size==0)
{
return;
}
pSeq->_size--;
}
int SeqListDEmpty(PSeqListD pSeq)//判断顺序表是否为空
{
assert(pSeq);
return 0 == pSeq->_size;
/*if(pSeq->_size==NULL)
return 0;
return 1;*/
}
int SeqListDSize(PSeqListD pSeq)//获取顺序表中元素的个数
{
assert(pSeq);
return pSeq->_size;
}
void SeqListDClear(PSeqListD pSeq)// 清空顺序表中的所有元素
{
assert(pSeq);
pSeq->_size = 0;
}
int SeqListDCapacity(PSeqListD pSeq)//获取顺序表的容量
{
assert(pSeq);
return pSeq->_capacity;
}
void SeqListDDestroy(PSeqListD pSeq)// 销毁顺序表
{
assert(pSeq);
if (pSeq->_array)
{
free(pSeq->_array);
pSeq->_array = NULL;
pSeq->_capacity = 0;
pSeq->_size = 0;
}
}
int CheckCapacity(PSeqListD pSeq)// 检测顺序表是否需要增容
{
if (pSeq->_size >= pSeq->_capacity)
{
int newCapacity = pSeq->_capacity * 2;
DataType* pTemp = (DataType*)malloc(sizeof(DataType)*pSeq->_capacity * 2);
if (pTemp == NULL)
{
printf("空间不足\n");
return 0;
}
memcpy(pTemp, pSeq->_array, sizeof(DataType)*pSeq->_size);
free(pSeq->_array);
pSeq->_array = pTemp;
pSeq->_capacity = newCapacity;
}
return 1;
}
//test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqListD.h"
void TestSeqListD()
{
SeqListD seq;
SeqListDInit(&seq);
SeqListDPushBack(&seq, 0);
SeqListDPushBack(&seq, 1);
SeqListDPushBack(&seq, 2);
printf("size = %d\n", SeqListDSize(&seq));
printf("capacity = %d\n", SeqListDCapacity(&seq));
SeqListDPushBack(&seq, 3);
SeqListDPushBack(&seq, 4);
SeqListDPushBack(&seq, 5);
printf("size = %d\n", SeqListDSize(&seq));
printf("capacity = %d\n", SeqListDCapacity(&seq));
SeqListDPopBack(&seq);
SeqListDPopBack(&seq);
SeqListDPopBack(&seq);
printf("size = %d\n", SeqListDSize(&seq));
printf("capacity = %d\n", SeqListDCapacity(&seq));
SeqListDClear(&seq);
printf("size = %d\n", SeqListDSize(&seq));
printf("capacity = %d\n", SeqListDCapacity(&seq));
SeqListDDestroy(&seq);
}
int main()
{
TestSeqListD();
system("pause");
return 0;
}