学习数据接构和算法的第10天

题目讲解

尾插

c 复制代码
#include <stdio.h>
#include <stdlib.h>
// 定义顺序表结构
#define MAX_SIZE 100
struct ArrayList {
    int array[MAX_SIZE];
    int size; // 当前元素个数
};
// 初始化顺序表
void init(struct ArrayList *list) {
    list->size = 0; // 初始时元素个数为0
}
// 尾插操作
void append(struct ArrayList *list, int data) {
    if (list->size < MAX_SIZE) { // 检查顺序表是否已满
        list->array[list->size] = data; // 在数组末尾插入新元素
        list->size++; // 更新元素个数
    } else {
        printf("Error: 顺序表已满,无法插入新元素。\n 
    }
}
// 打印顺序表元素
void printList(struct ArrayList *list) {
    printf("顺序表元素:");
    for (int i = 0; i < list->size; i++) {
        printf("%d ", list->array[i]); // 逐个打印顺序表元素
    }
    printf("\n");
}
int main() {
    struct ArrayList list;
    init(&list); // 初始化顺序表
    // 进行尾插操作
    append(&list, 1);
    append(&list, 2);
    append(&list, 3);
    // 打印顺序表元素
    printList(&list);
    return 0;
}
相关推荐
lisw056 小时前
稀疏算法概述!
算法·机器人·无人机
YLCHUP7 小时前
【括号匹配+dp优化】题解:双端括号(brackets)_动态规划_状态优化_数学建模_贪心_性质挖掘_STLmap_C++算法竞赛
数据结构·c++·笔记·算法·数学建模·贪心算法·动态规划
凌镜icecraft8 小时前
我改文件扩展名的时候那个警告弹窗再也不会弹出来了
windows·c#·wpf·explorer
郝学胜-神的一滴8 小时前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl
console.log('npc')11 小时前
Python第9课 类学习笔记,前端js语法对照
前端·python·学习
the3clipse12 小时前
Windows 11 26H2 AI 功能使用指南:回顾、单击以执行与实时字幕
人工智能·windows·copilot+ pc·recall 回顾·单击以执行·实时字幕翻译·windows studio
geovindu12 小时前
python:Logistic Regression
开发语言·后端·python·算法
geovindu14 小时前
python: Random Forest
开发语言·后端·python·算法·随机森林
凌冰_15 小时前
数据结构 - 线性结构(三)
java·数据结构·算法·软件工程
拂拉氏15 小时前
【知识讲解】 数据结构与算法-树状数组的认识
数据结构·算法·树状数组