C++实现数组模拟链表(实现链表的增删功能)

代码如下:

#include <iostream>
using namespace std;
const int N = 100;struct Node
{int data;int next;
};class ArrayList
{
private:Node node[N];int maxSize;//数组容量int idx;//接下来要插入的结点的下标int len;//链表长度public:void initList(){node[0].next = 0;//把0当做nullmaxSize = N;len = 0;}bool createList(int n)//创建初始链表{if (n > N - 1) return false;int x;for (int i = 0; i < n; i++){cin >> x;node[i].next = i + 1;node[i + 1].data = x;node[i + 1].next = 0;}idx = n+1;len = 5;return true;}bool insertList(int i, int e)//在i位置插入e{if (idx == maxSize) return false;if (i < 1 || i > len+1 ) return false;if (i == 1){node[idx].data = e;node[idx].next = 0;node[idx].next = node[0].next;node[0].next = idx;idx++;len++;return true;}int cnt = 0;for (int p = node[0].next; p != 0; p = node[p].next){cnt++;if (cnt == i - 1){node[idx].data = e;node[idx].next = 0;node[idx].next = node[p].next;node[p].next = idx;idx++;len++;return true;}}return true;}bool deleteList(int i, int &e)//删除i位置的元素,并把该元素的值返回给传入第二个位置的变量{if (i < 1 || i > len) return false;if (len == 0) return false;if (i == 1){node[0].next = node[node[0].next].next;len--;}int cnt = 0;for (int p = node[0].next; p != 0; p = node[p].next){cnt++;if (cnt == i - 1){node[p].next = node[node[p].next].next;len--;return true;}}return true;}int locateElem(int e)//返回e元素是链表的第几个元素{int cnt = 0;for (int i = node[0].next; i != 0; i = node[i].next){cnt++;if (node[i].data == e){return cnt;}}}bool isElem(int e)//判断元素e是否存在{for (int i = node[0].next; i != 0; i = node[i].next){if (node[i].data == e){return true;}}return false;}void printList()//输出链表{for (int i = node[0].next; i != 0; i = node[i].next){cout << node[i].data << " ";}cout << endl;}
};int main()
{ArrayList l;l.createList(5);l.insertList(1, 3);l.insertList(2, 6);int a;l.deleteList(1, a);l.deleteList(2, a);l.insertList(1, 3);l.insertList(2, 6);cout << l.isElem(33) << endl;cout << l.isElem(23) << endl;cout << l.locateElem(33) << endl;l.printList();return 0;
}

示例如下:

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/309060.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Gartner:容器采用将迅速增长,但不会很快有利可图

导语容器的未来可期&#xff0c;到2024年&#xff0c;所有应用程序中的15&#xff05;将在容器中运行&#xff0c;而这一数据今天仅为5&#xff05;&#xff0c;但当前变现还比较难。正文近日&#xff0c;Gartner公司发表其首次为软件容器管理软件和服务市场做的预测。Gartner说…

快速排序在最坏的情况下时间复杂度(Ω(nlgn)(算法导论第三版9.3-3))

快速排序在最坏的情况下时间复杂度Ω(nlgn) 1⃣️在元素各异时或者少量相等&#xff08;元素个数n>70) 时间复杂度Ω(nlgn) void quick_sort_by_median(int *array,int start,int end) {if(start<end){int median select(array,start,end,(end - start 1)/2 (end - …

线性表的定义与操作-顺序表,链式表(C语言)

顺序表: typedef int Position; typedef struct LNode *List; struct LNode {ElementType Data[MAXSIZE];Position Last; };/* 初始化 */ List MakeEmpty() {List L;L (List)malloc(sizeof(struct LNode));L->Last -1;return L; }/* 查找 */ #define ERROR -1Position Fi…

【Azure Show】|第三期 人工智能大咖与您分享!嘉宾陈海平胡浩陈堰平

欢迎来到Azure Show!Azure ShowHello,大家好&#xff0c;又来到新的一期的Azure Show!本期是人工智能专场&#xff0c;我们邀请到微软Data&AI的解决方案架构师陈堰平&#xff0c;Tensorflow.NET 作者陈海平&#xff0c;还有微软人工智能方向最有价值专家胡浩和大家分享人工…

确定S中最接近中位数的k个元素(算法导论第三版9.3-7)

确定S中最接近中位数的k个元素 &#xff08;算法导论第三版9.3-7题&#xff09; 时间复杂度O(n) vector<int> k_elements_closest_to_median(int *array,int start,int end,int k) {vector<int> result;int median select(array,start,end,(end - start 1)/2 …

深入探究ASP.NET Core异常处理中间件

前言全局异常处理是我们编程过程中不可或缺的重要环节。有了全局异常处理机制给我们带来了很多便捷&#xff0c;首先我们不用满屏幕处理程序可能出现的异常&#xff0c;其次我们可以对异常进行统一的处理&#xff0c;比如收集异常信息或者返回统一的格式等等。ASP.NET Core为我…

找出有序数组X和Y中所有元素的中位数(X,Y分别含n个元素)(算法导论第三版9.3-8)

找出有序数组X和Y中所有元素的中位数&#xff08;X&#xff0c;Y分别含n个元素&#xff09; &#xff08;算法导论第三版9.3-8&#xff09; 时间复杂度O&#xff08;lgn&#xff09; int find_median_two_ordered_arrays(int *array_a,int *array_b,int length) {int a_start…

有序序列中的i个最大数(算法导论思考题9-1)

有序序列中的i个最大数 &#xff08;算法导论思考题9-1&#xff09; a 时间复杂度O(nlgni) //总共时间复杂度O(nlgni) vector<int> i_largest_number_in_ordered_sequence_a(int *array,int length,int i){vector<int>result;//时间复杂度O(nlgn)quick_sort_by_m…

.NET Core加解密实战系列之——消息摘要与数字签名算法

简介加解密现状&#xff0c;编写此系列文章的背景&#xff1a;需要考虑系统环境兼容性问题&#xff08;Linux、Windows&#xff09;语言互通问题&#xff08;如C#、Java等&#xff09;&#xff08;加解密本质上没有语言之分&#xff0c;所以原则上不存在互通性问题&#xff09;…

堆栈的定义与操作-顺序存储,链式存储(C语言)

顺序存储&#xff1a; typedef int Position; struct SNode {ElementType *Data; /* 存储元素的数组 */Position Top; /* 栈顶指针 */int MaxSize; /* 堆栈最大容量 */ }; typedef struct SNode *Stack;Stack CreateStack( int MaxSize ) {Stack S (Stack)malloc(…

快速排序和选择模版类

快速排序和选择模版类 template<typename T> void insert_sort(T *array,int start,int end) {for (int i start 1; i < end 1; i) {int j i - 1 ;T key array[i];while (j > start && key < array[j]){array[j1] array[j];j--;}array[j1] key…

造轮子-AgileConfig一个基于.NetCore开发的轻量级配置中心

微服务确实是行业的一个趋势&#xff0c;我自己也在把一些项目往微服务架构迁移。玩微服务架构配置中心是一个绕不过去的东西&#xff0c;有很多大牌的组件可以选&#xff0c;比如spring-cloud-config&#xff0c;apoll&#xff0c;disconf等等。而我为什么还要造一个轮子呢&am…

队列的定义与操作-顺序存储,链式存储(C语言)

顺序存储&#xff1a; typedef int Position; struct QNode {ElementType *Data; /* 存储元素的数组 */Position Front, Rear; /* 队列的头、尾指针 */int MaxSize; /* 队列最大容量 */ }; typedef struct QNode *Queue;Queue CreateQueue( int MaxSize ) {Que…

带权中位数-算法导论第三版第九章思考题9-2

带权中位数-算法导论第三版第九章思考题9-2 b 时间复杂度O(nlgn) float find_median_with_weights_b(float *array,int length) {quick_sort<float>(array,0,length-1);float sum 0;int index 0;while (sum < 0.5){sum array[index];index;}return array[index-1…

SQL Server 分页+json分享

1。SQL Server 版本2012 新增SQL分页的写法最近封装一个轻量级的ORM用到了分页&#xff0c;以前只知道使用Row_Number函数&#xff0c;现在发现sqlserver 新增的 {orderBy} offset {start} rows fetch next {pageSize} rows only 也挺好用的。简单回顾下 sqlserver 各个版本支持…

二叉树的存储结构及四种遍历(C语言)

二叉树的存储结构: typedef struct TNode *Position; typedef Position BinTree; /* 二叉树类型 */ struct TNode{ /* 树结点定义 */ElementType Data; /* 结点数据 */BinTree Left; /* 指向左子树 */BinTree Right; /* 指向右子树 */ };二叉树的四种遍历: void Inord…

Stack(栈 c++模版实现)

Stack(栈 c模版实现&#xff09; // // Created by XXX on 2021/7/10. //#ifndef C11LEARN_STACK_H #define C11LEARN_STACK_H template<typename T> class Stack{ private:int capacity;int top;T *array; public:Stack(int capacity 50);Stack(const Stack<T>&…

用十行代码快速创建权限管理系统

&#xff08;坚持做自己&#xff09;为了防止说是标题党&#xff0c;我先展示下真是就需要十行代码&#xff1a;当然还有appsettings.json配置文件&#xff0c;和种子数据文件&#xff0c;这个不算代码之内。1、项目背景介绍Blog.Core项目开源也两年了&#xff0c;经过了很多许…

编程中的一种特殊递归-尾递归

尾递归&#xff1a;在程序要返回的地方出现递归 从编译的角度来讲&#xff0c;尾递归都可以用循环来实现。 例子&#xff1a;二叉搜索树的查找操作Find递归函数可以写成循环的方式实现

Queue(队列 C++模版实现)

Queue&#xff08;队列 C模版实现&#xff09; #ifndef C11LEARN_QUEUE_H #define C11LEARN_QUEUE_H template<typename T> class Queue { private:int tail;int head;int capacity;T *array; public:Queue(int capacity 50);Queue(const Queue<T> &queue);Q…