【推荐】数据结构:顺序表C语言实现

顺序表的基本功能C语言实现: (基于静态数组)
1. 初始化
2. 尾插
3. 尾删
4. 头插
5. 头删
6. 读任意位置元素
7. 修改任意位置元素
8. 查找指定元素值的下标
9. 在任意位置插入元素
10.删除顺序表中指定的值, 如果存在重复元素, 只删除第一个
11.删除顺序表中所有的指定的值, 另外要实现一个时间复杂度为 O(N) 的优化版本
12.获取顺序表元素个数
13.判定顺序表是否为空
14.冒泡排序

L.h //顺序表头文件

#pragma once //防止头文件重复定义
#include<stdio.h>
#include<stdlib.h>

#define MAX_SIZE 100      //表示数组的最大长度
#define TEST_HEADER printf("\n========%s=======\n",__FUNCTION__) //定义宏,当前函数的函数名

typedef char SeqListType;  
typedef struct SeqList          //定义结构体
{
    SeqListType arr[MAX_SIZE];  //最大空间
    size_t  size;              //有效元素

}SeqList;

void SeqListInit(SeqList *L);  //初始化
void SeqListPushBack(SeqList *L, SeqListType e); //尾插
void SeqListPopBack (SeqList *L);    //尾删
void SeqListPushFront(SeqList *L, SeqListType e); //头插
void SeqListPopFront(SeqList *L);   //头删
void SeqListInsert(SeqList *L, int pos, SeqListType e); //任意位置插入
void SeqListErase(SeqList *L, int pos);  //人任意位置删除
void SeqListGetValue(SeqList *L, int pos); //读取任意位置元素
void SeqListSetValue(SeqList *L, int pos,SeqListType e); //修改任意位置元素
size_t SeqListGetPos(SeqList *L, SeqListType e); //查找指定元素的下标
void SeqListRemove(SeqList *L, SeqListType to_remove); //删除指定的值如果存在重复元素只删除第一个
void SeqListRemoveAll(SeqList *L, SeqListType to_delete); //删除所有的指定元素,实现时间复杂度O(N)的优化
size_t SeqListSize(SeqList *L); //获取顺序表元素个数
int SeqListEmpty(SeqList *L);  //判断顺序表是否为空
void SeqListBubbleSort(SeqList *L);  //冒泡排序

L.c //功能实现文件

#include "L.h"
void SeqListInit(SeqList *L)   //顺序表初始化
{
    if (L == NULL)
    {
        printf("非法输入\n");
        return;
    }
    L->size = 0;
}
void SeqListPrintChar(SeqList*L, const char *msg)  //打印函数
{
    if (L == NULL)
    {
        printf("非法输入");
        return;
    }
    printf("[%s]\n", msg);
    size_t i = 0;
    for (i = 0; i < L->size; ++i)
    {
        printf("[%c]", L->arr[i]);
    }
    printf("\n");
}
void SeqListPushBack(SeqList *L, SeqListType e)  //尾插
{
    if (L == NULL)
    {
        printf("非法输入\n");
        return;
    }
    if (L->size >= MAX_SIZE)
    {
        printf("顺序表已经满了");
        return;
    }
    L->arr[L->size] = e;
    ++L->size;         //更新元素个数
}


void SeqListPopBack(SeqList *L)    //尾删
{
    if (L == NULL)
    {
        printf("非法输入\n");
        return;
    }
    if (L->size == 0)
    {
        printf("空顺序表\n");
        return;
    }
    --L->size;    //使最后一个元素无效
    return;
}

void SeqListPushFront(SeqList *L, SeqListType e)   //头插
{
    if (L == NULL)
    {
        printf("非法输入\n");
        return;
    }
    if (L->size >= MAX_SIZE)
    {
        printf("顺序表已经满了\n");
        return;
    }
    ++L->size;
    size_t i = L->size-1;
    for (; i > 0;i--)
    {
        L->arr[i] = L->arr[i - 1];
    }
    L->arr[0] = e;
    return;
}
void SeqListPopFront(SeqList *L)  //头删
{
    if (L == NULL)
    {
        printf("非法输入\n");
        return;
    }
    if (L->size == 0)
    {
        printf("空顺序表");
        return;
    }

    size_t i = 0;
    for (; i < L->size - 1; i++)
    {
        L->arr[i] = L->arr[i+1];
    }
    --L->size;
    return;
}


void SeqListInsert(SeqList *L, int pos, SeqListType e)   //任意位置插入
{
    if (L == NULL)
    {
        printf("非法输入\n");
        return;
    }
    if (L->size == MAX_SIZE)
    {
        printf("顺序表已经满了");
        return;
    }
    if (pos == 0)
    {
        SeqListPushFront(L, e);
    }
    ++L->size;
    int i = L->size - 1;
    for (; i > pos; i--)
    {
        L->arr[i] = L->arr[i-1];
    }
    L->arr[pos] = e;
    return;
}

void SeqListErase(SeqList *L, int pos)  //任意位置删除
{
    if (L == NULL)
    {
        printf("非法输入\n");
        return;
    }
    if (L->size == 0)
    {
        printf("空顺序表");
        return;
    }
    if (pos >= L->size)
    {
        return;
    }
    int i = pos;
    for (; i < L->size - 1; i++)
    {
        L->arr[i] = L->arr[i + 1];
    }
    --L->size;
    return;
}

void SeqListGetValue(SeqList *L, int pos)  //读取任意位置的元素
{
    if (L == NULL)
    {
        printf("非法输入\n");
        return;
    }
    if (L->size == 0)
    {
        printf("空顺序表");
        return;
    }
    if (pos >= L->size)
    {
        printf("读取超过范围");
        return;
    }
    printf("要查找的元素为 %c\n", L->arr[pos]);
    return L->arr[pos];
}

void SeqListSetValue(SeqList *L, int pos, SeqListType e)  //修改任意位置的元素
{
    if (L == NULL)
    {
        printf("非法输入\n");
        return;
    }
    if (pos >= L->size)
    {
        printf("读取超过范围");
        return;
    }
    L->arr[pos] = e;
    return;

}

size_t SeqListGetPos(SeqList *L, SeqListType value){//查找指定元素值的下标
    if (L == NULL){
        printf("非法输入\n");
        return;
    }
    size_t i = 0;
    for (; i <L->size; i++){
        if (L->arr[i] == value){
            printf("%d\n", i);
            return i;
        }
    }
    printf("非法坐标\n");
    return -1;
}

void SeqListRemove(SeqList *L, SeqListType to_remove)    //删除指定的值如果存在重复元素只删除第一个
{
    if (L == NULL)
    {
        printf("非法输入\n");
        return;
    }
    size_t i = SeqListGetPos(L, to_remove);
    if (i < 0)
    {
        return;
    }
    SeqListErase(L, i);
    return;

}

void SeqListRemoveAll(SeqList* L, SeqListType to_delete){//删除顺序表中所有的指定的值,实现时间复杂度O(N)的优化
    if (L == NULL)
    {
        printf("非法输入\n");
        return;
    }
    size_t i = 0;
    for (; i < L->size - 1; i++)
    {
        size_t i = SeqListGetPos(L, to_delete);
        if (i < 0)
        {    
            printf("没有找到");
            return;
        }
        SeqListErase(L, i);
    }
    return;
}

size_t SeqListSize(SeqList *L)   //获取顺序表元素的个数
{
    if (L == NULL)
    {
        printf("非法输入\n");
        return;
    }
    if (L->size == 0)
    {
        printf("空顺序表\n");
        return;
    }
    return L->size;
}

int SeqListEmpty(SeqList *L)  //判断顺序表是否为空
{
    if (L == NULL)
    {
        printf("非法输入\n");
        return;
    }
    int i = 1;
    if (L->size == 0)
    {
        printf("空顺序表\n");
        return i;
    }
    return 0;
}

static void swapvalue(SeqListType *a, SeqListType *b)  //交换函数
{
    SeqListType tmp = *a;
    *a = *b;
    *b = tmp;
    return;
}
void SeqListBubbleSort(SeqList *L)    //冒泡排序
{
    if (L == NULL)
    {
        printf("非法输入\n");
        return;
    }
    int i = 0;
    int j = 0;
    int flag = 0;
    for (i = 0; i < L->size - 1; i++)
    {
        for (j = 0; j < L->size - i - 1; j++)
        {
            if (L->arr[j]>L->arr[j + 1])
            {
                flag = 1;
                swapvalue(&L->arr[j], &L->arr[j + 1]);

            }
        }
        if (flag == 0)
        {
            break;
        }
    }

}

test.c //测试文件

#include "L.h" 
void TestInit()    //测试初始化
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    printf("L.size expect 0,actual %lu\n", L.size);
}
void TestPushBack()//尾插
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    SeqListPushBack(&L, 'a');
    SeqListPushBack(&L, 'b');
    SeqListPushBack(&L, 'c');
    SeqListPushBack(&L, 'd');
    SeqListPrintChar(&L, "尾部插入四个元素");

}

void TestPopBack()   //尾删
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    SeqListPushBack(&L, 'a');
    SeqListPushBack(&L, 'b');
    SeqListPushBack(&L, 'c');
    SeqListPushBack(&L, 'd');
    SeqListPopBack(&L);
    SeqListPopBack(&L);
    SeqListPrintChar(&L, "尾部删除两个元素");
}

void TestPushFront()           //头插
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    SeqListPushBack(&L, 'a');
    SeqListPushBack(&L, 'b');
    SeqListPushBack(&L, 'c');
    SeqListPushBack(&L, 'd');
    SeqListPushFront(&L, 'x');
    SeqListPushFront(&L, 'y');
    SeqListPrintChar(&L, "头部插入两个元素");

}

void  TestPopFront()      //头删
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    SeqListPushBack(&L, 'a');
    SeqListPushBack(&L, 'b');
    SeqListPushBack(&L, 'c');
    SeqListPushBack(&L, 'd');
    SeqListPopFront(&L);
    SeqListPopFront(&L);
    SeqListPrintChar(&L, "头部删除两个元素");

}

void TestInsert()
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    SeqListPushBack(&L, 'a');
    SeqListPushBack(&L, 'b');
    SeqListPushBack(&L, 'c');
    SeqListPushBack(&L, 'd');
    SeqListInsert(&L, 2, 'y');
    SeqListPrintChar(&L, "任意位置插入");
}

void TestErase()   //任意位置删除
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    SeqListPushBack(&L, 'a');
    SeqListPushBack(&L, 'b');
    SeqListPushBack(&L, 'c');
    SeqListPushBack(&L, 'd');
    SeqListErase(&L, 1);
    SeqListPrintChar(&L, "任意位置删除");

}

void TestGetValue()   //读取任意位置的元素
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    SeqListPushBack(&L, 'a');
    SeqListPushBack(&L, 'b');
    SeqListPushBack(&L, 'c');
    SeqListPushBack(&L, 'd');
    SeqListGetValue(&L, 1);
    SeqListPrintChar(&L, "在2的位置读取元素值");
}

void TestSetValue()   //修改任意位置的元素值
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    SeqListPushBack(&L, 'a');
    SeqListPushBack(&L, 'b');
    SeqListPushBack(&L, 'c');
    SeqListPushBack(&L, 'd');
    SeqListSetValue(&L, 1, 'X');
    SeqListPrintChar(&L, "把2的位置元素值改为X");
}

void TestGetPos()   // 查找指定元素的下标
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    SeqListPushBack(&L, 'a');
    SeqListPushBack(&L, 'b');
    SeqListPushBack(&L, 'c');
    SeqListPushBack(&L, 'd');
    SeqListGetPos(&L, 'b');
    SeqListPrintChar(&L, "查找元素b的下标");
}

void TestRemove()
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    SeqListPushBack(&L, 'a');
    SeqListPushBack(&L, 'b');
    SeqListPushBack(&L, 'b');
    SeqListPushBack(&L, 'c');
    SeqListPushBack(&L, 'd');
    SeqListRemove(&L, 'b');
    SeqListPrintChar(&L, "删除第一个b");
}
void TestRemoveAll()
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    SeqListPushBack(&L, 'a');
    SeqListPushBack(&L, 'b');
    SeqListPushBack(&L, 'b');
    SeqListPushBack(&L, 'c');
    SeqListPushBack(&L, 'd');
    SeqListRemoveAll(&L,'b');
    SeqListPrintChar(&L, "删除所有的元素b");

}

void TestSize()
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    SeqListPushBack(&L, 'a');
    SeqListPushBack(&L, 'b');
    SeqListPushBack(&L, 'c');
    SeqListPushBack(&L, 'd');
    size_t t=SeqListSize(&L);
    SeqListPrintChar(&L, "获取顺序表的长度");
    printf("%d\n", t);
}

void TestEmpty()
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    SeqListPushBack(&L, 'a');
    SeqListPushBack(&L, 'b');
    SeqListPopBack(&L, 'a');
    SeqListPopBack(&L, 'b');
    SeqListPrintChar(&L, "顺序表是否为空");
    SeqListEmpty(&L);

}
void TestBubbleSort()     
{
    TEST_HEADER;
    SeqList L;
    SeqListInit(&L);
    SeqListPushBack(&L, 'a');
    SeqListPushBack(&L, 'd');
    SeqListPushBack(&L, 'c');
    SeqListPushBack(&L, 'b');
    SeqListBubbleSort(&L);
    SeqListPrintChar(&L, "冒泡排序升序");

}
int main()
{

    //TestInit();    //初始化
    //TestPushBack();    //尾插
    //TestPopBack();     //尾删
    //TestPushFront();    //头插
    //TestPopFront();     //头删
    //TestInsert();   //任意位置插入
    //TestErase(); //任意位置删除
    //TestGetValue(); //读取任意位置元素值
    //TestSetValue();   //修改任意位置的元素值
    //TestGetPos();  //查找指定元素的下标
    //TestRemove();   //删除指定的值如果存在重复元素只删除第一个
    //TestRemoveAll();  //删除所有的元素b
    //TestSize();         //获取顺序表的长度 
    //TestEmpty();         //顺序表是否为空
    //TestBubbleSort();    //冒泡排序升序
        return 0;
}

附验证图如下
1 初始化
这里写图片描述
2 尾插
这里写图片描述
3 尾删
这里写图片描述
4 头插
这里写图片描述
5 头删
这里写图片描述
6 任意位置插入
这里写图片描述
7 任意位置删除
这里写图片描述
8 读取任意位置元素值
这里写图片描述
9 修改任意位置的元素值
这里写图片描述
10 查找指定元素的下标
这里写图片描述
11 删除指定的值如果存在重复元素只删除第一个
这里写图片描述
12 删除指定的所有的元素
这里写图片描述
13 获取顺序表的长度
这里写图片描述
14 顺序表是否为空
这里写图片描述
15 冒泡排序升序
这里写图片描述

猜你喜欢

转载自blog.csdn.net/yu876876/article/details/79728734