相关内容:

线性表的顺序实现

链式实现(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++)的更多相关文章

  1. C++线性表的链式存储结构

    C++实现线性表的链式存储结构: 为了解决顺序存储不足:用线性表另外一种结构-链式存储.在顺序存储结构(数组描述)中,元素的地址是由数学公式决定的,而在链式储存结构中,元素的地址是随机分布的,每个元素 ...

  2. C语言 线性表 双向链式结构 实现

    一个双向链式结构实现的线性表 duList (GCC编译). /** * @brief 线性表双向链表结构 * @author wid * @date 2013-10-28 * * @note 若代码 ...

  3. C++编程练习(2)----“实现简单的线性表的链式存储结构“

    单链表采用链式存储结构,用一组任意的存储单元存放线性表的元素. 对于查找操作,单链表的时间复杂度为O(n). 对于插入和删除操作,单链表在确定位置后,插入和删除时间仅为O(1). 单链表不需要分配存储 ...

  4. C 线性表的链式存储实现及插入、删除等操作示例

    一.链式存储的优势 线性表的存储可以通过顺序存储或链式存储实现,其中顺序存储基于数组实现(见本人上一篇博客),在进行插入删除等操作时,需对表内某一部分元素逐个移动,效率较低.而链式结构不依赖于地址连续 ...

  5. 数据结构-线性表的链式存储相关算法(C语言实现)

    链表的简单介绍 为什么需要线性链表 当然是为了克服顺序表的缺点,在顺序表中,做插入和删除操作时,需要大量的移动元素,导致效率下降. 线性链表的分类 按照链接方式: 按照实现角度: 线性链表的创建和简单 ...

  6. 数据结构算法C语言实现(二)---2.3线性表的链式表示和实现之单链表

    一.简述 [暂无] 二.头文件 #ifndef _2_3_part1_H_ #define _2_3_part1_H_ //2_3_part1.h /** author:zhaoyu email:zh ...

  7. 线性表 顺序存储 链式存储 ---java实现

    首先抽象出一个线性表抽象类(包括主要的增删操作) public abstract class MyAbstractList<E> { public abstract void add(E ...

  8. 线性表的链式存储——C语言实现

    SeqList.h #ifndef _WBM_LIST_H_ #define _WBM_LIST_H_ typedef void List; typedef void ListNode; //创建并且 ...

  9. 线性表的链式存储C语言版

    #include <stdio.h> #include <malloc.h> #define N 10 typedef struct Node { int data; stru ...

随机推荐

  1. vue-router 页面切换后保持在页面顶部而不是保持原先的滚动位置的办法

    vue-router有提供一个方法scrollBehavior,它可以使切换到新路由时,想要页面滚到顶部,或者是保持原先的滚动位置,就像重新加载页面那样. 这个功能只在 HTML5 history 模 ...

  2. [agc014d] Black and White Tree(玄学树D)

    Description 有一颗n个点的树,刚开始每个点都没有颜色. Alice和Bob会轮流对这棵树的一个点涂色,Alice涂白,Bob涂黑,Alice先手. 若最后存在一个白点,使得这个白点所有相邻 ...

  3. 全球主要城市经纬度api

    原文发布时间为:2011-06-23 -- 来源于本人的百度文章 [由搬家工具导入] http://www.google.com/ig/cities?country=cn http://www.goo ...

  4. DataSet导出Excel,比以往的方法导出的Excel外观更加好看

    原文发布时间为:2010-06-21 -- 来源于本人的百度文章 [由搬家工具导入] ======目前方法=========== #region 生成Excel/// <summary>/ ...

  5. DataSet导出到Excel文件

    public static void ExportToExcel(DataSet source, string fileName) { System.IO.StreamWriter excelDoc ...

  6. python --- comment

    single line comment : multiple line comment :

  7. cisco packet 实验教程(一)

    01. 开篇:组建小型局域网 实验任务 1.利用一台型号为2960的交换机将2pc机互连组建一个小型局域网: 2.分别设置pc机的ip地址: 3.验证pc机间可以互通. 实验设备 Switch_296 ...

  8. linux 常用命令: runuser

    rpm: coreutils-8.4-9.el6.x86_64 runuser --help 用法:runuser [选项]... [-] [用户 [参数]... ] Change the effec ...

  9. AC日记——[HAOI2007]理想的正方形 P2216

    [HAOI2007] 理想的正方形 思路: 正解多个单调队列: 但是我用树套树水了过去: 来,上代码: #include <cstdio> #include <cstring> ...

  10. (9)centos下防火墙firewalld设置

    学习apache安装的时候需要打开80端口,由于centos 7版本以后默认使用firewalld后,网上关于iptables的设置方法已经不管用了,想着反正iptable也不会用,索性直接搬官方文档 ...