算法复习--------------基本算法:静态链表的实现

2024-06-13 07:18

本文主要是介绍算法复习--------------基本算法:静态链表的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

静态链表也叫模拟指针,采用一个节点数组以及对该数组进行索引的模拟指针来实现的

该数组中的每个元素都包含两个域,data和link,具体实现图跟链表差不多,就不给出图了,下面给出具体的实现代码


1.ClassSnode.h

#ifndef __imNode
#define __imNodetemplate<class T> class SimSpace;
template<class T> class SimChain;
template<class T>
class SimNode{friend SimSpace<T>;friend SimChain<T>;
private:T m_Data;int link; 
};#endif

2.ClassSimSpace.h

#ifndef SimSpace__
#define SimSpace__
#include<stdlib.h>
#include<iostream>
#include"ClassSnode.h"template<class T> class SimChain;template<class T>
class SimSpace{
private:int NumberOfNode, first;SimNode<T> *node; //节点数组friend SimChain<T>;
public:SimSpace(int MaxSpaceSize = 100);~SimSpace();int Allocate();void Deallocate(int &i);//删除节点 i};template<class T>
SimSpace<T>::~SimSpace(){}template<class T>
SimSpace<T>::SimSpace(int MaxSpaceSize){NumberOfNode = MaxSpaceSize;node = new SimNode<T>[NumberOfNode];//初始化空间表节点for (int i = 0; i < NumberOfNode; i++){node[i].link = i + 1;}node[NumberOfNode].link = -1;first = 0;
}template<class T>
int SimSpace<T>::Allocate(){//分配一个自由节点if (first == -1)throw;int i = first;first = node[i].link;return i;
}template<class T>
void SimSpace<T>::Deallocate(int &i){node[i].link = first;first = i;i = -1;
}
#endif



3.ClassSimChain.h

#ifndef SimChain__
#define SimChain__#include"ClassSimSpace.h"
#include<stdlib.h>
#include<iostream>using namespace std;
template<class T>
class SimChain{
private:int first;static SimSpace<T> s;
public:SimChain(){ first == -1; }~SimChain(){ Destory(); }void Destory(); //清除所有链表数据int Length()const;bool Find(int k, T& x)const;int Search(const T& x)const;SimChain<T>& Datele(int k, T& x);SimChain<T>& Insert(int l, const T& x);void OutPut(ostream& out)const;};template<class T>
void SimChain<T>::Destory(){//释放所有的链表节点int next;while (first != -1){next = s.node[first].link;s.Deallocate(first);first = next;}
}template<class T>
int SimChain<T>::Length()const{//返回链表中节点的数量int current = first;int len = 0;while (current != -1){current = s.node[current].link;len++;}return len;
}template<class T>
bool SimChain<T>::Find(int k, T& x)const{if (k < 1)return false;int current = first,index = 1;while (index < k && current != -1){current = s.node[current].link;index++;}if (current == -1){return false;}x = s.node[current].m_Data;return true;
}template<class T>
int SimChain<T>::Search(const T& x)const{int current = first,index = 1;while (current != -1 &&s.node[current].m_Data != x){current = s.node[current].link;index++;}return ((current > 0) ? index : 0);
}template<class T>
SimChain<T>& SimChain<T>::Datele(int k, T& x){if (k < 1 || first == -1) throw;int p = first;if (k == 1)first = s.node[first].link;else{int q = first;for (int index = 1; index < k - 1 && q != -1; index++){q = s.node[q].link;}if (q == -1 || s.node[q].link == -1){throw;}p = s.node[q].link;s.node[q].link = s.node[p].link;}x = s.node[p].m_Data;s.Deallocate(p);return *this;
}template<class T>
SimChain<T>& SimChain<T>::Insert(int k, const T& x){if (k < 0)throw;int p = first;for (int index = 1; index < k && p != -1; index++){p = s.node[p].link;}if (k > 0 && p == -1)throw;int y = s.Allocate();s.node[y].m_Data = x;if (k){s.node[y].link = s.node[p].link;s.node[p].link = y;}else{s.node[y].link = first;first = y;}return *this;
}template<class T>
void SimChain<T>::OutPut(ostream& out)const{for (int current = first; current != -1; current = s.node[current].link)out << s.node[current].m_Data << " ";
}template<class T>
ostream& operator<<(ostream& out, const SimChain<T>& x)
{x.OutPut(out);return out;
}#endif



4.具体使用

// Schain.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"
#include"ClassSimChain.h"
#include<iostream>
using namespace std;SimSpace<int> SimChain<int>::s;int _tmain(int argc, _TCHAR* argv[])
{int x;SimChain<int> c;cout << "Chain length is" << c.Length() << endl;c.Insert(0, 2).Insert(1, 6);cout << "Chain length is" << c.Length() << endl;c.Find(1, x);cout << "first element is " << x << endl;c.Datele(1, x);cout << "Deleted " << x << endl;cout << "New length is " << c.Length() << endl;cout << "the Position of 6 is" << c.Search(6) << endl;c.Insert(0, 9).Insert(1, 8).Insert(2, 7);cout << "current chain is " << c << endl;cout << "the length of chain is " << c.Length() << endl;getchar();return 0;
}



5.输出结果:


Chain length is0
Chain length is2
first element is 2
Deleted 2
New length is 1
the Position of 6 is1
current chain is 9 8 7 6
the length of chain is 4



这篇关于算法复习--------------基本算法:静态链表的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1056634

相关文章

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

康拓展开(hash算法中会用到)

康拓展开是一个全排列到一个自然数的双射(也就是某个全排列与某个自然数一一对应) 公式: X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0! 其中,a[i]为整数,并且0<=a[i]<i,1<=i<=n。(a[i]在不同应用中的含义不同); 典型应用: 计算当前排列在所有由小到大全排列中的顺序,也就是说求当前排列是第

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

基本知识点

1、c++的输入加上ios::sync_with_stdio(false);  等价于 c的输入,读取速度会加快(但是在字符串的题里面和容易出现问题) 2、lower_bound()和upper_bound() iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。 iterator upper_bou

综合安防管理平台LntonAIServer视频监控汇聚抖动检测算法优势

LntonAIServer视频质量诊断功能中的抖动检测是一个专门针对视频稳定性进行分析的功能。抖动通常是指视频帧之间的不必要运动,这种运动可能是由于摄像机的移动、传输中的错误或编解码问题导致的。抖动检测对于确保视频内容的平滑性和观看体验至关重要。 优势 1. 提高图像质量 - 清晰度提升:减少抖动,提高图像的清晰度和细节表现力,使得监控画面更加真实可信。 - 细节增强:在低光条件下,抖

【数据结构】——原来排序算法搞懂这些就行,轻松拿捏

前言:快速排序的实现最重要的是找基准值,下面让我们来了解如何实现找基准值 基准值的注释:在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。 在此我们采用三数取中法,也就是取左端、中间、右端三个数,然后进行排序,将中间数作为枢纽值。 快速排序实现主框架: //快速排序 void QuickSort(int* arr, int left, int rig

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

csu1329(双向链表)

题意:给n个盒子,编号为1到n,四个操作:1、将x盒子移到y的左边;2、将x盒子移到y的右边;3、交换x和y盒子的位置;4、将所有的盒子反过来放。 思路分析:用双向链表解决。每个操作的时间复杂度为O(1),用数组来模拟链表,下面的代码是参考刘老师的标程写的。 代码如下: #include<iostream>#include<algorithm>#include<stdio.h>#