学习数据结构和算法的第8天

顺序表的实现

进行头插

eg:在数组 1 2 3 4 5的开头插入-1

变成-1 1 2 3 4 5

c 复制代码
#include<stdio.h>
typedef struct SeqList {
    SLDataType a[100]; // 假设顺序表最大容量为100
    int size; // 当前顺序表的大小
} SL;
void SeqListPushFront(SL* ps, SLDataType x)
{
    int end = ps->size-1;
    while (end >= 0) {
        ps->a[end+1] = ps->a[end];
        --end;
    }
    ps->a[0] = x;
    ps->size++;
}
int main()
{
    SL seqList; // 创建一个顺序表对象
    seqList.size = 0; // 初始化顺序表的大小为0
    // 向顺序表的开头插入元素
    SeqListPushFront(&seqList, 10);
    SeqListPushFront(&seqList, 20);
    SeqListPushFront(&seqList, 30);
    // 打印顺序表中的元素
    for (int i = 0; i < seqList.size; i++) {
        printf("%d ", seqList.a[i]);
    }
    printf("\n");
    
    return 0;
}

头删

eg:数组1 2 3 4 5

删去1

c 复制代码
#include <stdio.h>
typedef struct SeqList {
    SLDataType a[100];
    int size;
} SL;
void SeqListRemoveFront(SL* ps)
{
    if (ps->size <= 0) {
        // 顺序表为空,无法进行头删操作
        return;
    }
    for (int i = 0; i < ps->size - 1; i++) {
        ps->a[i] = ps->a[i + 1];
    }
    ps->size--;
}
int main()
{
    SL seqList;
    seqList.size = 0;
    // 添加一些元素到顺序表
    seqList.a[0] = 10;
    seqList.a[1] = 20;
    seqList.a[2] = 30;
    seqList.size = 3;
    // 执行头删操作
    SeqListRemoveFront(&seqList);
    // 打印顺序表中的元素
    for (int i = 0; i < seqList.size; i++) {
        printf("%d ", seqList.a[i]);
    }
    printf("\n");
    return 0;
}
相关推荐
卷福同学30 分钟前
【养虾日记】Openclaw操作浏览器自动化发文
人工智能·后端·算法
春日见1 小时前
如何入门端到端自动驾驶?
linux·人工智能·算法·机器学习·自动驾驶
图图的点云库2 小时前
高斯滤波实现算法
c++·算法·最小二乘法
CrystalShaw2 小时前
[AI codec]opus-1.6\DRED 编码侧 学习笔记
笔记·学习
张张123y2 小时前
RAG从0到1学习:技术架构、项目实践与面试指南
人工智能·python·学习·面试·架构·langchain·transformer
·醉挽清风·2 小时前
学习笔记—Linux—文件IO
linux·服务器·学习
Accerlator2 小时前
计算机网络学习
学习·计算机网络
一叶落4382 小时前
题目:15. 三数之和
c语言·数据结构·算法·leetcode
星爷AG I2 小时前
14-12 动作序列学习(AGI基础理论)
人工智能·学习·agi
y = xⁿ2 小时前
【LeetCodehot100】2:两数相加 19 删除链表倒数第n个节点
数据结构·链表