(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的架子 ...
随机推荐
- c#winform图片绘制与图片验证码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- idea将项目导出为war包
idea 那么好用,早就把eclipse抛弃了.不过每次都是在给项目发包的时候,不得不重新打开eclipse导出为war包.感觉自己蠢蠢的.上网查了一下教程,按照网上的教程设置好了之后,运行项目发现并 ...
- 利用Sphinx编写文档
利用Sphinx编写文档 1.Sphinx简介和使用理由 ================= Sphinx是一个用Python语言编写而成的文档编写工具.用Sphinx编写文档的时候,用户只需要编写符 ...
- Spring总结三:DI(依赖注入)
简介: 所谓的依赖注入,其实是当一个bean实例引用到了另外一个bean实例时spring容器帮助我们创建依赖bean实例并注入(传递)到另一个bean中,比如你使用Spring容器创建的对象A里面需 ...
- ionic中的后退方法
1)$ionicHistory.goBack(); 2)$ionicNavBarDelegate.back(); 个人感觉: 1)$ionicHistory.goBack()会按照html历史来后退 ...
- Python:cmd传参
假如你写了一个文件test.py,你需要三个参数,你运行时: python test.py arg1 arg2 arg3 在test.py中读取这几个参数: import sys print 'Num ...
- libevent源码深度剖析三
libevent源码深度剖析三 ——libevent基本使用场景和事件流程 张亮 1 前言 学习源代码该从哪里入手?我觉得从程序的基本使用场景和代码的整体处理流程入手是个不错的方法,至少从个人的经验上 ...
- laravel数据迁移(创建错误列不能创建)
创建数据表的命令 php artisan make:migration create_users_table 执行这个迁移的命令, php artisan migrate 其实感觉就像简单的方法创建数 ...
- tomcat在linux服务器上部署应用
连接服务器 服务器地址:xxx.xxx.xxx.xxx 用户名:xxxx 密码:xxxx 进入到服务器中的tomcat路径,关闭服务器,例如 路径:/opt/wzgcyth/apache-tomcat ...
- unity3d 为什么要烘焙?烘焙作用是为了什么?
可以这样理解.你把物体模型放进了场景里之后, 引擎会计算光线,光线照到你的物体的表面形成反光和阴影. 如果不烘焙, 游戏运行的时候,这些反光和阴影都是由显卡和CPU计算出来的.你烘焙之后,这些反光和阴 ...