(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的架子 ...
随机推荐
- CentOS Firewall简单使用
启动 systemctl start firewalld 停止 systemctl stop firewalld 获取 firewalld 状态 firewall-cmd --state 在不改变状态 ...
- wince驱动开发入门
因为课题前期调研没做好,用的CPU板卡和数据采集卡来自两个部门.加上买的是裸板,自己定制的OS,技术支持不爱搭理.所以给的AI板卡的驱动一直装不上,自己在郁闷中寻找答案,就扎进了wince驱动的知识库 ...
- 24-从零玩转JavaWeb-包装类、自动装箱、自动拆箱
一.什么是包装类 二.对基本数据类型包装的好处 三.装箱操作 四.拆箱操作 五.自动装箱 六.自动拆箱 七.字符串与基本数据类型和包装类的转换 八.包装类的缓存设计
- ORA-01940:无法删除当前已链接的用户
(1)查看用户的连接状况 select user name, sid, serial# from v$session; (2)找到要删除用户的sid,和serial,并删除 例如要删除用户nc633t ...
- C#获取访问者ip和获取本机ip地址
获取访问者ip: string userIP; // HttpRequest Request = HttpContext.Current.Request; HttpRequest Request = ...
- java基础之JDBC八:Druid连接池的使用
基本使用代码: /** * Druid连接池及简单工具类的使用 */ public class Test{ public static void main(String[] args) { Conne ...
- c++ 多态问题(在虚函数里调用虚函数)
最近在看cocos2d-x的源码,非常感激cocos2d作者的开源精神.在看代码的过程中感觉两个方向让我受益,1.把之前从书中看到的c++知识,明白了怎么运用.2.学习作者驾驭代码的巧妙方法. 看co ...
- Mybatis中resultType和resultMap
一.概述MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部Res ...
- SparkR 读取数据& Spark运行的配置
1.本地LOCAL环境安装Spark并试运行配置(在Ubuntu系统下例子) # 打开文件配置环境变量: JAVA,SCALA,SPARK,HADOOP,SBT gedit /etc/profile ...
- ESP8266-iot-简介1
ESP8266简介