顺序表的结构以及一些函数的实现

1.结构--以及各部分的成员的说明.

typedef int ElemType;
typedef struct SqList
{
    ElemType* data; // 指向用于存储ElemType类型的堆区空间的指针
    int length; // 长度: 以存储的属于元素的个数
    int size; // 空间的大小,记录的是能够存储的元素个数
}SqList;

2.一些默认数值的设定,以及需要实现的部分函数的声明

typedef bool(*Compare)(ElemType, ElemType);//函数指针.

#define DEFAULTSIZE 10 //默认大小
// 初始化
void InitSqList(SqList* sq_list, int init_size);
// 销毁
void DestroySqList(SqList* sq_list);
// 插入
 /*
 1、头插法
 2、尾插法
 3、按位置插入
 */
bool InsertSqListHead(SqList* sq_list, ElemType value);
bool InsertSqListRear(SqList* sq_list, ElemType value);
bool InsertSqListPos(SqList* sq_list, ElemType value, int pos);
// 删除
//要求:时间复杂度O(n),空间复杂度O(1)
/*
 1、头删法
 2、尾删法
 3、按位置删
 4、按值删除
 */
bool DeleteSqListHead(SqList* sq_list);
bool DeleteSqListRear(SqList* sq_list);
bool DeleteSqListPos(SqList* sq_list, int pos);
bool DeleteSqListValue(SqList* sq_list, ElemType value);
// 查找 : 返回位置(下标):取值范围 0 -- length-1
int FindSqList(SqList* sq_list, ElemType value,Compare fun);
// 判空
bool Empty(SqList* sq_list);
// 判满
bool Full(SqList* sq_list);
//打印(便于观察)
void Show(SqList* sq_list);

3.具体实现

#include<stdlib.h>//用到的头文件
#include<stdio.h>
#include<assert.h>

static bool AppendSpace(SqList* sq_list)
{
    int new_size = (sq_list->size * 2);
    ElemType* new_space = (ElemType*)malloc(sizeof(ElemType) * new_size);
    if (nullptr == new_space)
    {
        return false;
    }
    for (int i = 0; i < sq_list->size; i++)
    {
        new_space[i] = sq_list->data[i];
    }

    free(sq_list->data);

    sq_list->data = new_space;
    sq_list->size = new_size;
    return true;
    
}

//初始化
void InitSqList(SqList* sq_list, int init_size)
{
    if (nullptr == sq_list)
    {
        exit(0);//exit(EXIT_FAILURE) == exit(1)  exit( EXIT_SUCCESS) == exit(0)
    }

    sq_list->data = (ElemType*)malloc(sizeof(SqList) * init_size);
    if (nullptr == sq_list->data)
    {
        exit(0);
    }
    sq_list->length = 0;
    init_size = (init_size > 0) ? init_size : DEFAULTSIZE;
}

void DestroySqList(SqList* sq_list)
{
    if (nullptr == sq_list)
    {
        exit(0);
    }

    free(sq_list->data);
    sq_list->data = nullptr;
    sq_list->size = sq_list->length = 0;
}
//插入
bool InsertSqListHead(SqList* sq_list, ElemType value)
{
    return InsertSqListPos(sq_list, value, 0);
}

bool InsertSqListRear(SqList* sq_list, ElemType value)
{
    if (nullptr == sq_list) exit(0);

    return InsertSqListPos(sq_list, value, sq_list->length);
}


bool InsertSqListPos(SqList* sq_list, ElemType value, int pos)
{
    if (sq_list->data == nullptr) exit(0);

    if (pos < 0 || pos > sq_list->length)
    {
        printf("Insert Fail: Pos is error");
        return false;
    }

    if (Full(sq_list))
    {
        if (!AppendSpace(sq_list))
        {
            printf("Insert Fail:Append Space Fail\n");
            return false;
        }
    }

    // 挪动pos位置及其之后的数据,统一向后挪动一个位置: 从length - 1向前挪动
    for (int i = sq_list->length - 1; i >= pos; i--)
    {
        sq_list->data[i + 1] = sq_list->data[i];
    }
    sq_list->data[pos] = value;
    sq_list->length++;
    return true;
}


//删除
bool DeleteSqListHead(SqList* sq_list)//删头
{
    return DeleteSqListPos(sq_list, 0);
}

bool DeleteSqListRear(SqList* sq_list)//删尾
{
    if (sq_list->length == 0) exit(0);

    return DeleteSqListPos(sq_list, sq_list->length - 1);
}

// 删除顺序表中所有的value值: 时间复杂度为O(n), 空间复杂度为O(1)
bool DeleteSqListValue(SqList* sq_list, ElemType value)//删值
{
    if (sq_list == nullptr) exit(0);
    int i = 0; 
    int j = 0;
    for (; j < sq_list->length; j++)
    {
        if (sq_list->data[j] != value)
        {
            sq_list->data[i] = sq_list->data[j];
            i++;    
        }
    }
    sq_list->length = i;
    return true;
}


bool DeleteSqListPos(SqList* sq_list, int pos)//删点
{
    if (nullptr == sq_list) exit(0);

    if(pos < 0 || pos > sq_list->length - 1)//要判断pos值是否合法
    {
        printf("Delete Fail: Pos is error \n");
        return false;
    }

    if (Empty(sq_list))//删除时要判断是否为空
    {
        printf("Delete Fail: Sqlist is Empty \n");
        return false;
    }

    for (int i = pos; i < sq_list->length - 1; i++)//挪值
    {
        sq_list->data[i] = sq_list->data[i + 1];
    }

    sq_list->length--;

    return true;
}

//查找
int FindSqList(SqList* sq_list, ElemType value, Compare fun)
{
    if (nullptr == sq_list) exit(0);

    for (int i = 0; i < sq_list->length; i++)
    {
        if (fun(value, sq_list->data[i]))
        {
            return i;
        }
    }

    return -1;
}


bool Full(SqList* sq_list)
{
    if (sq_list == nullptr)  exit(0);

    return sq_list->length == sq_list->size;
}

bool Empty(SqList* sq_list)
{
    if (sq_list == nullptr) exit(0);
    return sq_list->length == 0;
}

void Show(SqList* sq_list)
{
    if (nullptr == sq_list) exit(0);
    printf("%p :", sq_list->data);
    for (int i = 0; i < sq_list->length; i++)
    {
        printf("%d ", sq_list->data[i]);
    }
    printf("\n");
}

ps:这些代码可直接拷贝使用,是验证过的,若是出错务必告诉我,别让我一错到底....(呜呜呜).

 

posted @ 2021-04-28 20:53  Wz_qq_2***6  阅读(57)  评论(0)    收藏  举报