利用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++ 版本的更多相关文章

  1. 如何在 OSX 中使用多个JDK版本

    升级macbook小白的硬盘成SSD后,重新安装了系统和JDK8,但是启动eclipse还是报告需要安装JDK6,于是也按照提示安装了Apple JDK6,这导致系统中有两个JDK,一个是Oracle ...

  2. react-router-dom Link search 传参

    <Link> 和之前版本没太大区别,重点看下组件属性: to(string/object):要跳转的路径或地址: replace(bool):为 true 时,点击链接后将使用新地址替换掉 ...

  3. mac下通过brew切换php版本

    第一步,先安装 brew    Brew 是 Mac 下面的包管理工具,通过 Github 托管适合 Mac 的编译配置以及 Patch,可以方便的安装开发工具. Mac 自带ruby 所以安装起来很 ...

  4. DevExpress 隐藏Ribbon中barbuttonItem的SuperTip(1)

    public frmMain() { InitializeComponent(); ribbonControl1.Manager.HighlightedLinkChanged += Manager_H ...

  5. visual.studio.15.preview5 编译器

    前段时间微软更新了新版开发工具visual studio 15 preview5,安装后连文件结构目录都变了,想提取编译器还找不到. 不是原来的VC\BIN目录,已迁移到IDE\MSVC\14.10. ...

  6. Visual Assist的破解与安装

    转载[PYG成员作品] [2016-09-26更新]Visual Assist X10.9.2112-Cracked.By.PiaoYun/P.Y.G 近期的一个稳定版本的破解方式: VA原版, VA ...

  7. React Router 4.x 开发,这些雷区我们都帮你踩过了

    前言 在前端框架层出不穷的今天,React 以其虚拟 DOM .组件化开发思想等特性迅速占据了主流位置,成为前端开发工程师热衷的 Javascript 库.作为 React 体系中的重要组成部分:Re ...

  8. Appium1.9 之 Chromedriver安装方式

    1.在 appium 官网上下载安装后,下载的是1.7.1的版本,安装之后是1.9.1最新版本. 2.appium安装之后,会发现涉及到 浏览器相关的业务时(我使用的是chrome)会提示 “No C ...

  9. [Web 前端] React Router v4 入坑指南

    cp from : https://www.jianshu.com/p/6a45e2dfc9d9 万恶的根源 距离React Router v4 正式发布也已经过去三个月了,这周把一个React的架子 ...

随机推荐

  1. sftp put权限不够

    报错如下: sftp> put play.zip ./ Uploading play.zip to /opt/library/./play.zip remote open("/opt/ ...

  2. java定时任务调度工具Timer与Quartz的区别

    Timer与Quartz的区别有三点: 1.出身不同:Timer由jdk直接提供,调用方式简单粗暴,不需要其它jar包支持.Quartz并非jdk自带,需要引入相应的jar包 2.能力区别:主要体现在 ...

  3. Linux的基本指令shell

    计算机语言的进化过程,7代语言 机器语言(6种位运算)->  汇编语言->C语言  ->  C++/Java   -> Paython / go /Ruby   -> 自 ...

  4. key things of ARC

    [key things of ARC] 1.使用原则. 2.__weak变量的使用问题 3.__autoreleasing的使用问题 4.block中易造成的强引用环问题. 5.栈变量被初始化为nil ...

  5. ora-28547:连接服务器失败,可能是 Oracle Net 管理失败

    检查如下: 监听程序的配置文件 发现多了 (PROGRAM = extproc) 去掉后如下: # listener.ora Network Configuration \dbhome_1\netwo ...

  6. Oracle 中 over() 函数

    :first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { border: 0; m ...

  7. linux安全关机脚本

    linux安全关机脚本 在断电4分钟后判断关键 目的:在断电以后服务器连接UPS,UPS最多只能支持5分钟也会没电,所以在这里做个判断,如果断电4分钟后,市电还没来就关机. 以下两个设备为两个下路由器 ...

  8. 3.Dynamic Layout 动态布局。在槽中处理布局

    在应用程序中,一个界面的布局基本都是固定的. 在这个实例中,我们把管理布局的代码放在槽中.这样点击一次按钮,触发槽.布局改变一次.这样就成为一个动态布局. (一) 水平和竖直布局改变 横向: 纵向: ...

  9. 4.python 系统批量运维管理器之paramiko模块

    paramiko paramiko是ssh服务最经常使用的模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. paramiko实现ssh2不外乎两个角度:SSH客户端与服务端 SS ...

  10. css总结6:行高和字体大小

    1 CSS line-height 属性 代码: p.small {line-height:70%}p.big {line-height:200%} 运行后:70%与200%宽高 2 CSS font ...