头文件
#pragma once
#include <assert.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int DataType;
struct SeqListD
{
DataType* _pData;
int _size;
int _capacity;
};
typedef struct SeqListD SeqListD;
typedef struct SeqListD* PSeqListD;
void SeqListDInit(struct SeqListD* pSeq);
void SeqListDPushBack(PSeqListD pSeq, DataType data);
void SeqListDPopBack(PSeqListD pSeq);
int SeqListDEmpty(PSeqListD pSeq);
int SeqListDSize(PSeqListD pSeq);
int SeqListDCapacity(PSeqListD pSeq);
void SeqListDClear(PSeqListD pSeq);
int CheckCapacity(PSeqListD pSeq);
void SeqListDDestroy(PSeqListD pSeq);
void Insert(PSeqListD pSeq, size_t pos, DataType data);
函数实现
#include"seqd.h"
void SeqListDInit(PSeqListD pSeq)
{
assert(pSeq);
pSeq->_pData = (DataType*)malloc(sizeof(DataType)*(pSeq->_capacity));
if (pSeq->_pData == NULL)
{
printf("开辟空间失败");
return;
}
pSeq->_size = 0;
pSeq->_capacity = 0;
}
void SeqListDPushBack(PSeqListD pSeq, DataType data)
{
assert(pSeq);
if (CheckCapacity(pSeq))
{
pSeq->_pData[pSeq->_size] = data;
pSeq->_size++;
}
return 0;
}
void SeqListDPopBack(PSeqListD pSeq)
{
aseert(pSeq);
if (SeqListDEmpty(pSeq))
{
printf("顺序表已空\n");
return;
}
pSeq->_size--;
}
int SeqListDEmpty(PSeqListD pSeq)
{
assert(pSeq);
if (pSeq->_size == 0)
{
return 1;
}
return 0;
}
int SeqListDSize(PSeqListD pSeq)
{
assert(pSeq);
return pSeq->_size;
}
int SeqListDCapacity(PSeqListD pSeq)
{
assert(pSeq);
return pSeq->_capacity;
}
void SeqListDClear(PSeqListD pSeq)
{
assert(pSeq);
pSeq->_size = 0;
}
int CheckCapacity(PSeqListD pSeq)
{
aseert(pSeq);
if (pSeq->_size >= pSeq->_capacity)
{
int newcapacity = pSeq->_capacity * 2;
DataType *ptemp= (DataType *)malloc(sizeof(DataType)*newcapacity);
if (pSeq->_pData == NULL)
{
printf("扩容失败\n");
return 0;
}
memcpy(ptemp, pSeq->_pData, sizeof(DataType)*newcapacity);
free(pSeq->_pData);
pSeq->_capacity = newcapacity;
pSeq->_pData = ptemp;
}
return 1;
}
void SeqListDDestroy(PSeqListD pSeq)
{
assert(pSeq);
pSeq->_capacity = 0;
pSeq->_size = 0;
free(pSeq->_pData);
return;
}
void Insert(PSeqListD pSeq, size_t pos, DataType data)
{
aseert(pSeq);
if (CheckCapacity(pSeq))
{
for (int i = pSeq->_size - 1; i > pos; i++)
{
pSeq->_pData[i + 1] = pSeq->_pData[i];
pSeq->_size++;
}
}
return 0;
}