教你如何快速手撕一张顺序表

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);

}

事实上,写个任意位置插入和删除就解决特殊位置的头删头插那些了

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脆皮骷髏人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值