**
1.定义一个动态顺序表
由于定长开辟顺序表的大小依赖于存储空间的初始分配量 SIZE,要想解决此问题,需要在顺序表存储空间不足时进行动态扩容
**
#define INIT_SIZE 10 // 存储空间的初始分配量
typedef struct Dsqlist
{
int *elem; // 数组存储数据元素,需在初始化时动态开辟内存
int usedsize; // 顺序表的当前长度
int size; // 顺序表存储空间的大小
}Dsqlist,*PDSqlist;
:
2.动态顺序表的各种操作
void InitList(PDSqlist psq);
bool Insert(PDSqlist psq,int pos,int val);
int Search(PDSqlist Psq,int pos,int key);//查找 key 值
bool DeletePos(PDSqlist Psq,int pos,int *rtv);//删除 pos 位置的值
bool Delete(PDSqlist Psq,int pos,int key);//删除一个 key 值
void Clear(PDSqlist psq);
void Destroy(PDSqlist psq);
void Show(PDSqlist psq)
①初始化
顺序表的初始化就是把顺序表初始化为空的顺序表,需为数组elem动态开辟存储空间,并把顺序表的长度usedsize置为0,size置为初始分配量INIT_SIZ即可
void InitList(PDSqlist psq)
{
assert(psq != NULL);
psq->elem = (int *)malloc(sizeof(int) * INIT_SIZE);
assert(psq->elem != NULL);//判断是否开辟成功
psq->usedsize = 0;
psq->size = INIT_SIZE;
}
②判断顺序表是否为满
static bool isFull(PDSqlist psq)
{
if (psq->usedsize == psq->size)
{
return true;
}
return false;
}
③为顺序表动态扩容
当顺序表为满时,需要调用动态扩容函数对顺序表的elem数组进行动态扩容,这里采用二倍扩容的方式
static void Inc(PDSqlist psq)
{
int *p = (int *)realloc(psq->elem, psq->size*sizeof(int) * 2);
if (p != NULL)
{
psq->elem = p;
}
psq->size *= 2;
}
4、在 pos 位置插入 val 值
与之前的 Insert()函数 不同的是,当顺序表为满时,需要调用动态扩容函数对顺序表的elem数组进行动态扩容
bool Insert(PDSqlist psq, int pos, int val)
{
if (isFull(psq))
{
Inc(psq);
}
for (int i = psq->usedsize - 1; i >= pos; i--)
{
psq->elem[i + 1] = psq->elem[i];
}
psq->elem[pos] = val;
psq->usedsize++;
return true;
}
5、释放动态开辟的存储空间
由于elem数组是动态开辟的,因此具有动态扩容顺序表与普通顺序表不同的是需要专门设立一个函数对动态开辟的存储空间进行释放,否则会在程序运行过程中产生内存泄露。
void Destroy(PDSqlist psq)
{
free(psq->elem);
psq->elem = NULL;
psq->usedsize = 0;
psq->size = 0;
}
以下操作和定长开辟顺序表完全一致
static bool isEmpty(PDSqlist Psq)//判断是否为空
{
return Psq->usedsize == 0;
}
int Search(PDSqlist Psq,int pos,int key)//查找 key 值
{
if(pos < 0 || pos >= Psq->usedsize || isEmpty(Psq))
{
return -1;
}
for(int i = pos;i < Psq->usedsize;i++)
{
if(Psq->elem[i] == key)
{
return i;
}
}
return -1;
}
bool DeletePos(PDSqlist Psq,int pos,int *rtv)//删除 pos 位置的值
{
if(pos < 0 || pos >= Psq->usedsize || isEmpty(Psq))
{
return false;
}
if(rtv != NULL)
{
*rtv = Psq->elem[pos];
}
for(int i = pos;i < Psq->usedsize-1;i++)
{
Psq->elem[i] = Psq->elem[i+1];
}
Psq->usedsize--;
return true;
}
bool Delete(PDSqlist Psq,int pos,int key)//删除一个 key 值
{
int index = Search(Psq,pos,key);
if(index == -1)
{
return false;
}
return DeletePos(Psq,index,NULL);
}
void Clear(PDSqlist psq)//清空
{
psq->usedsize = 0;
//psq->size = 0;
}
void Show(PDSqlist psq)
{
for (int i = 0; i < psq->usedsize; i++)
{
printf("%d ",psq->elem[i]);
}
printf("\n");
}
dsqlist.h:
#pragma once
#define INIT_SIZE 10
typedef struct Dsqlist
{
int *elem;
int usedsize;
int size;
}Dsqlist,*PDSqlist;
void InitList(PDSqlist Psq);
bool Insert(PDSqlist Psq,int pos,int val);
int Search(PDSqlist Psq,int pos,int key);
bool DeletePos(PDSqlist Psq,int pos,int *rtv);
bool Delete(PDSqlist Psq,int pos,int key);
bool GetElem(PDSqlist Psq,int pos,int *rtv);
int GetLength(PDSqlist Psq);
void Clear(PDSqlist Psq);
void Destroy(PDSqlist Psq);
void Show(PDSqlist Psq);
dsqlist.cpp:
#include "dsqlist.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
void InitList(PDSqlist Psq)
{
assert(Psq != NULL);
Psq->elem = (int *)malloc(INIT_SIZE * sizeof(int));
assert(Psq->elem != NULL);
Psq->usedsize = 0;
Psq->size = INIT_SIZE;
}
static void Inc(PDSqlist Psq)
{
int *p = (int *)realloc(Psq->elem,Psq->size *2 *sizeof(int));
assert(p != NULL);
if (p != NULL)
{
Psq->elem = p;
}
assert(Psq->elem != NULL);
Psq->size *= 2;
}
static bool IsFull(PDSqlist Psq)
{
return Psq->usedsize == Psq->size;
}
bool Insert(PDSqlist Psq,int pos,int val)
{
assert(Psq != NULL);
if (pos < 0 || pos > Psq->usedsize)
{
return false;
}
if (IsFull(Psq))
{
Inc(Psq);
}
for (int i = Psq->usedsize-1; i >= pos; i--)
{
Psq->elem[i+1] = Psq->elem[i];
}
Psq->elem[pos] = val;
Psq->usedsize++;
return true;
}
static bool IsEmpty(PDSqlist Psq)
{
return Psq->usedsize == 0;
}
int Search(PDSqlist Psq,int pos,int key)
{
assert(Psq != NULL);
if (pos < 0 || pos > Psq->usedsize || IsEmpty(Psq))
{
return -1;
}
for (int i = pos; i < Psq->usedsize; i++)
{
if (Psq->elem[i] == key)
{
return i;
}
}
return -1;
}
bool DeletePos(PDSqlist Psq,int pos,int *rtv)
{
assert(Psq != NULL);
if (pos < 0 || pos >= Psq->usedsize || IsEmpty(Psq))
{
return false;
}
if (rtv != NULL)
{
*rtv = Psq->elem[pos];
}
for (int i = pos; i < Psq->usedsize; i++)
{
Psq->elem[i] = Psq->elem[i+1];
}
Psq->usedsize--;
return true;
}
bool Delete(PDSqlist Psq,int pos,int key)
{
assert(Psq != NULL);
int index = Search(Psq,pos,key);
if (index == -1)
{
return false;
}
return DeletePos(Psq,index,NULL);
}
bool GetElem(PDSqlist Psq,int pos,int *rtv)
{
assert(Psq != NULL);
*rtv = Psq->elem[pos];
return true;
}
int GetLength(PDSqlist Psq)
{
assert(Psq !=NULL);
return Psq->usedsize;
}
void Clear(PDSqlist Psq)
{
assert(Psq != NULL);
Psq->usedsize = 0;
}
void Destroy(PDSqlist Psq)
{
assert(Psq != NULL);
free(Psq->elem);
Psq->elem = NULL;
Psq->size = 0;
Psq->usedsize = 0;
}
void Show(PDSqlist Psq)
{
assert(Psq !=NULL);
for (int i = 0; i < Psq->usedsize; i++)
{
printf("%d ",Psq->elem[i]);
}
printf("\n");
}
test.cpp
#include<stdio.h>
#include<vld.h>
#include "dsqlist.h"
int main()
{
Dsqlist ds;
InitList(&ds);
for (int i = 0; i < 70; i++)
{
Insert(&ds,i,i);
}
Show(&ds);
Delete(&ds,0,2);
Delete(&ds,0,9);
Delete(&ds,0,0);
Show(&ds);
Destroy(&ds);
return 0;
}