(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的架子 ...
随机推荐
- jQuery.prop() 与attr()
1.attr()是jQuery 1.0版本就有的函数,prop()是jQuery 1.6版本新增的函数.毫无疑问,在1.6之前,你只能使用attr()函数:1.6及以后版本,你可以根据实际需要选择对应 ...
- 构造IOCTL命令的学习心得-----_IO,…
在编写ioctl代码之前,需要选择对应不同命令的编号.为了防止对错误的设备使用正确的命令,命令号应该在系统范围内唯一,这种错误匹配并不是不会发生,程序可能发现自己正在试图对FIFO和audio等这类非 ...
- 字节流之文件输出流FileOutputStream
文件拷贝:
- 1 JPA入门----项目搭建以及CRUD
maven搭建JPA开发环境 1 依赖的maven pom文件 主要有hibernate-core.hibernate-entitymanager.javax-persistence.mysq ...
- 轻量级的同步机制——volatile语义详解(可见性保证+禁止指令重排)
目录 1.关于volatile 2.语义一:内存可见性 2.1 一个例子 2.2 java的内存模型(JMM) 2.3 happens-before规则 2.4 volatile解决内存可见性问题的原 ...
- Elasticsearch聚合限制内存使用
限制内存使用 通常为了让聚合(或者任何需要访问字段值的请求)能够快点,访问fielddata一定会快点, 这就是为什么加载到内存的原因.但是加载太多的数据到内存会导致垃圾回收(gc)缓慢, 因为JVM ...
- Html Meta标签记录
记录学习过程中碰到的meta标签 方便今后查阅 X-UA-Compatible: 设置浏览器兼容 如<meta http-equiv="X-UA-Compatible" co ...
- 501. Find Mode in Binary Search Tree查找BST中的众数
[抄题]: Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently oc ...
- opennebula image单个实例响应数据格式
{ ", ", ", "TEMPLATE": { "DEV_PREFIX": "hd", " }, ...
- Mac notes
1. Mac应用数据存放位置 ~/Library/Application Support/ 比如sublime text的应用数据~/Library/Application Support/Subli ...