C语言:动态顺序表的增删查改,数据结构

顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构。线性表采用顺序存储的方式存储就称之为顺序表。顺序表是将表中的结点依次存放在计算机内存中一组地址连续的存储单元中。
是处理数据结构的一种简单的方法。
动态顺序结构与静态顺序结构之间的区别在于,动态顺序结构能够根据情况在内存池中申请空间。


SeqList.h

#ifndef __SEQLIST_H__
#define __SEQLIST_H__

#define _CRT_SECURE_NO_WARNINGS 10
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef int DataType;

typedef struct SeqList
{
    DataType* array;
    size_t size;
    size_t capacity;
}SeqList;

void CheckCapacity(SeqList *seq);//检查空间是否够大  
void InitSeqList(SeqList* seq);//初始化顺序表  
void DestorySeqList(SeqList* seq);//销毁顺序表  

void PushBack(SeqList* seq, DataType x);//从尾部插入元素  
void Popback(SeqList* seq);//从尾部删除元素  

void PushFront(SeqList* seq, DataType x);//从头部插入元素  
void PopFront(SeqList* seq);//从头部删除元素  

void Insert(SeqList* seq, size_t pos, DataType x);//指定位置插入元素  
int find(SeqList* seq, DataType x);// 寻找元素  
void Erase(SeqList* seq, size_t pos);// 删除指定位置元素  
void Remove(SeqList* seq, DataType x);//删除元素X  
void RemoveAll(SeqList* seq, DataType x);//删除顺序表中所有的X  

void BubbleSort(SeqList* seq);//冒泡排序  
void SelectSort(SeqList* seq);//选择排序  
void InsertSort(SeqList* seq);//插入排序  
int BinarySearch(SeqList* seq, DataType x);//二分查找  
int BinarySearchD(SeqList* seq, size_t begin, size_t end, DataType x);//递归,二分查找  
void PrintSeqList(SeqList* seq);//输入顺序表  

#endif//__SEQLIST_H__

SeqList.c

#include"SeqList.h"

void CheckCapacity(SeqList *seq)//检查是否需要扩容  
{
    if (seq->size >= seq->capacity)
    {
        seq->array = (DataType*)realloc(seq->array, sizeof(DataType)*(seq->capacity) * 2);
        assert(seq->array);
        seq->capacity *= 2;
    }
}

void InitSeqList(SeqList* seq)//初始化顺序表  
{
    assert(seq);
    seq->array = (DataType*)malloc(3 * sizeof(DataType));
    assert(seq->array);

    seq->size = 0;
    seq->capacity = 3;
}

void DestorySeqList(SeqList* seq)//销毁顺序表  
{
    assert(seq);
    if (seq->array)
    {
        free(seq->array);
    }
    seq->array = NULL;
    seq->size = 0;
    seq->capacity = 0;
}

void PushBack(SeqList* seq, DataType x)//从尾部插入元素  
{
    assert(seq);
    CheckCapacity(seq);

    seq->array[seq->size] = x;
    seq->size++;
}

void Popback(SeqList* seq)//从尾部删除元素  
{
    assert(seq);

    if (seq->size <= 0)
    {
        printf("The SeqList is empty\n");
        return;
    }
    seq->size--;
}

void PushFront(SeqList* seq, DataType x)//从头部插入元素  
{
    assert(seq);
    CheckCapacity(seq);
    int end = seq->size;
    while (end >= 0)//数组存储从下标0开始到size-1,因为增加一个数进入数组,指向size,size++
    {
        seq->array[end] = seq->array[end - 1];
        end--;
    }
    seq->size++;//size++放在前面可以增加程序的可读性
    seq->array[0] = x;
}

void PopFront(SeqList* seq)//从头部删除元素  
{
    assert(seq);
    size_t end = 0;

    if (seq->size <= 0)
    {
        printf("The SeqList is empty\n");
        return;
    }

    while (end < seq->size-1)
    {
        seq->array[end] = seq->array[end + 1];
        end++;
    }
    seq->size = seq->size - 1;
}

void Insert(SeqList* seq, size_t pos, DataType x)//指定位置插入元素  
{
    assert(seq);
    CheckCapacity(seq);
    size_t i;
    seq->size++;
    if ((0 > pos) || (pos>seq->size - 1))
    {
        printf("pos illegal input!!! \n");
        return;
    }
    for (i = seq->size; i > pos; i--)
    {
        seq->array[i] = seq->array[i - 1];
    }
    seq->array[pos] = x;
}

int Find(SeqList* seq, DataType x)// 寻找元素  
{
    assert(seq);

    size_t i = 0;
    while (i < seq->size)
    {
        if (seq->array[i] == x)
        {
            return i+1;
        }
    }
    return -1;
}

void Erase(SeqList* seq, size_t pos)// 删除指定位置元素  
{
    assert(seq);
    assert(pos > 0);
    assert(pos <= seq->size);
    size_t end = pos; 
    if (seq->size <= 0)
    {
        printf("The SeqList is empty\n");
        return;
    }

    while (end < seq->size - 2)
    {
        end++;
    }
    seq->size--;
}

void Remove(SeqList* seq, DataType x)//删除元素X  
{
    assert(seq);

    size_t ret = Find(seq, x);
    Erase(seq, ret);
}

void RemoveAll(SeqList* seq, DataType x)//删除顺序表中所有的X ,跳过需要删除的数 
{
    assert(seq);
    if (seq->size <= 0)
    {
        printf("The SeqList is empty\n");
        return;
    }
    size_t i = 0;
    size_t j = 0;
    int count = 0;
    for (i = 0; j < seq->size; i++)
    {
        if (seq->array[i] == x)
        {
            count++;
        }
        else
        {
            seq->array[j] = seq->array[i];
            j++;
        }
    }
    seq->size -= count;
}

void swap(int *a, int *b)
{
    int  tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
}

void BubbleSort(SeqList* seq)//冒泡排序  
{
    assert(seq);
    int flag = 0;
    if (seq->size <= 0)
    {
        printf("The SeqList is empty\n");
        return;
    }
    do{
        for (size_t i = 0; i < seq->size; i++)
        {
            for (size_t j = i + 1; j < seq->size; j++)
            {
                if (seq->array[i]>seq->array[j])
                {
                    swap(&(seq->array[i]), &(seq->array[j]));
                    flag = 1;
                }
            }
        }
    } while (flag);
}

void SelectSort(SeqList* seq)//选择排序  
{
    assert(seq);
    if (seq->size <= 0)
    {
        printf("The SeqList is empty\n");
        return;
    }
    int begin = 0;
    int end = seq->size - 1;
    while (begin <= end)
    {
        int MinIndex = begin;
        int MaxIndex = end;
        for (int i = begin; i <= end; i++)
        {
            if (seq->array[i] < seq->array[MinIndex])
                MinIndex = i;
            if (seq->array[i] > seq->array[MaxIndex])
                MaxIndex = i;
        }
        swap(&(seq->array[begin]), &(seq->array[MinIndex]));
        if (MaxIndex == begin)
        {
            MaxIndex = MinIndex;
        }
        swap(&(seq->array[end]), &(seq->array[MaxIndex]));
        begin++;
        end--;
    }
}

void InsertSort(SeqList* seq)//插入排序  
{
    assert(seq);
    if (seq->size <= 0)
    {
        printf("The SeqList is empty\n");
        return;
    }
    for (size_t i = 0; i < seq->size - 1; i++)
    {
        int end = i;
        int tmp = seq->array[end + 1];
        while (end >= 0 && seq->array[end] > tmp)
        {
            seq->array[end + 1] = seq->array[end];
            end--;
        }
        seq->array[end + 1] = tmp;
    }
}

int BinarySearch(SeqList* seq, DataType x)//二分查找  
{
    assert(seq);
    if (seq->size <= 0)
    {
        printf("The SeqList is empty\n");
        return -1;
    }
    int begin = 0;
    int end = seq->size - 1;
    while (begin <= end)
    {
        int mid = begin + (end - begin) / 2;
        if (seq->array[mid] < x)
        {
            begin = mid + 1;
        }
        else if (seq->array[mid] > x)
        {
            end = mid - 1;
        }
        else
            return mid + 1;
    }
    return -1;
}

int BinarySearchD(SeqList* seq, size_t begin, size_t end, DataType x)//递归,二分查找  
{
    assert(seq);
    if (begin > end)
        return -1;
    int mid = begin + ((end - begin) >> 1); //优先级  
    if (seq->array[mid] ==x)
        return mid;
    else if (seq->array[mid] > x)
        BinarySearchD(seq, begin, mid - 1, x);
    else if (seq->array[mid] < x)
        BinarySearchD(seq, mid + 1, end, x);
}

void PrintSeqList(SeqList* seq)
{
    assert(seq);
    size_t i = 0;
    for (i = 0; i < seq->size; i++)
    {
        printf("%d ", seq->array[i]);
    }
    printf("\n");
}

main.c

#include"SeqList.h"
SeqList s;

void test1()
{
    InitSeqList(&s);
    PushFront(&s, 8);
    PushFront(&s, 7);
    PushFront(&s, 6);

    PrintSeqList(&s);

    Insert(&s, 0, 10);

    PrintSeqList(&s);

    DestorySeqList(&s);
}

int main()
{
    test1();
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值