当前位置: 首页 > article >正文

【数据结构/C++】线性表_顺序表的基本操作

image.png

#include <iostream>
using namespace std;
#define MaxSize 10
// 1. 顺序表
// 静态分配
typedef struct
{
  int data[MaxSize];
  int length; // 当前长度
} SqList;
// 静态分配初始化顺序表
void InitList(SqList &L)
{
  for (int i = 0; i < MaxSize; i++)
  {
    L.data[i] = 0;
  }
  L.length = 0;
}
// 插入元素 到第 i 个位置
bool ListInsert(SqList &L, int i, int e)
{
  if (i < 1 || i > L.length + 1)
  {
    return false;
  }
  if (L.length >= MaxSize)
  {
    return false;
  }
  for (int j = L.length; j >= i; j--)
  {
    L.data[j] = L.data[j - 1];
  }
  L.data[i - 1] = e;
  L.length++;
  return true;
}
// 删除第 i 个位置的元素
bool ListDelete(SqList &L, int i, int &e)
{
  if (i < 1 || i > L.length)
  {
    return false;
  }
  // 将被删除的元素赋值给 e,便于输出
  e = L.data[i - 1];
  for (int j = i; j < L.length; j++)
  {
    L.data[j - 1] = L.data[j];
  }
  L.length--;
  return true;
}
// 遍历顺序表
void ListTraverse(SqList L)
{
  for (int i = 0; i < L.length; i++)
  {
    cout << L.data[i] << " ";
  }
  cout << endl;
}
int main()
{
  int e;
  SqList L;
  InitList(L);
  ListInsert(L, 1, 1);
  ListInsert(L, 2, 2);
  ListInsert(L, 3, 3);
  ListDelete(L, 2, e);
  cout << "删除的元素是" << e << endl;
  ListTraverse(L);
  return 0;
}

image.png


http://www.kler.cn/news/148237.html

相关文章:

  • Windows11通用快捷键集合
  • 嵌入式开发DDR的选择
  • python-面试重点问题
  • 【深度学习】CNN中pooling层的作用
  • 使用new Vue()的时候发生了什么?
  • Ajax技
  • 解锁领先的有限元分析软件ABAQUS:不同版本功能特点及价格
  • 月底年末如何成交?速看!外贸销冠都在用的催单技巧,让成交量飙升!
  • 【JavaEE初阶】——Linux 基本使用和 web 程序部署(下)
  • H5(uniapp)中使用echarts
  • 【办公软件】XML格式文件怎么转Excel表格文件?
  • C#学习相关系列之数组---常用方法使用(二)
  • C#,《小白学程序》第十六课:随机数(Random)第三,正态分布的随机数的计算方法与代码
  • SpectralGPT: Spectral Foundation Model 论文翻译1
  • 【开源】基于Vue.js的高校学生管理系统的设计和实现
  • 【Linux学习笔记】protobuf 基本数据编码
  • 链表OJ--下
  • 31.0/LinkedList/Set/ashSet/ TreeSet/Map/ HashMap/ TreeMap
  • rtsp点播异常出现‘circluar_buffer_size‘ option was set but it is xx
  • c语言练习12周(15~16)
  • 408—电子笔记分享
  • IEEE Fellow 2024名单揭晓:哪些半导体专家值得关注
  • 力扣二叉树--第三十四天
  • 你真的懂人工智能吗?AI真的只是能陪你聊天而已吗?
  • MySQL的Redo Log跟Binlog
  • C#,《小白学程序》第二十七课:大数四则运算之“运算符重载”的算法及源程序
  • 智慧城市交通大屏|助力解决城市交通问题
  • HarmonyOS 位置服务开发指南
  • 福州大学《嵌入式系统综合设计》 实验八:FFMPEG视频编码
  • C++: String类接口学习