数据结构——线性表之顺序表

什么是线性表

它是由n个具有相同特性的数据元素组成的有限序列。顺序表是线性表的一种实现方式,它使用数组在内存中连续存储数据元素。顺序表在逻辑上是线性结构,物理上也可以是连续存储的。

程序实例

首先,我们创建一个头文件 seqlist.h:

// seqlist.h
#ifndef _SEQLIST_H_
#define _SEQLIST_H_

#include <cstddef>
#include <stdio.h>
#include <stdlib.h>

typedef int SLDataType;

typedef struct Seqlist {
    SLDataType* a;
    int size;
    int capacity;
} SL;

void SeqListInit(SL* ps);
void SeqListDestory(SL* ps);
void SeqListPrint(SL* ps);
void CheckCapacity(SL* ps);
void SeqListPushBack(SL* ps, SLDataType x);
void SeqListPopBack(SL* ps);
void SeqListPushFront(SL* ps, SLDataType x);
void SeqListPopFront(SL* ps);
int SeqListFind(SL* ps, SLDataType x);
void SeqListInsert(SL* ps, size_t pos, SLDataType x);
void SeqListErase(SL* ps, size_t pos);

#endif // _SEQLIST_H_

接下来,我们实现这些函数在 seqlist.c 中:

// seqlist.c
#include "seqlist.h"

void SeqListInit(SL* ps) {
    ps->a = NULL;
    ps->capacity = ps->size = 0;
}

void SeqListDestory(SL* ps) {
    free(ps->a);
    ps->a = NULL;
    ps->capacity = ps->size = 0;
}

void SeqListPrint(SL* ps) {
    for (int i = 0; i < ps->size; ++i) {
        printf("%d ", ps->a[i]);
    }
    printf("\n");
}

void CheckCapacity(SL* ps) {
    if (ps->size == ps->capacity) {
        ps->capacity = (ps->capacity == 0) ? 4 : ps->capacity * 2;
        ps->a = (SLDataType*)realloc(ps->a, ps->capacity * sizeof(SLDataType));
        if (ps->a == NULL) {
            fprintf(stderr, "realloc fail\n");
            exit(EXIT_FAILURE);
        }
    }
}

void SeqListPushBack(SL* ps, SLDataType x) {
    CheckCapacity(ps);
    ps->a[ps->size] = x;
    ps->size++;
}

void SeqListPopBack(SL* ps) {
    if (ps->size == 0) {
        fprintf(stderr, "Cannot pop from an empty list\n");
        return;
    }
    ps->size--;
}

void SeqListPushFront(SL* ps, SLDataType x) {
    CheckCapacity(ps);
    for (int i = ps->size; i > 0; --i) {
        ps->a[i] = ps->a[i - 1];
    }
    ps->a[0] = x;
    ps->size++;
}

void SeqListPopFront(SL* ps) {
    if (ps->size == 0) {
        fprintf(stderr, "Cannot pop from an empty list\n");
        return;
    }
    for (int i = 1; i < ps->size; ++i) {
        ps->a[i - 1] = ps->a[i];
    }
    ps->size--;
}

int SeqListFind(SL* ps, SLDataType x) {
    for (int i = 0; i < ps->size; ++i) {
        if (ps->a[i] == x) {
            return i;
        }
    }
    return -1; // Not found
}

void SeqListInsert(SL* ps, size_t pos, SLDataType x) {
    if (pos > ps->size) {
        fprintf(stderr, "Insert position out of range\n");
        return;
    }
    CheckCapacity(ps);
    for (int i = ps->size; i > pos; --i) {
        ps->a[i] = ps->a[i - 1];
    }
    ps->a[pos] = x;
    ps->size++;
}

void SeqListErase(SL* ps, size_t pos) {
    if (pos >= ps->size) {
        fprintf(stderr, "Erase position out of range\n");
        return;
    }
    for (int i = pos; i < ps->size - 1; ++i) {
        ps->a[i] = ps->a[i + 1];
    }
    ps->size--;
}

最后,我们创建一个测试用例 main.c 来展示如何使用这些函数:

// main.c
#include "seqlist.h"

int main() {
    SL list;
    SeqListInit(&list);

    // 测试插入
    SeqListPushBack(&list, 1);
    SeqListPushBack(&list, 2);
    SeqListPushBack(&list, 3);
    SeqListPushFront(&list, 0);
    SeqListInsert(&list, 2, 1.5);

    // 打印顺序表
    printf("List after insertions:\n");
    SeqListPrint(&list);

    // 测试查找
    int found = SeqListFind(&list, 1.5);
    printf("Element 1.5 found at position: %d\n", found);

    // 测试删除
    SeqListPopBack(&list);
    SeqListPopFront(&list);
    SeqListErase(&list, 1);

    // 打印顺序表
    printf("List after deletions:\n");
    SeqListPrint(&list);

    // 清理资源
    SeqListDestory(&list);
    return 0;
}

要运行这个程序,您需要将 seqlist.h、seqlist.c 和 main.c 保存到您的计算机上,然后使用C编译器编译它们。例如,如果您使用的是GCC编译器,可以通过以下命令编译程序:

gcc -o main seqlist.c main.c
./main

原文链接

https://blog.csdn.net/qq_52158753/article/details/129817704

  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值