线性表:(1)顺序表【底层是连续的空间,所以进行元素的插入和删除时,必须搬移大量的元素】:a、静态顺序表(底层空间受到限制)b、动态顺序表(底层空间不受限制)
(2)链表:任意位置的插入不需要搬移元素,不是连续的空间。
a、分类:单链表(一个节点只有一个指针,指向后一个节点)带头节点的单链表和不带头节点的单链表;双链表(每一个节点有两个指针,既保存后一个节点的位置,也保存了前一个节点的位置);
顺序表中尾插的效率高,头插还得整个元素进行搬移。
PseqlistD.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);
void PrintSeqlistD(PSeqListD pSeq);
PseqlistD.c
# define _CRT_SECURE_NO_WARNINGS 1
# include"PseqlistD.h"
# include
# include
# include
# include
void SeqListDInit(PSeqListD pSeq)
{
if (NULL == pSeq)
return;
pSeq->_array = (DataType*)malloc(3 * sizeof(DataType));
if (NULL == pSeq->_array)
return;
pSeq->_capacity = 3;
pSeq->_size = 0;
}
int CheckCapacity(PSeqListD pSeq)
{
if (NULL == pSeq)
return 0;
if ((pSeq->_size) >= pSeq->_capacity)
{
size_t newCapacity = 2 * pSeq->_capacity;
//增容:新的空间为增加50%;每一次都将容量增加2倍。STL库平台不一样,增容的
DataType* pTmp = (DataType*)malloc(newCapacity * sizeof(DataType));
if (NULL == pTmp)
return 0;
//将原空间元素移动到新空间
memcpy(pTmp, pSeq->_capacity, pSeq->_size*sizeof(DataType));
free(pSeq->_array);
pSeq->_array = pTmp;
pSeq->_capacity = newCapacity;
}
return 1;
}
void SeqListDPushBack(PSeqListD pSeq, DataType data)
{
if (NULL == pSeq)
return;
if (!CheckCapacity(pSeq))
return;
pSeq->_array[pSeq->_size++] = data;
}
void SeqListDPopBack(PSeqListD pSeq)
{
if (NULL == pSeq)
return;
if (pSeq->_size == 0)
{
printf("为空,不可删\n");
return;
}
pSeq->_size--;
}
int SeqListDEmpty(PSeqListD pSeq)
{
return 0 == pSeq->_size;
}
int SeqListDSize(PSeqListD pSeq)
{
return pSeq->_size;
}
void SeqListDClear(PSeqListD pSeq)
{
pSeq->_size = 0;
}
int SeqListDCapacity(PSeqListD pSeq)
{
return pSeq->_capacity;
}
void SeqListDDestroy(PSeqListD pSeq)
{
if (pSeq->_array)
{
free(pSeq->_array);
pSeq->_size = 0;
pSeq->_capacity = 0;
}
}
void PrintSeqlistD(PSeqListD pSeq)
{
int i = 0;
for (; i < pSeq->_size; ++i)
{
printf("%d", pSeq->_array[i]);
}
printf("\n");
}
test.c
# define _CRT_SECURE_NO_WARNINGS 1
# include"PseqlistD.h"
void testSeqlistD1()
{
SeqListD s;
SeqListDInit(&s);
SeqListDPushBack(&s, 1);
SeqListDPushBack(&s, 3);
SeqListDPushBack(&s, 4);
PrintSeqlistD(&s);
}
void testSeqlistD2()
{
SeqListD s;
SeqListDInit(&s);
SeqListDPushBack(&s, 1);
SeqListDPushBack(&s, 3);
SeqListDPushBack(&s, 4);
printf("size=%d\n", SeqListDSize(&s));
printf("capacit=%d", SeqListDCapacity(&s));
}
void testSeqlistD3()
{
SeqListD s;
SeqListDInit(&s);
SeqListDPushBack(&s, 1);
SeqListDPushBack(&s, 3);
SeqListDPushBack(&s, 4);
PrintSeqlistD(&s);
SeqListDClear(&s);
printf("%d", SeqListDSize(&s));
printf("%d", SeqListDCapacity(&s));
}
void testSeqlistD4()
{
SeqListD s;
SeqListDInit(&s);
SeqListDPushBack(&s, 1);
SeqListDPushBack(&s, 3);
SeqListDPushBack(&s, 4);
PrintSeqlistD(&s);
SeqListDDestroy(&s);
printf("%d", SeqListDSize(&s));
printf("%d", SeqListDCapacity(&s));
}
int main()
{
//testSeqlistD1();
//testSeqlistD2();
//testSeqlistD3();
testSeqlistD4();
system("pause");
return 0;
}
结果:test1:
test2:
test3:
test4: