线性表的链式实现(C++)
相关内容:
(推荐在GitHub上查看,下面代码只是最初实现,后续如果有修改,由于太麻烦,不会再更改下文,仅仅更新Github上的代码文件)
结点以及链表类的定义:
#define ElemType int
class LNode {
public:
LNode(ElemType ele, LNode *pointer) :data(ele), next(pointer) {
}
LNode(ElemType ele) {
data = ele;
next = nullptr;
}
LNode() {
data = ;
next = nullptr;
}
ElemType data;
LNode *next;
};
class LinkList {
public:
LinkList() {
head = nullptr;
}
// 初始化时,创建一个长度为len的链表
LinkList(int len) {
head = nullptr;
this->LListAddNodes(len);
}
~LinkList() {
this->LListDestory();
}
LNode *LListHead() {
return head;
}
// 设置链表头结点
void LListSetHead(LNode *node) {
head = node;
}
// 获取链表长度
int LListLength();
// 判断链表是否为空
bool LListEmpty() {
return(head == nullptr) ? true : false;
}
// 返回链表中第pos个结点
bool GetNode(int pos, LNode **node);
// 返回指定data在链表中第一次出现的位置
bool LocateNode(ElemType ele, LNode **node);
// 在指定位置插入后一个结点,但若pos为0是表示在链表头插入一个结点
bool LListInsert(int pos, LNode *node);
// 删除指定位置结点
bool LListDelete(int pos);
// 删除指定位置结点并返回被删除结点的信息
bool LListDelete(int pos, LNode *node);
// 遍历线性表
void LListTraverse();
// 在链表尾添加cnt个结点
bool LListAddNodes(int cnt);
// 销毁链表,释放所有结点资源
void LListDestory();
private:
LNode *head;
};
具体实现:
int LinkList::LListLength()
{
int cnt = ;
LNode *node = head; while (node != nullptr) {
cnt++;
node = node->next;
}
return cnt;
} bool LinkList::GetNode(int pos, LNode **node)
{
if (pos > this->LListLength() || pos < )
return false; LNode *cur = head; for (int i = ; i <= pos; i++) {
cur = cur->next;
} *node = cur; return true;
} bool LinkList::LocateNode(ElemType ele, LNode **node)
{
LNode *curNode = head;
while (curNode != nullptr) { if (curNode->data == ele) {
*node = curNode;
return true;
}
curNode = curNode->next;
} return false;
} bool LinkList::LListInsert(int pos, LNode *node)
{
// 插入位置错误
if (pos < || pos > this->LListLength())
return false; if (pos == ) {
node->next = head;
head = node;
}
else {
LNode *temp = nullptr;
this->GetNode(pos, &temp);
node->next = temp->next;
temp->next = node;
} return true;
} bool LinkList::LListDelete(int pos)
{
if (pos < || pos > this->LListLength())
return false; if (pos == ) { // 删除头结点
LNode *temp = head;
head = temp->next;
delete(temp);
}
else {
LNode *temp_1 = nullptr;
LNode *temp_2 = nullptr;
this->GetNode(pos - , &temp_1); // 获取被删除结点的前一个结点
temp_2 = temp_1->next;
temp_1->next = temp_2->next;
delete(temp_2);
} return true;
} // 删除结点立即释放结点所占用的空间,被返回的仅仅是被删除结点的信息
bool LinkList::LListDelete(int pos, LNode *node)
{
if (pos < || pos > this->LListLength())
return false; if (pos == ) {
LNode *temp = head;
head = temp->next;
node->data = temp->data;
node->next = temp->next;
delete(temp);
}
else {
LNode *temp_1 = nullptr;
LNode *temp_2 = nullptr;
this->GetNode(pos - , &temp_1);
temp_2 = temp_1->next;
temp_1->next = temp_2->next;
node->data = temp_2->data;
node->next = temp_2->next;
delete(temp_2);
} return true;
} void LinkList::LListTraverse()
{
LNode *curNode = head; while (curNode != nullptr) {
std::cout << curNode->data << std::endl;
curNode = curNode->next;
}
} bool LinkList::LListAddNodes(int cnt)
{
//std::cout << cnt << std::endl; if (cnt < )
return false;
else if (cnt == )
return true; LNode *curNode = head; if (curNode != nullptr) {
// 找到链表尾结点
while (curNode->next != nullptr) {
curNode = curNode->next;
} for (int i = ; i < cnt; i++) {
int temp;
std::cin >> temp;
LNode *node = new LNode(temp); if (!node) {
return false;
} //node->next = nullptr;
//node->data = i; curNode->next = node;
curNode = node;
}
}
else {
int temp;
std::cin >> temp;
LNode *node = new LNode(temp);
head = node;
curNode = node; for (int i = ; i < cnt - ; i++) {
std::cin >> temp;
node = new LNode(temp); if (!node) {
//std::cout << "new Error!" << std::endl;
return false;
} curNode->next = node;
curNode = node;
}
} return true;
} void LinkList::LListDestory()
{
//std::cout << "Destory!" << endl;
LNode *cur = head; if (!cur)
return; LNode *next = cur->next; while (cur) {
delete cur; if (next) {
cur = next;
next = next->next;
}
else
break;
}
}
用于测试的代码及测试结果:
#include "link_list.h" int main()
{
//LNode node(5);
//if (node.next == nullptr)
//std::cout << "next = nullptr, data = " << node.data << std::endl; LinkList list();
//LinkList list; int len = list.LListLength();
std::cout << "InitLen = " << len << std::endl; if (list.LListEmpty())
std::cout << "Empty List!" << std::endl; std::cout << std::endl << "First Traverse: " << std::endl; list.LListTraverse(); std::cout << std::endl << "Insert two Node!" << std::endl; LNode *node = new LNode(); if (!node)
std::cout << "new Error!" << std::endl; LNode *node_2 = new LNode(); if (!node_2)
std::cout << "new Error!" << std::endl; list.LListInsert(, node);
list.LListInsert(, node_2); len = list.LListLength();
std::cout << "CurLen = " << len << std::endl; std::cout << std::endl << "Second Traverse: " << std::endl;
list.LListTraverse(); std::cout << "Get Node's Data!" << std::endl;
LNode *node_pos_3 = nullptr;
list.GetNode(, &node_pos_3); std::cout << "node_pos_3: " << "data = " << node_pos_3->data << std::endl; LNode node_del;
if (list.LListDelete(, &node_del)) {
std::cout << "Delete Node: " << "data = " << node_del.data << std::endl;
} len = list.LListLength();
std::cout << "CurLen = " << len << std::endl; std::cout << std::endl << "Third Traverse: " << std::endl;
list.LListTraverse(); std::cout << "End..." << std::endl;
int temp;
std::cin >> temp; return ;
} /*
输出信息:
1
2
3
4
5
InitLen = 5 First Traverse:
1
2
3
4
5 Insert two Node!
CurLen = 7 Second Traverse:
0
1
2
3
4
5
6
Get Node's Data!
node_pos_3: data = 2
Delete Node: data = 2
CurLen = 6 Third Traverse:
0
1
3
4
5
6
End...
*/
线性表的链式实现(C++)的更多相关文章
- C++线性表的链式存储结构
C++实现线性表的链式存储结构: 为了解决顺序存储不足:用线性表另外一种结构-链式存储.在顺序存储结构(数组描述)中,元素的地址是由数学公式决定的,而在链式储存结构中,元素的地址是随机分布的,每个元素 ...
- C语言 线性表 双向链式结构 实现
一个双向链式结构实现的线性表 duList (GCC编译). /** * @brief 线性表双向链表结构 * @author wid * @date 2013-10-28 * * @note 若代码 ...
- C++编程练习(2)----“实现简单的线性表的链式存储结构“
单链表采用链式存储结构,用一组任意的存储单元存放线性表的元素. 对于查找操作,单链表的时间复杂度为O(n). 对于插入和删除操作,单链表在确定位置后,插入和删除时间仅为O(1). 单链表不需要分配存储 ...
- C 线性表的链式存储实现及插入、删除等操作示例
一.链式存储的优势 线性表的存储可以通过顺序存储或链式存储实现,其中顺序存储基于数组实现(见本人上一篇博客),在进行插入删除等操作时,需对表内某一部分元素逐个移动,效率较低.而链式结构不依赖于地址连续 ...
- 数据结构-线性表的链式存储相关算法(C语言实现)
链表的简单介绍 为什么需要线性链表 当然是为了克服顺序表的缺点,在顺序表中,做插入和删除操作时,需要大量的移动元素,导致效率下降. 线性链表的分类 按照链接方式: 按照实现角度: 线性链表的创建和简单 ...
- 数据结构算法C语言实现(二)---2.3线性表的链式表示和实现之单链表
一.简述 [暂无] 二.头文件 #ifndef _2_3_part1_H_ #define _2_3_part1_H_ //2_3_part1.h /** author:zhaoyu email:zh ...
- 线性表 顺序存储 链式存储 ---java实现
首先抽象出一个线性表抽象类(包括主要的增删操作) public abstract class MyAbstractList<E> { public abstract void add(E ...
- 线性表的链式存储——C语言实现
SeqList.h #ifndef _WBM_LIST_H_ #define _WBM_LIST_H_ typedef void List; typedef void ListNode; //创建并且 ...
- 线性表的链式存储C语言版
#include <stdio.h> #include <malloc.h> #define N 10 typedef struct Node { int data; stru ...
随机推荐
- [AGC004E] Salvage Robots (DP)
Description 蛤蟆国的领土我们可以抽象为H*W的笼子,在这片蛤土上,有若干个机器人和一个出口,其余都是空地,每次蛤蟆会要求让所有的机器人向某个方向移动一步,当机器人移动到出口时会被蛤蟆活摘出 ...
- linux之tr
通过使用 tr,您可以非常容易地实现 sed 的许多最基本功能.您可以将 tr 看作为 sed 的(极其)简化的变体:它可以用一个字符来替换另一个字符,或者可以完全除去一些字符.您也可以用它来除去重复 ...
- Ant自动打包
在ant的官网http://ant.apache.org进行下载后apache-ant-1.8.2包 解压(存放的路径不要有中文字符) 把ant里的lib设置到环境变量:E:\Android\apac ...
- 基于 Intraweb 和 JQuery 的开发套件
基于 Intraweb 和 JQuery 的开发套件 http://www.cgdevtools.com/ 开发速度无敌,界面也非常美. 我的web短板终于解决了.....!!!!..!!! 做一个小 ...
- 使用p6spy格式化日志输出
P6Spy 是针对数据库访问操作的动态监测框架(为开源项目,项目首 页:www.p6spy.com)它使得数据库数据可无缝截取和操纵,而不必对现有应用程序的代码作任何修改.P6Spy 分发包包括P6L ...
- centos7下mysql双主+keepalived
一.keepalived简介 keepalived是vrrp协议的实现,原生设计目的是为了高可用ipvs服务,keepalived能够配置文件中的定义生成ipvs规则,并能够对各RS的健康状态进行检测 ...
- 在本地(自己电脑上)部署了tomcat服务器,真机测试遇到的问题
开始的时候自己就是给app搭建了一个小的框架,只有一个界面发送了网络请求,部署的tomcat,数据成功请求,得到了数据. 后来随着联网请求的增多,突然发现联网请求一直失败.自己dubug了最开始的第一 ...
- 前端js、jQuery实现日期格式化、字符串格式化
1. js仿后台的字符串的StringFormat方法 在做前端页面时候,经常会对字符串进行拼接处理,但是直接使用字符串拼接,不但影响阅读,而且影响执行效率,且jQuery有没有定义字符串的Strin ...
- Cryptography I 学习笔记 --- 基于陷门置换的公钥加密
RSA算法的工作流程 1. 生成公钥私钥 生成两个素数p和q,计算n=p*q,计算φ(n)=n-p-q+1,然后生成e与d,使 e * d = 1 mod φ(n). 然后以(n, e)作为公钥,(n ...
- FZU 2125 简单的等式 【数学/枚举解方程式】
现在有一个等式如下:x^2+s(x,m)x-n=0.其中s(x,m)表示把x写成m进制时,每个位数相加的和.现在,在给定n,m的情况下,求出满足等式的最小的正整数x.如果不存在,请输出-1. Inpu ...