Teach you how to quickly tear up a sequence table

SeqList.h

// SeqList.h
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>


typedef int SLDataType;
typedef struct SeqList
{
    SLDataType* arr;//指向动态开辟的结构体数组
    int sz;//存储数据的个数
    int capacity;//容量
}SeqList;

// 对数据的管理:增删查改 
void SeqListInit(SeqList* ps);
void SeqListDestroy(SeqList* ps);

void SeqListPrint(SeqList* ps);
void SeqListPushBack(SeqList* ps, SLDataType x);
void SeqListPushFront(SeqList* ps, SLDataType x);
void SeqListPopFront(SeqList* ps);
void SeqListPopBack(SeqList* ps);

// 顺序表查找
int SeqListFind(SeqList* ps, SLDataType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, int pos, SLDataType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, int pos);

SL.c

#include "seqList.h"

void SeqListInit(SeqList* ps)
{
    ps->arr = (SLDataType*)malloc(sizeof(SeqList) * 4);
    ps->sz = 0;
    ps->capacity = 4;
}

void SeqListDestroy(SeqList* ps)
{
    free(ps->arr);
    ps->arr = NULL;
}

void SeqListPrint(SeqList* ps)
{
    if (ps->arr == NULL)
    {
        return;
    }
    int i = 0;
    for (i = 0; i < ps->sz; i++)
    {
        printf("%d->", ps->arr[i]);
    }
    printf("\n");
    return;
}

void checkCapacity(SeqList *ps)
{
    if (ps->sz == ps->capacity)
    {
        int newcapacity = ps->capacity;
        ps->arr = realloc(ps->arr, sizeof(SLDataType) * newcapacity);
        if (ps->arr == NULL)
        {
            perror("realloc fail");
        }
    }
    return;
}
void SeqListInsert(SeqList* ps, int pos, SLDataType x)
{
    ps->sz++;
    checkCapacity(ps);
    int i = pos;
    for (i =ps->sz-1; i >pos-1; i--)
    {
        ps->arr[i] = ps->arr[i - 1];
    }
    ps->arr[pos] = x;
    return;
}
int SeqListFind(SeqList* ps, SLDataType x)
{
    int i = 0;
    for (i = 0; i < ps->sz; i++)
    {
        if (ps->arr[i] == x)
        {
            return i;
        }
    }
    return -1;
}

void SeqListErase(SeqList* ps, int pos)
{
    int i = 0;
    if (pos == ps->sz)
    {
        ps->arr[pos] = 0;
        ps->sz--;
    }
    else 
    {
        for (i = pos; i < ps->sz-1; i++)
        {
            ps->arr[i] = ps->arr[i + 1];
        }
        ps->sz--;
    }
}
void SeqListPushBack(SeqList* ps, SLDataType x)
{
    SeqListInsert(ps, ps->sz, x);
}
void SeqListPushFront(SeqList* ps, SLDataType x)
{
    SeqListInsert(ps, 0,x);
}
void SeqListPopFront(SeqList* ps)
{
    SeqListErase(ps, 0);
}
void SeqListPopBack(SeqList* ps)
{
    SeqListErase(ps, ps->sz);
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include "seqList.h"
int main()
{
    SeqList SL;
    SeqListInit(&SL);
    SeqListInsert(&SL, 0, 1);
    SeqListInsert(&SL, 0, 2);
    SeqListInsert(&SL, 1, 3);
    SeqListPrint(&SL);
    //int ret=SeqListFind(&SL, 100);
    //printf("%d", ret);
    SeqListErase(&SL, 2);
    SeqListErase(&SL, 1);
    SeqListPrint(&SL);

}

In fact, writing an insertion and deletion at any position solves the problem of head deletion and head insertion at special positions.

Guess you like

Origin blog.csdn.net/m0_74234485/article/details/129172236