顺序表的增删查操作

本文目标:实现基于静态数组的顺序表的以下基本操作:

  1. 初始化
  2. 尾插
  3. 尾删
  4. 头插
  5. 头删
  6. 读任意位置元素
  7. 修改任意位置元素
  8. 查找指定元素值的下标
  9. 在任意位置插入元素

——————————————————————————————
seqlist.h

#pragma once     //防止头文件重复包含

#include  

#define SeqListMaxNum 1000    //数组最大元素个数

typedef char SeqListType;    //重定义类型
typedef struct SeqList
{
    SeqListType SeqListArr[SeqListMaxNum];
    size_t size;      //元素个数
}SeqList;

void SeqListInit(SeqList* seq);       //初始化顺序表
void SeqListPrint(SeqList* seq, char* ch);      //打印顺序表
void SeqListPushEnd(SeqList *seq, SeqListType value);
void SeqListPopEnd(SeqList *seq);   //尾部删除顺序表
void SeqListPushStart(SeqList *seq, SeqListType value); //头部插入顺序表
void SeqListPopStart(SeqList *seq); //头部删除顺序表
void SeqListPushPosition(SeqList* seq, size_t pos, SeqListType value);    //在下标为pos处插入元素
void SeqListPopPosition(SeqList* seq, size_t pos);               //删除下标为pos的元素

void SeqListSetList(SeqList* seq, size_t pos, SeqListType value);     //修改下标为pos的元素

SeqListType SeqListGetList(SeqList* seq, size_t pos);       //读下标为pos的元素
size_t SeqListGetpos(SeqList* seq, SeqListType value);      //查找value元素值的下标

seqlist.c

#include "SeqList.h"
#include 
#include 

//初始化顺序表
void SeqListInit(SeqList* seq)
{
    assert(seq);
    seq->size = 0;
}

//打印顺序表
void SeqListPrint(SeqList* seq, char* ch)
{
    assert(seq);
    int i = 0;
    printf("%s\n", ch);
    for (i = 0; i < (seq->size); i++)
    {
        printf("下标为%d的元素是:[%c]\n", i, seq->SeqListArr[i]);
    }
    printf("size = %ld\n\n", seq->size);
}
//尾部插入元素

void SeqListPushEnd(SeqList *seq, SeqListType value)
{
    assert(seq);
    if (seq->size >= SeqListMaxNum)
        printf("顺序表已满,无法插入\n");
    else
        seq->SeqListArr[seq->size] = value;
        ++seq->size;
}
//尾部删除元素
void SeqListPopEnd(SeqList *seq)
{
    assert(seq);
    if (seq->size == 0)
    {
        printf(",顺序表为空,删除失败\n");
        return;
    }
    else
    {
        --seq->size;

    }

}
//头部插入元素
void SeqListPushStart(SeqList *seq, SeqListType value)
{
    assert(seq);
    if (seq->size >= SeqListMaxNum)
        printf("顺序表已满,无法插入\n");
    else
    {

        int i = seq->size - 1;
        for (; i > 0; i--)
        {
            seq->SeqListArr[i + 1] = seq->SeqListArr[i];    //搬移数据
        }
        seq->SeqListArr[0] = value; //头部插入数据
        ++seq->size;
    }

}
//头部删除元素
void SeqListPopStart(SeqList *seq)
{
    assert(seq);
    if (seq->size == 0)
    {
        printf(",顺序表为空,删除失败\n");
        return;
    }
    else
    {
        int i=0;
        for (; i < (seq->size-1); i++)
        {
            seq->SeqListArr[i] = seq->SeqListArr[i+1];  //搬移数据
        }
        --seq->size;

    }
}
//在任意位置插入元素
void SeqListPushPosition(SeqList* seq, size_t pos, SeqListType value)
{
    assert(seq);
    if (seq->size >= SeqListMaxNum)
        printf("顺序表已满,无法插入\n");
    else if (pos >= seq->size)
    {
        printf("非法坐标!\n\n");
        return;
    }
    else
    {
        int i = seq->size - 1;
        for (; i >= pos; i--)
        {
            seq->SeqListArr[i + 1] = seq->SeqListArr[i];      //将pos下标后所有元素后移,为插入元素留出空间
        }
        seq->SeqListArr[pos] = value;
        seq->size++;
    }
}
//删除下标为pos的元素
void SeqListPopPosition(SeqList* seq, size_t pos)
{
    assert(seq);
    if (seq->size == 0)
    {
        printf("顺序表为空表,无法删除!\n\n");
        return;
    }
    else if (pos >= seq->size)
    {
        printf("非法坐标!\n\n");
        return;
    }
    else
    {
        int i = pos;
        for (; i < seq->size - 1; i++)
        {
            seq->SeqListArr[i] = seq->SeqListArr[i + 1];
        }
        seq->size--;
    }
}
//修改下标为pos的元素
void SeqListSetList(SeqList* seq, size_t pos, SeqListType value)
{
    assert(seq);
    if (pos >= seq->size)
    {
        printf("非法坐标!\n\n");
        return;
    }
    else
    {
        seq->SeqListArr[pos] = value;
    }
}

//读下标为pos的元素
SeqListType SeqListGetList(SeqList* seq, size_t pos)
{
    assert(seq);
    if (pos >= seq->size)
    {
        printf("非法坐标!\n\n");
        return -1;
    }
    else
    {
        return seq->SeqListArr[pos];
    }
}

//查找value元素值的下标
size_t SeqListGetpos(SeqList* seq, SeqListType value)
{
    assert(seq);
    int i = 0;
    for (; i < seq->size; i++)
    {
        if (seq->SeqListArr[i] == value)
        {
            return i;
        }
    }
    return -1;
}

test.c

//测试代码
#include "SeqList.h"
#include 
#include 

//测试初始化顺序表
void TestSeqListInit()
{
    SeqList seq;
    SeqListInit(&seq);
    SeqListPrint(&seq, "*****初始化顺序表*****");
}
//测试尾部插入元素
void TestSeqListPushEnd()
{

    SeqList seq;
    SeqListInit(&seq);
    SeqListPushEnd(&seq, 'a');
    SeqListPushEnd(&seq, 'b');
    SeqListPushEnd(&seq, 'c');
    SeqListPushEnd(&seq, 'd');
    SeqListPrint(&seq, "*****尾插入四个元素到顺序表*****");



}
//测试尾部删除元素
void TestSeqListPopEnd()
{
    SeqList seq;
    SeqListInit(&seq);

    SeqListPushEnd(&seq, 'a');
    SeqListPushEnd(&seq, 'b');
    SeqListPushEnd(&seq, 'c');
    SeqListPushEnd(&seq, 'd');
    SeqListPopEnd(&seq);

    SeqListPrint(&seq, "*****尾部删除元素*****");
    printf("空表情况\n");
    SeqListInit(&seq);
    SeqListPopEnd(&seq);
    printf("\n");



}
void TestSeqListPushStart()
{
    SeqList seq;
    SeqListInit(&seq);
    SeqListPushEnd(&seq, 'a');
    SeqListPushEnd(&seq, 'b');
    SeqListPushEnd(&seq, 'c');
    SeqListPushEnd(&seq, 'd');
    SeqListPushStart(&seq, 'e');
    SeqListPrint(&seq, "*****头插一个元素到顺序表*****");
}
void TestSeqListPopStart()
{
    SeqList seq;
    SeqListInit(&seq);
    SeqListPushEnd(&seq, 'a');
    SeqListPushEnd(&seq, 'b');
    SeqListPushEnd(&seq, 'c');
    SeqListPushEnd(&seq, 'd');
    SeqListPopStart(&seq);
    SeqListPrint(&seq, "*****头删除一个元素*****");
}
//测试在下标为pos处插入元素
void TestSeqListPushPosition()
{
    SeqList seq;
    SeqListInit(&seq);
    SeqListPushEnd(&seq, 'a');
    SeqListPushEnd(&seq, 'b');
    SeqListPushEnd(&seq, 'c');
    SeqListPushEnd(&seq, 'd');
    SeqListPushPosition(&seq, 2, 's');
    SeqListPrint(&seq, "*****在顺序表中指定位置插入一个元素*****");
    printf("非法坐标情况(在下标为20的位置插入元素):\n");
    SeqListPushPosition(&seq, 20, 'r');
}

//测试删除下标为pos的元素
void TestSeqListPopPosition()
{
    SeqList seq;
    SeqListInit(&seq);
    SeqListPushEnd(&seq, 'a');
    SeqListPushEnd(&seq, 'b');
    SeqListPushEnd(&seq, 'c');
    SeqListPushEnd(&seq, 'd');
    SeqListPopPosition(&seq, 2);
    SeqListPrint(&seq, "*****在顺序表中指定位置删除一个元素*****");
    printf("非法坐标情况(删除下标为8的元素):\n");
    SeqListPopPosition(&seq, 8);
}

//测试修改下标为pos的元素
void TestSeqListSetList()
{
    SeqList seq;
    SeqListInit(&seq);
    SeqListPushEnd(&seq, 'a');
    SeqListPushEnd(&seq, 'b');
    SeqListPushEnd(&seq, 'c');
    SeqListPushEnd(&seq, 'd');
    SeqListSetList(&seq, 1, 'o');
    SeqListPrint(&seq, "*****在顺序表中修改指定位置的元素*****");
    printf("非法坐标情况(修改下标为10的元素):\n");
    SeqListSetList(&seq, 10, 'k');
}

//测试读取下标为pos的元素
void TestSeqListGetList()
{
    SeqList seq;
    SeqListInit(&seq);
    SeqListPushEnd(&seq, 'e');
    SeqListPushEnd(&seq, 'f');
    SeqListPushEnd(&seq, 'g');
    SeqListPushEnd(&seq, 'h');
    SeqListPrint(&seq, "*****在顺序表中读取指定位置元素*****");
    char ch = SeqListGetList(&seq, 1);
    printf("下标为1的元素为%c\n", ch);
    printf("非法坐标情况(读取下标为6的元素):\n");
    SeqListGetList(&seq, 6);
}

//测试查找value元素值的下标
void TestSeqListGetpos()
{
    SeqList seq;
    SeqListInit(&seq);
    SeqListPushEnd(&seq, 'e');
    SeqListPushEnd(&seq, 'f');
    SeqListPushEnd(&seq, 'g');
    SeqListPushEnd(&seq, 'h');
    SeqListPrint(&seq, "*****在顺序表中读取指定位置元素下标*****");
    size_t pos = SeqListGetpos(&seq, 'h');
    printf("h元素的下标为%ld\n", pos);
    printf("非法情况(读取元素W的下标):\n");
    size_t pos1 = SeqListGetpos(&seq, 'W');
    printf("W元素的下标为%ld,坐标非法!\n\n", pos1);
}

int main()
{

    TestSeqListInit();
    TestSeqListPushEnd();
    TestSeqListPopEnd();
    TestSeqListPushStart();
    TestSeqListPopStart();
    TestSeqListPushPosition();
    TestSeqListPopPosition();
    TestSeqListSetList();
    TestSeqListGetList();
    TestSeqListGetpos();
    system("pause");
    return 0;
}

顺序表的增删查操作_第1张图片
顺序表的增删查操作_第2张图片

你可能感兴趣的:(数据结构)