不定长的顺序表代码来了

(1)首先是dsqlist.h文件了

#pragma once
typedef struct DSQlist {

	int length;//有效数据长度  //和定长的顺序表不同 定义有效数据长度是为了方便对此顺序表进行判满操作
	int* elem;//存放数据
	int listsize;//数组长度
}DSQlist, * DPSQlist;//分号别忘了


//功能函数

//初始化
void InitSqList(DPSQlist ps);

//对ps进行判满
static bool IsFull(DPSQlist ps);

//realloc扩容
static bool Inc(DPSQlist ps);

//插入数据,在ps顺序表的pos位置插入val;
bool Insert(DPSQlist, int pos, int val);

//判空
bool IsEmpty(DPSQlist ps);

//在ps中查找第一个key值,找到了返回下标,没有找到返回-1;
int Search(DPSQlist ps, int key);

//删除pos位置的值
bool DelPos(DPSQlist ps, int pos);

//删除第一个val的值
bool DelVal(DPSQlist ps, int val);

//返回key的前驱下标,如果不存在返回-1;
int GetPrio(DPSQlist ps, int key);

//返回key的后继下标,如果不存在返回-1;
int GetNext(DPSQlist ps, int key);

//输出
void  Show(DPSQlist ps);


//清空数据
void Clear(DPSQlist ps);


//销毁
void Destroy(DPSQlist ps);


(2)dsqlist.cpp实现函数功能的文件

#include"dsqlist.h"
#include<assert.h>
#include <stdio.h>
#include<stdlib.h>
#define SIZE 10//自定义就好了  #define 在这里定义数据可以使用在整个代码中 注意没有分号  且名称一般是大写

//大家可以发现每一个函数的实现前面都有一个assert函数  这是为了保障我们函数的健壮性 如果出现ps==NULL的情况 程序运行后会提醒 而不是直接崩掉 
//初始化
void InitSqList(DPSQlist ps)//从赋初值开始
{
    assert(ps != NULL);
    if (ps == NULL)
    {
        return;
    }
    ps->elem = (int*)malloc(SIZE * sizeof(int));//malloc函数是一个申请内存空间的函数 且返回值是void*  所以在这里还用到了类型的强转
    ps->length = 0;//刚开始没有数据在顺序表里
    ps->listsize = SIZE;//附上自定义大小的顺序表长度
}

//对ps进行判满
static bool IsFull(DPSQlist ps)
{
    assert(ps != NULL);
    if (ps == NULL)
    {
        return false;
    }
    return ps->length == ps->listsize;
}

//realloc扩容
static bool Inc(DPSQlist ps)
{
    assert(ps != NULL);
    if (ps == NULL)
    {
        return false;
    }
    if (IsFull(ps))
    {
        ps->elem = (int*)realloc(ps->elem, ps->listsize * 2 * (sizeof(int)));//realloc 函数是一个扩容函数 函数里第一个参数是要扩容的数组或者这里的顺序表 第二个参数是要扩容的大小 要注意;1.是要有需要申请的数据类型 比如int  2.返回值问题 原函数的返回值是void* 一般要强转为咱们需要的数据类型  还有一个calloc函数  不是很常用 了解即可
    }
    ps->listsize *= 2;//扩容扩大了两倍 容量更新
    return true;
}

//插入数据,在ps顺序表的pos位置插入val;
bool Insert(DPSQlist ps, int pos, int val)
{
    assert(ps != NULL);
    if (ps == NULL)
    {
        return false;
    }
    if (IsFull(ps))//若满了要扩容
    {
        Inc(ps);
    }
    //1.找到pos位置 数据后移
    for (int i = ps->length-1; i >= pos; i--)
    {
            ps->elem[i + 1] = ps->elem[i];
       
    }
    //2.插入数据
    ps->elem[pos] = val;
    //3.有效数据++
    ps->length++;
    return true;
}

//判空
bool IsEmpty(DPSQlist ps)
{
    assert(ps != NULL);
    if (ps == NULL)
    {
        return false;
    }
    return ps->length == 0;
}

//在ps中查找第一个key值,找到了返回下标,没有找到返回-1;
int Search(DPSQlist ps, int key)
{
    assert(ps != NULL);
    if (ps == NULL)
    {
        return -1;
    }
    for (int i = 0; i < ps->length; i++)
    {
        if (ps->elem[i] == key)
        {
            return i;
        }
    }
    return -1;
}

//删除pos位置的值  按位置删
bool DelPos(DPSQlist ps, int pos)
{
    assert(ps != NULL);
    if (ps == NULL)
    {
        return false;
    }
    if (pos<0 || pos>=ps->length)
    {
        return false;
    }
    //1.找到pos的下边  后面的数据前移  覆盖掉要删除的值
    for (int i = Search(ps, pos); i < ps->length-1; i++)
    {
        ps->elem[i] = ps->elem[i+1];
    }
    //2.有效数据减减
    ps->length--;
    return true;
}

//删除第一个val的值  按值删
bool DelVal(DPSQlist ps, int val)
{
    assert(ps != NULL);
    if (ps == NULL)
    {
        return false;
    }
    int i=Search(ps, val);
    DelPos(ps, i);
    return true;
}

//返回key的前驱下标,如果不存在返回-1;
int GetPrio(DPSQlist ps, int key)
{
    assert(ps != NULL);
    if (ps == NULL)
    {
        return false;
    }
    int i = Search(ps, key);
    if (i <= 0)//头节点没有前驱
    {
        return -1;
    }
    else
        return i - 1;
}

//返回key的后继下标,如果不存在返回-1;
int GetNext(DPSQlist ps, int key)
{
    assert(ps != NULL);
    if (ps == NULL)
    {
        return false;
    }
    int i = Search(ps, key);
    if (i < 0||i==ps->length-1)//尾巴没有后继
    {
        return -1;
    }
    else
        return i + 1;
}

//输出
void  Show(DPSQlist ps)
{
    assert(ps != NULL);
    if (ps == NULL)
    {
        return;
    }
    for (int i = 0; i < ps->length; i++)
    {
        printf("%d  ", ps->elem[i]);
    }
    printf("\n");
}


//清空数据
void Clear(DPSQlist ps)
{
    assert(ps != NULL);
    if (ps == NULL)
    {
        return;
    }
    ps->length = 0;
}


//销毁
void Destroy(DPSQlist ps)
{
    assert(ps != NULL);
    if (ps == NULL)
    {
        return;
    }
    free(ps->elem);//有malloc就必须有free 不然会发生内存泄露
    ps->length = 0;
    ps->elem == NULL;
    ps->listsize = 0;
}

(3)test.cpp测试文件

#include"dpsqlist.h"
#include<assert.h>
#include <stdio.h>
#include<stdlib.h>

int main()
{
	DSQlist head;
	InitSqList(&head);
	for (int i = 0; i < 10; i++)
	{
		Insert(&head, i, i);
	}
	//Show(&head);
	/*int i = Search(&head, 2);
	printf("%d\n", i);*/


	///*DelPos(&head, 9);
	//Show(&head);*/


	/*DelVal(&head, 4);
	Show(&head);*/


	/*int i = GetPrio(&head, 6);
	printf("%d\n", i);*/

	///*int i = GetNext(&head, 2);
	//printf("%d\n", i);*/

	/*Clear(&head);
	Show(&head);*/

	Destroy(&head);
	Show(&head);
	
	return 0;
}

(4)附上测试的照片

 

 

 

 

 

 

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我在凌晨等太阳¤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值