(C/C++) Link List - C++ 版本
利用C++寫一個基本的 Link list 練習,功能包含 pint list、CreatList、Insert、Delete、Reverse、Search、Clear、GetLen。
先建立相關的Class ListNode、LinkedList
class LinkedList; // 需要先宣告
class ListNode{
public:
int data;
ListNode *next;
public:
ListNode():data(), next(){};
ListNode(int a):data(a), next(){};
friend class LinkedList;
}; class LinkedList{
public:
// int size; // size是用來記錄Linked list的長度, 非必要
ListNode *first;
public:
LinkedList() :first(){};
void PrintList();
void CreateList(int *arr, int len);
void Push_front(int x);
void Push_back(int x);
void Delete(int x);
void Clear();
void Reverse();
int ListLen();
ListNode * ListSearch(int x);
void Insert(int x, int data);
};
首先是Creat List,給一個arry利用Link list串在一起。
void LinkedList::CreateList(int *arr, int len)
{
if (arr == NULL || len == )
return; ListNode * previous = new ListNode();
for (int i = ; i < len; i++)
{
ListNode * newNode = new ListNode();
newNode->data = arr[i];
if (i == )
first = newNode;
else
previous->next = newNode;
newNode->next = NULL;
previous = newNode;
}
}
PrintList : 印出所有的 List
void LinkedList::PrintList()
{
if (first == )
{
cout << "List is empty.\n";
return;
} ListNode * current = first;
while (current != )
{
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
Insert List : 把新的node插在x之後
void LinkedList::Insert(int x, int data)
{
ListNode * current = first;
ListNode * previous = new ListNode();
ListNode * newNode = new ListNode();
while (current)
{
if (current->data == x)
{
newNode->data = data;
previous->next = newNode;
newNode->next = current;
break;
}
else
{
previous = current;
current = current->next;
}
} }
Delete : 刪除特定 node
void LinkedList::Delete(int x)
{
ListNode * current = first, *previous = ; while (current != && current->data != x) {
previous = current;
current = current->next;
} if (current == ) {
std::cout << "There is no " << x << " in list.\n";
}
else if (current == first) {
first = current->next;
delete current;
current = ;
}
else {
previous->next = current->next;
delete current;
current = ;
}
}
Reverse : 反轉 list
void LinkedList::Reverse()
{
if (first == || first->next == )
return; ListNode *previous = , *current = first, *preceding = first->next; while (preceding != )
{
current->next = previous;
previous = current;
current = preceding;
preceding = preceding->next;
}
current->next = previous;
first = current;
}
Search node : 尋找特定node
ListNode * LinkedList::ListSearch(int x)
{
if (first == )
{
printf("List is empty\n");
return NULL;
}
ListNode * newNode = first;
while (newNode)
{
if (newNode->data != x)
newNode = newNode->next;
else
return newNode;
}
printf("not found Node!!\n");
return NULL;
}
ListLen : 回傳List長度
int LinkedList::ListLen()
{
if (first == ) return NULL;
int index = ;
ListNode * newNode = first;
while (newNode)
{
newNode = newNode->next;
index++;
}
return index;
}
Push_front/back : 插入節點在頭/尾
void LinkedList::Push_front(int x)
{
ListNode *newNode = new ListNode(x);
newNode->next = first;
first = newNode;
} void LinkedList::Push_back(int x)
{
ListNode * newNode = new ListNode(x);
if (first == )
{
first = newNode;
return;
}
ListNode * current = first;
while (current->next != )
{
current = current->next;
}
current->next = newNode;
}
(C/C++) Link List - C++ 版本的更多相关文章
- 如何在 OSX 中使用多个JDK版本
升级macbook小白的硬盘成SSD后,重新安装了系统和JDK8,但是启动eclipse还是报告需要安装JDK6,于是也按照提示安装了Apple JDK6,这导致系统中有两个JDK,一个是Oracle ...
- react-router-dom Link search 传参
<Link> 和之前版本没太大区别,重点看下组件属性: to(string/object):要跳转的路径或地址: replace(bool):为 true 时,点击链接后将使用新地址替换掉 ...
- mac下通过brew切换php版本
第一步,先安装 brew Brew 是 Mac 下面的包管理工具,通过 Github 托管适合 Mac 的编译配置以及 Patch,可以方便的安装开发工具. Mac 自带ruby 所以安装起来很 ...
- DevExpress 隐藏Ribbon中barbuttonItem的SuperTip(1)
public frmMain() { InitializeComponent(); ribbonControl1.Manager.HighlightedLinkChanged += Manager_H ...
- visual.studio.15.preview5 编译器
前段时间微软更新了新版开发工具visual studio 15 preview5,安装后连文件结构目录都变了,想提取编译器还找不到. 不是原来的VC\BIN目录,已迁移到IDE\MSVC\14.10. ...
- Visual Assist的破解与安装
转载[PYG成员作品] [2016-09-26更新]Visual Assist X10.9.2112-Cracked.By.PiaoYun/P.Y.G 近期的一个稳定版本的破解方式: VA原版, VA ...
- React Router 4.x 开发,这些雷区我们都帮你踩过了
前言 在前端框架层出不穷的今天,React 以其虚拟 DOM .组件化开发思想等特性迅速占据了主流位置,成为前端开发工程师热衷的 Javascript 库.作为 React 体系中的重要组成部分:Re ...
- Appium1.9 之 Chromedriver安装方式
1.在 appium 官网上下载安装后,下载的是1.7.1的版本,安装之后是1.9.1最新版本. 2.appium安装之后,会发现涉及到 浏览器相关的业务时(我使用的是chrome)会提示 “No C ...
- [Web 前端] React Router v4 入坑指南
cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...
随机推荐
- log4j配置文件的手动加载与配置初始化
一. 本地项目: 初始化log4j的日志配置,指定到src目录下(建议用2) //1. 本地项目-属性文件配置 PropertyConfigurator.configu ...
- 对于现代开发来说,JavaScript就是一种垃圾语言(转)
伯乐在线导读:昨天 Reddit/Programming 频道的头条热帖是一篇来自 julik live 博主的技术吐槽文,最初的英文标题是"For modern development J ...
- elasticsearch配置小记(转)
原文 http://bigbo.github.io/pages/2015/04/10/elasticsearch_config/ 基于 elasticsearch 1.4.4 版本.安装方式为RPM ...
- Linux Java Meven环境变量设置
linux中的环境变量设置,可以在两个地方设置.他们分别是: /etc/profile 在这个文件下设置表示全局的,所有用户有效. 用户工作目录,用 ls -a查看,可以看到有一个.bash_pro ...
- Junit问题01 利用 @Autowired 注入失效问题
1 利用 @Autowired 注入失效问题 1.1 问题描述 在使用Junit作为测试框架的单元测试中,直接了用@Autowired记性依赖注入时总是注入失败 1.2 问题原因 在测试类中没有设定上 ...
- JSTL标签之core标签的使用
参考:http://blog.csdn.net/qq_25827845/article/details/53311722 核心标签库的导入 <%@ taglib prefix="c&q ...
- Asp.Net程序目录下文件夹或文件操作导致Session失效的解决方案
1.配置web.config <system.web> <sessionState mode="StateServer" stateConnectionStrin ...
- Laravel Gate 授权方式的使用指南
参考链接:An Introduction to Laravel Authorization Gates 本文使用 Laravel 的 Gate 授权方式 实现一个基于用户角色的博客发布系统. 在系统包 ...
- css总结16:HTML5 多媒体音频(Audio)视频(video )
1 显示嵌入网页中的 MP3 文件: <embed height="50" width="100" src="horse.mp3"&g ...
- vs2012 许可 tfs 许可
Team Foundation Server 2012序列号或MSDN版本 BVGTF-T7MVR-TP46H-9Q97G-XBXRB VS2012注册码 亲测成功.我的是旗舰版... YKCW6-B ...