(C/C++) Link List - C++ 版本
利用C++寫一個基本的 Link list 練習,功能包含 pint list、CreatList、Insert、Delete、Reverse、Search、Clear、GetLen。
先建立相關的Class ListNode、LinkedList
1 class LinkedList; // 需要先宣告 2 class ListNode{ 3 public: 4 int data; 5 ListNode *next; 6 public: 7 ListNode():data(0), next(0){}; 8 ListNode(int a):data(a), next(0){}; 9 friend class LinkedList; 10 }; 11 12 13 class LinkedList{ 14 public: 15 // int size; // size是用來記錄Linked list的長度, 非必要 16 ListNode *first; 17 public: 18 LinkedList() :first(0){}; 19 void PrintList(); 20 void CreateList(int *arr, int len); 21 void Push_front(int x); 22 void Push_back(int x); 23 void Delete(int x); 24 void Clear(); 25 void Reverse(); 26 int ListLen(); 27 ListNode * ListSearch(int x); 28 void Insert(int x, int data); 29 };
首先是Creat List,給一個arry利用Link list串在一起。
1 void LinkedList::CreateList(int *arr, int len) 2 { 3 if (arr == NULL || len == 0) 4 return; 5 6 ListNode * previous = new ListNode(); 7 for (int i = 0; i < len; i++) 8 { 9 ListNode * newNode = new ListNode(); 10 newNode->data = arr[i]; 11 if (i == 0) 12 first = newNode; 13 else 14 previous->next = newNode; 15 newNode->next = NULL; 16 previous = newNode; 17 } 18 }
PrintList : 印出所有的 List
1 void LinkedList::PrintList() 2 { 3 if (first == 0) 4 { 5 cout << "List is empty.\n"; 6 return; 7 } 8 9 ListNode * current = first; 10 while (current != 0) 11 { 12 cout << current->data << " "; 13 current = current->next; 14 } 15 cout << endl; 16 }
Insert List : 把新的node插在x之後
1 void LinkedList::Insert(int x, int data) 2 { 3 ListNode * current = first; 4 ListNode * previous = new ListNode(); 5 ListNode * newNode = new ListNode(); 6 while (current) 7 { 8 if (current->data == x) 9 { 10 newNode->data = data; 11 previous->next = newNode; 12 newNode->next = current; 13 break; 14 } 15 else 16 { 17 previous = current; 18 current = current->next; 19 } 20 } 21 22 }
Delete : 刪除特定 node
1 void LinkedList::Delete(int x) 2 { 3 ListNode * current = first, *previous = 0; 4 5 while (current != 0 && current->data != x) { 6 previous = current; 7 current = current->next; 8 } 9 10 if (current == 0) { 11 std::cout << "There is no " << x << " in list.\n"; 12 } 13 else if (current == first) { 14 first = current->next; 15 delete current; 16 current = 0; 17 } 18 else { 19 previous->next = current->next; 20 delete current; 21 current = 0; 22 } 23 }
Reverse : 反轉 list
1 void LinkedList::Reverse() 2 { 3 if (first == 0 || first->next == 0) 4 return; 5 6 ListNode *previous = 0, *current = first, *preceding = first->next; 7 8 while (preceding != 0) 9 { 10 current->next = previous; 11 previous = current; 12 current = preceding; 13 preceding = preceding->next; 14 } 15 current->next = previous; 16 first = current; 17 }
Search node : 尋找特定node
1 ListNode * LinkedList::ListSearch(int x) 2 { 3 if (first == 0) 4 { 5 printf("List is empty\n"); 6 return NULL; 7 } 8 ListNode * newNode = first; 9 while (newNode) 10 { 11 if (newNode->data != x) 12 newNode = newNode->next; 13 else 14 return newNode; 15 } 16 printf("not found Node!!\n"); 17 return NULL; 18 }
ListLen : 回傳List長度
1 int LinkedList::ListLen() 2 { 3 if (first == 0) return NULL; 4 int index = 0; 5 ListNode * newNode = first; 6 while (newNode) 7 { 8 newNode = newNode->next; 9 index++; 10 } 11 return index; 12 }
Push_front/back : 插入節點在頭/尾
1 void LinkedList::Push_front(int x) 2 { 3 ListNode *newNode = new ListNode(x); 4 newNode->next = first; 5 first = newNode; 6 } 7 8 void LinkedList::Push_back(int x) 9 { 10 ListNode * newNode = new ListNode(x); 11 if (first == 0) 12 { 13 first = newNode; 14 return; 15 } 16 ListNode * current = first; 17 while (current->next != 0) 18 { 19 current = current->next; 20 } 21 current->next = newNode; 22 }
转载于:https://www.cnblogs.com/ollie-lin/p/9919072.html
(C/C++) Link List - C++ 版本相关推荐
- 在MacOS下通过brew link切换golang版本
安装Homebrew Homebrew 是 MacOS 上的一个包管理工具,类似于 linux 上的 yum .apt.rpm等.如果还没有安装 brew,先安装: /bin/bash -c &quo ...
- Matlab模型可视化仿真:SimMechanics Link的安装与使用
这是我的第一篇博客,以记录研究生生涯的各种瞎折腾.这几天一直在想能不能将Solidworks的模型导入matlab中做可视化仿真,原因在于matlab自带的机器人工具箱实在是太丑了.有多丑,看图就知道 ...
- Mac 下使用 homebrew 切换不同版本 php
最近刚切换回 Mac 下进行开发,所以写了比较多的 Mac 环境部署之类的文章.今天需要重新切换一下本地的开发环境.原本想直接在当前环境下开发, 毕竟 Mac 自带了 PHP 环境,今天需要新添一个扩 ...
- mac+php版本切换+cli,Mac环境下php版本切换
可以同过brew list 来查看的PHP查看php的版本 例如: brew list brew unlink php56 brew link php55 大版本可以用brew list来查,如果是小 ...
- react router 级联路由_前端路由原理解析和实现
作者:@whinc链接:https://github.com/whinc/blog/issues/13 在单页应用如此流行的今天,曾经令人惊叹的前端路由已经成为各大框架的基础标配,每个框架都提供了强大 ...
- Linux C/C++ Openssl RSA Encrypt/Decrypt(加密/解密) 简单示例教程
PEM文件有以下格式 1.PEM私钥文件格式 -----BEGIN RSA PRIVATE KEY----- -----END RSA PRIVATE KEY----- 生成该密钥的Linux命令 : ...
- Appium1.9 之 Chromedriver安装方式
1.在 appium 官网上下载安装后,下载的是1.7.1的版本,安装之后是1.9.1最新版本. 2.appium安装之后,会发现涉及到 浏览器相关的业务时(我使用的是chrome)会提示 " ...
- Go 函数特性和网络爬虫示例
爬取页面 这篇通过网络爬虫的示例,来了解 Go 语言的递归.多返回值.延迟函数调用.匿名函数等方面的函数特性. 首先是爬虫的基础示例,下面两个例子展示通过 net/http 包来爬取页面的内容. 获取 ...
- 边做边学:《 Inter Planetary File System》简介
by Niharika Singh 由Niharika Singh 边做边学:< Inter Planetary File System>简介 (Learn by doing: a nic ...
最新文章
- 2021第十二届蓝桥杯国赛总结-java大学c组
- React Hooks - useEffect通俗易懂
- web 点击劫持 X-Frame-Options
- linux通用中断子系统介绍
- 大牛书单|安全技术方向好书分享
- python xml etree word_使用python格式化插入的元素xml.etree模块,包括新行
- Golang实践录:使用gin框架实现转发功能:利用nginx转发
- BootStrap的介绍与案例使用
- Machine Learning - week 4 - Non-linear Hypotheses
- Excel数据分析的代码(包括列表的操作)
- ppt设置外观样式_ppt怎么设置幻灯片的背景一样?
- NSIS 安装VS2008运行时
- 简体字和繁体字转换四种方法
- 美国小学计算机编程课程,美国小学STEM课程案例研究
- 吹气球 Burst Balloons
- Modbus通信协议格式以及功能码
- 【时间之外】几张图说清楚供应链金融赚钱方法
- CAD Voronoi 生成插件 V2
- Spring中@Value注解详解
- Elasticsearch-Cat命令详解