缥缈烟雨

编程

动态顺序表的基本操作

SeqListD.h

#define _CRT_SECURE_NO_WARNING 1

#pragma once
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include <stdlib.h>

typedef int DataType;

typedef struct SeqListD
{
	DataType* array;
	int size;      //有效元素的个数
	int capacity;  //顺序表的容量
}SqlD,*PSqlD;


//动态顺序表的初始化
void SeqListDInit(PSqlD psd,int capacity);
void SeqListDPushBack(PSqlD psd,DataType data); //顺序表的尾插
void SeqListDPopBack(PSqlD psd);   //顺序表的尾删
//任意位置插入元素
void SeqListDInsert(PSqlD psd,int pos,DataType data);
//删除任意位置的元素
void SeqListDErase(PSqlD psd,int pos);

int SeqListDSize(PSqlD psd);//获取元素的个数

//获取顺序表的容量
int SeqListDCapacity(PSqlD psd);
int SeqListDEmpty(PSqlD psd);//判空

//将顺序表的元素清空 ,注意:不改变顺序表的空间大小
void SeqListDClear(PSqlD psd);
void SeqListDDestroy(PSqlD psd);

//对顺序表进行增容
int CheckCapacity(PSqlD psd);

//打印顺序表
void PrintSeqListD(PSqlD psd);

SeqListD.c

#define _CRT_SECURE_NO_WARNING 1

#include "SeqListD.h"



//动态顺序表的初始化
void SeqListDInit(PSqlD psd,int capacity)
{
	if(NULL == psd)  //判断顺序表是否存在
		return;
	//顺序表存在
	psd->array = (DataType*)malloc(capacity*sizeof(DataType));

    if(NULL == psd->array)
	{
		printf("申请空间失败!!!\n");
		return;
	}
	//申请成功
	psd->capacity = capacity;
	psd->size = 0;
}



//顺序表的尾插
void SeqListDPushBack(PSqlD psd,DataType data)
{
	if(NULL == psd)
		return;
	//如果顺序表存在
	if(psd->size == psd->capacity)//元素个数等于容量时,空间已满
		CheckCapacity(psd);//进行扩容
	psd->array[psd->size] = data;
	psd->size++;
}

//顺序表的尾删
void SeqListDPopBack(PSqlD psd)
{
	if(NULL == psd)
		return;
	if(0 == psd->size)
	{
		printf("顺序表已空无法删除!!!\n");
		return;
	}
	psd->size--;
}

//任意位置插入元素
void SeqListDInsert(PSqlD psd,int pos,DataType data)
{
	int i = 0;
	if(NULL == psd)
		return;
	if(psd->capacity == psd->size)
		CheckCapacity(psd);
	i = psd->size-1;
	for( ;i>pos-1;i--)
	{
		psd->array[i+1] = psd->array[i];
	}
	psd->array[pos] = data;
	psd->size++;
}

//删除任意位置的元素
void SeqListDErase(PSqlD psd,int pos)
{
	int i = 0;
	if(NULL == psd)
		return;
	if(0 == psd->size)
	{
		printf("顺序表已空无法删除!!!\n");
		return;
	}
	for(i=pos;i<psd->size;i++)
	{
		psd->array[i] = psd->array[i+1];
	}
	psd->size--;
}

//获取元素的个数
int SeqListDSize(PSqlD psd)
{
	if(NULL == psd)
		return 0;
	return psd->size;
}

//获取顺序表的容量
int SeqListDCapacity(PSqlD psd)
{
	if(NULL == psd)
		return 0;
	return psd->capacity;
}

//判空
int SeqListDEmpty(PSqlD psd)
{
	if(NULL == psd)
	{
		assert(0); // 断言顺序表是否存在
		return -1;
	}
	if(psd->size == 0)
		return 0;
	else
		return 1;
}

//将顺序表的元素清空 ,注意:不改变顺序表的空间大小
void SeqListDClear(PSqlD psd)
{
	if(NULL == psd)
		return;
	psd->size = 0;
	printf("顺序表已清空!!!\n");
}

//销毁顺序表
void SeqListDDestroy(PSqlD psd)
{
	if(NULL == psd)
		return;
	psd->size = 0;
	psd->capacity = 0;
	free(psd->array);
	psd->array = NULL;
	printf("顺序表已销毁!!!\n");
}

//顺序表扩容
int CheckCapacity(PSqlD psd)
{
	if(NULL == psd)
		return 0;
	if(psd->size == psd->capacity)
	{
		int newcapacity = psd->capacity*2;
		psd->array = (DataType*)realloc(psd->array,newcapacity*sizeof(DataType));
		if(NULL ==psd->array)
		{
			printf("申请空间失败!!!\n");
			return 0;
		}
		psd->capacity = newcapacity;
	}
	return 1;
}


//打印顺序表
void PrintSeqListD(PSqlD psd)
{
	int i = 0;
	if(NULL == psd)
		return;
	for( ; i<psd->size;i++)
	{
		printf("%d ",psd->array[i]);
	}
	printf("\n");
}


	

test.c

#define _CRT_SECURE_NO_WARNING 1

#include "SeqListD.h"


int main()
{
	SqlD s;
	SeqListDInit(&s,8);
	SeqListDPushBack(&s,1);
	SeqListDPushBack(&s,2);
	SeqListDPushBack(&s,3);
	SeqListDPushBack(&s,4);
	SeqListDPushBack(&s,5);
	SeqListDPushBack(&s,6);
	SeqListDPushBack(&s,7);
	SeqListDPushBack(&s,8);
	SeqListDPushBack(&s,9);
	PrintSeqListD(&s);

	//SeqListDInsert(&s,3,120);
	//PrintSeqListD(&s);

	/*SeqListDPopBack(&s);
	PrintSeqListD(&s);*/

	SeqListDInsert(&s,4,7);
	PrintSeqListD(&s);

	//SeqListDPopBack(&s);
	//PrintSeqListD(&s);

	SeqListDErase(&s,6);
	PrintSeqListD(&s);
	printf("此时顺序表的元素个数是:%d\n", SeqListDSize(&s));
	printf("此时顺序表的容量是:%d\n", SeqListDCapacity(&s));

	//SeqListDClear(&s);
	//PrintSeqListD(&s);

	SeqListDDestroy(&s);
	PrintSeqListD(&s);
	return 0;
}

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ijn842/article/details/79944963
上一篇顺序表的基本操作———C语言版
下一篇单链表的基本操作(C语言版)
博主设置当前文章不允许评论。

没有更多推荐了,返回首页

关闭
关闭