热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

单链表的高效遍历及性能优化策略

本文探讨了单链表的高效遍历方法及其性能优化策略。在单链表的数据结构中,插入操作的时间复杂度为O(n),而遍历操作的时间复杂度为O(n^2)。通过在`LinkList.h`和`main.cpp`文件中对单链表进行封装,我们实现了创建和销毁功能的优化,提高了单链表的使用效率。此外,文章还介绍了几种常见的优化技术,如缓存节点指针和批量处理,以进一步提升遍历性能。

如何遍历单链表中的每个数据元素?


LinkList<int> list;
for(int i=0; i<5; i++)
{
list.insert(
0,i);
}
for(int i=0; i)
{
cout
<get(i) << endl;
}

插入操作中,时间复杂度为O(n)

遍历操作中,时间复杂度为O(n*n)

技术分享图片

 技术分享图片

 技术分享图片

 技术分享图片

 LinkList.h


#ifndef LINKLIST
#define LINKLIST
#include
"List.h"
#include
"Object.h"
#include
"Exception.h"
namespace DTLib
{
template

class LinkList : public List
{
protected:
struct Node : public Object
{
T value;
Node
* next;
};
// mutable Node m_header;//加上mutable就允许在const成员函数中取m_header的地址了
/*这个类型的定义仅仅是为了头结点,非常巧妙。*/
mutable
struct : public Object
{
char reserved[sizeof(T)];
Node
* next;
}m_header;
int m_length;
int m_step;
Node
*
m_current;
Node
* position(int i) const
{
Node
* ret = reinterpret_cast(&m_header);
for(int p=0; p)
{
ret
= ret->next;
}
return ret;
}
public:
LinkList()
{
m_header.next
= NULL;
m_length
= 0;
m_step
= 1;
m_current
= NULL;
}
bool insert(const T& e) //往线性表的尾部插入一个元素,因此将i省略掉。
{
return insert(m_length,e);
}
bool insert(int i, const T& e)
{
bool ret = ((0 <=i) && (i <= m_length) );
if(ret)
{
Node
* node = new Node();
if(node != NULL)
{
Node
* current = position(i);
node
->value = e;
node
->next = current->next;
current
->next = node;
m_length
++;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException,
"no memory to insert new element...");
}
}
}
bool remove(int i)
{
bool ret = ((0<=i) && (i<m_length));
if(ret)
{
Node
* current = position(i);
Node
* toDel = current->next;
toDel
->next = current->next;
delete toDel;
m_length
--;
}
return ret;
}
bool set(int i, const T& e)
{
bool ret = ((0<=i) && (i<m_length));
if(ret)
{
Node
* current= position(i);
current
->next->value = e;
}
return ret;
}
T
get(int i) const
{
T ret;
if(get(i,ret))
{
return ret;
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException,
"invalid parameter i get element...");
}
return ret;
}
bool get(int i, T& e) const
{
bool ret = ((0<=i) && (i<m_length));
if(ret)
{
Node
* current = position(i);
e
= current->next->value;
}
return ret;
}
int find(const T& e)const
{
int ret = -1;
int i=0;
Node
* node = m_header.next;
while(node)
{
if(node->value == e)
{
ret
= i;
break;
}
else
{
node
= node->next;
i
++;
}
}
return ret;
}
bool move(int i, int step=1)
{
bool ret = (0 <= i) && (i 0);
if(ret)
{
m_current
= position(i)->next;
m_step
= step;
}
return ret;
}
bool end() //用来判断当前的遍历是否结束
{
return (m_current == NULL);
}
T current()
//返回当前游标所指向的节点数据元素的值
{
if(!end())
{
return m_current->value;
}
else
{
THROW_EXCEPTION(InvalidOperationException,
"No value at current position...");
}
}
bool next() //用于移动游标
{
int i = 0;
while((i end() ))
{
m_current = m_current->next;
i
++;
}
return (i == m_step);
}
int length() const
{
return m_length;
}
void clear()
{
while(m_header.next)
{
Node
* toDel = m_header.next;
m_header.next
= toDel->next;
delete toDel;
}
m_length
= 0;
}
~LinkList()
{
clear();
}
};
}
#endif // LINKLIST

main.cpp


#include
#include

#include
"LinkList.h"
using namespace std;
using namespace DTLib;
int main()
{
LinkList
<int> list;
for(int i=0; i<5; i++)
{
list.insert(
0,i);
}
/*将游标移动到第0个节点所在的位置list.move(0)
list.end()判断当前游标是否已达到末尾;
list.next()移动游标
意义就是先将指针指向第0个数据元素,打印第0个数据元素的值;
然后移动一次,移动一次后再来获取数据元素的值...时间复杂度就是O(n)
*/
for(list.move(0); !list.end(); list.next())
{
cout
< endl;
}
//可以只遍历下标为偶数的数据元素
for(list.move(0,2); !list.end(); list.next())
{
cout
< endl;
}
return 0;
}

 

单链表内部的一次封装


virtual Node* create()
{
return new Node();
}
virtual void destory(Node* pn)
{
delete pn;
}

封装create和destory这两个函数有什么意义呢?

技术分享图片

 


推荐阅读
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 本文介绍如何使用 NSTimer 实现倒计时功能,详细讲解了初始化方法、参数配置以及具体实现步骤。通过示例代码展示如何创建和管理定时器,确保在指定时间间隔内执行特定任务。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 数据库内核开发入门 | 搭建研发环境的初步指南
    本课程将带你从零开始,逐步掌握数据库内核开发的基础知识和实践技能,重点介绍如何搭建OceanBase的开发环境。 ... [详细]
  • MQTT技术周报:硬件连接与协议解析
    本周开发笔记重点介绍了在新项目中使用MQTT协议进行硬件连接的技术细节,涵盖其特性、原理及实现步骤。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 本文介绍了如何在C#中启动一个应用程序,并通过枚举窗口来获取其主窗口句柄。当使用Process类启动程序时,我们通常只能获得进程的句柄,而主窗口句柄可能为0。因此,我们需要使用API函数和回调机制来准确获取主窗口句柄。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
author-avatar
欣仪威侑扬芸_782
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有