静态顺序表的实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41112517/article/details/79945802

Seqlist.h

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<malloc.h>
#define MAX_SIZE 10
typedef int DataType;
typedef struct Seqlist
{
    DataType _array[MAX_SIZE];
    int _size;//1、顺序表中有效元素的个数.2尾插时新元素存在的位置。 
}Seqlist, *PSeqlist;

void SeqlistInit(PSeqlist ps);//初始化顺序表
void SeqlistPushBack(PSeqlist ps, DataType data);//尾插
void SeqlistPopBack(PSeqlist ps);//尾删
void SeqlistPushFront(PSeqlist ps, DataType data);//头插
void SeqlistPopFront(PSeqlist ps);//头删
void SeqlistInsert(PSeqlist ps, int pos, DataType data);//任意位置为插入
void SeqlistErase(PSeqlist ps, int pos); //任意位置为删除
void Remove(PSeqlist ps, DataType data);//从前往后走删除值为data的元素
void RemoveAll(PSeqlist ps, DataType data); //删除所有值为data的元素
int SeqlistFind(PSeqlist ps, DataType data); //找到一个值为data的元素
int SeqlistSize(PSeqlist ps);//知道顺序表的大小
void PrintSeqlist(PSeqlist ps);//打印顺序表
void BubbleSort(PSeqlist ps);//冒泡排序
void SelectSort(PSeqlist ps);//选择排序

Seqlist.c

#include<stdio.h>
#include"Seqlist.h"
void SeqlistInit(PSeqlist ps)
{
    if (ps)
        ps->_size = 0;
}
//1、参数检测
//2、边界条件限制
//3、逻辑操作
void SeqlistPushBack(PSeqlist ps, DataType data)
{
    if (NULL == ps)//判断顺序表有没有
        return;
    if (ps->_size == MAX_SIZE)
    {
        printf("顺序表已经满了!!\n");
        return;
    }
    ps->_array[ps->_size] = data;
    ps->_size++;

}
void SeqlistPopBack(PSeqlist ps)
{
    if (NULL == ps)
        return;
    if (ps->_size == 0)
    {
        printf("顺序表已经是空的!!\n");
        return;
    }
    ps->_size--;
}
void SeqlistPushFront(PSeqlist ps, DataType data)
{
    int i = 0;
    if (NULL == ps)
        return;
    if (ps->_size == MAX_SIZE)//检测顺序表是否已经满了。
    {
        printf("顺序表已经满了!!\n");
        return;
    }
    //搬移元素
    //第一种方法
    for (i = ps->_size; i > 0; --i)//i从size开始i不需要等于0.
    {
        ps->_array[i] = ps->_array[i - 1];
    }
    //第二种方法
    //for (i = ps->_size - 1; i >= 0; --i)
    //{
    //  ps->_array[i + 1] = ps->_array[i];
    //}
    //插入元素
    ps->_array[0] = data;
    ps->_size++;


}
void SeqlistPopFront(PSeqlist ps)
{
    int i = 0;
    if (NULL == ps)
        return;
    if (ps->_size == 0)
    {
        printf("顺序表已经是空的!!\n");
        return;
    }
    //搬移元素
    //第一种方法
    for (i = 0; i < ps->_size - 1; i++)
    {
        ps->_array[i] = ps->_array[i + 1];
    }
    //第二种方法
    //for (i = 1; i < ps->_size; ++i)
    //{
    //  ps->_array[i - 1] = ps->_array[i];
    //}
    ps->_size--;

}

void SeqlistInsert(PSeqlist ps, int pos, DataType data)
{
    int i = 0;
    if (NULL == ps || !(pos >= 0 && pos <= ps->_size))//这是正确的
    if ((pos >= 0 && pos <= ps->_size) || NULL == ps)//这是错误的
        return;
    if (ps->_size == MAX_SIZE)//检测顺序表是否已经满了。
    {
        printf("顺序表已经满了!!\n");
        return;
    }
    for (i = ps->_size - 1; i >= pos; --i)
    {
        ps->_array[i + 1] = ps->_array[i];  //往后移

    }
    ps->_array[pos] = data;
    ps->_size++;

}

void SeqlistErase(PSeqlist ps, int pos)
{
    int i = pos;
    if (NULL == ps || !(pos >= 0 && pos < ps->_size))
        return;
    if (0 == ps->_size)
    {
        printf("顺序表为空,无法删除!!!\n");
        return;
    }
    for (i = pos; i < ps->_size - 1; ++pos)
    {
        ps->_array[pos] = ps->_array[pos + 1];

    }
    ps->_size--;

}
int SeqlistFind(PSeqlist ps, DataType data)
{
    int i = 0;
    if (NULL == ps)
        return;
    for (i = 0; i < ps->_size; ++i)
    {
        if (ps->_array[i] == data)
            return i;
    }
    return -1;

}
void Remove(PSeqlist ps, DataType data)
{
    SeqlistErase(ps, SeqlistFind(ps, data));

}
void RemoveAll(PSeqlist ps, DataType data)
{
    //方法1、将SeqlistFind和SeqlistErase两个函数结合起来
    //int pos = 0;
    //while (-1 != (pos = SeqlistFind(ps, data)))
    //  SeqlistErase(ps, pos);
    //方法2、借助辅助数组,将不为data的元素全部放到新的空间中,
    //将新的空间的元素拷贝到顺序表中。
    //方法3
    int count = 0;
    int i = 0;
    for (i = 0; i < ps->_size; ++i)
    if (ps->_array[i] == data)
    {
        count++;
    }

    else
    {
        ps->_array[i - count] = ps->_array[i];
    }
    ps->_size -= count;

}

int SeqlistSize(PSeqlist ps)
{
    if (NULL == ps)
    {
        printf("顺序表为空!!!\n");
        return 0;
    }
    return ps->_size;

}
void PrintSeqlist(PSeqlist ps)
{
    int i = 0;
    if (NULL == ps)
        return;
    for (i = 0; i < ps->_size; ++i)
    {
        printf("%d ", ps->_array[i]);
    }
    printf("\n");


}
void Swap(int* pLeft, int* pRight)
{
    int temp = *pLeft;
    *pLeft = *pRight;
    *pRight = temp;


}
void BubbleSort(PSeqlist ps)
{
    int j = 0;
    int i = 0;
    int IsChange = 0;
    for (i = 0; i < ps->_size - 1; ++i)//冒泡的趟数
    {
        for (j = 0; j < ps->_size - i - 1; ++j)
        {
            Swap(&(ps->_array[j]), &(ps->_array[j + 1]));
            IsChange = 1;
        }
    }
    if (!IsChange)
        return;



}

void TestSeqlist()
{
    Seqlist s;
    SeqlistInit(&s);

}
void TestSeqlistPushPop()
{

    Seqlist s;
    SeqlistInit(&s);
    SeqlistPushBack(&s, 1);
    SeqlistPushBack(&s, 2);
    SeqlistPushBack(&s, 3);
    SeqlistPushBack(&s, 4);
    SeqlistPushBack(&s, 5);
    SeqlistPushBack(&s, 6);//尾插入元素
    printf("size = %d\n", SeqlistSize(&s));
    PrintSeqlist(&s);

    SeqlistPopBack(&s);
    SeqlistPopBack(&s);//尾删了两个元素
    printf("size = %d\n", SeqlistSize(&s));
    PrintSeqlist(&s); 
    SeqlistPushFront(&s, 0);//头插一个0
    printf("size = %d\n", SeqlistSize(&s));
    PrintSeqlist(&s);
    SeqlistPopFront(&s);//头删
    printf("size = %d\n", SeqlistSize(&s));
    PrintSeqlist(&s);


}


void TestSeqlistInsertErase()
{

    Seqlist s;
    SeqlistInit(&s);
    SeqlistPushBack(&s, 1);
    SeqlistPushBack(&s, 2);
    SeqlistPushBack(&s, 3);
    SeqlistPushBack(&s, 4);
    SeqlistPushBack(&s, 5);
    SeqlistPushBack(&s, 6);//尾插入元素
    printf("size = %d\n", SeqlistSize(&s));
    PrintSeqlist(&s);

    SeqlistInsert(&s, 3, 7);//在3号位置插入一个7也就变成了1237456
    printf("size = %d\n", SeqlistSize(&s));
    PrintSeqlist(&s);


    SeqlistErase(&s, 3, 7);//在3号位置删除一个7也就变成了123456
    printf("size = %d\n", SeqlistSize(&s));
    PrintSeqlist(&s);


}
void Remove1()
{
    Seqlist s;
    SeqlistInit(&s);
    SeqlistPushBack(&s, 1);
    SeqlistPushBack(&s, 2);
    SeqlistPushBack(&s, 3);
    SeqlistPushBack(&s, 4);
    SeqlistPushBack(&s, 5);
    SeqlistPushBack(&s, 6);//尾插入元素
    printf("size = %d\n", SeqlistSize(&s));
    PrintSeqlist(&s);
    Remove(&s, 2);
    printf("size = %d\n", SeqlistSize(&s));
    PrintSeqlist(&s);

}

void TestSeqlistRemoveAll()
{
    Seqlist s;
    SeqlistInit(&s);
    SeqlistPushBack(&s, 1);
    SeqlistPushBack(&s, 2);
    SeqlistPushBack(&s, 3);
    SeqlistPushBack(&s, 4);
    SeqlistPushBack(&s, 5);
    SeqlistPushBack(&s, 6);
    SeqlistPushBack(&s, 5);
    SeqlistPushBack(&s, 2);
    SeqlistPushBack(&s, 2);
    printf("size = %d\n", SeqlistSize(&s));
    PrintSeqlist(&s);

    RemoveAll(&s, 2);
    PrintSeqlist(&s);
    printf("size = %d\n", SeqlistSize(&s));
    PrintSeqlist(&s);


}

int main()
{
    //分别注释之后进行测试。
    // TestSeqlist();
    //TestSeqlistPushPop();
    //TestSeqlistInsertErase();
    //Remove1();
    //TestSeqlistRemoveAll();
    return 0;

}
阅读更多

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