#ifndef List_h__
#define List_h__ #include <stdio.h> struct ListNode
{
int value;
ListNode* pNext;
ListNode(int n) :value(n), pNext(nullptr){}
}; class ListSolution
{
public:
ListSolution& AddToTail(ListNode** ppHead, int value);
ListSolution& RemoveNode(ListNode** ppHead, int value);
ListSolution& RemoveAll(ListNode** ppHead);
void Print(ListNode* pHead);
}; #endif // List_h__
#include "List.h"

ListSolution& ListSolution::AddToTail(ListNode** ppHead, int value)
{
if (!ppHead)
{
return *this;
} ListNode* pNewNode = new ListNode(value); if (nullptr == *ppHead)
{
*ppHead = pNewNode;
}
else
{
ListNode* pCurNode = *ppHead;
while (pCurNode->pNext)
{
pCurNode = pCurNode->pNext;
} pCurNode->pNext = pNewNode;
} return *this;
} ListSolution& ListSolution::RemoveNode(ListNode** ppHead, int value)
{
if (nullptr == ppHead
|| nullptr == *ppHead)
{
return *this;
} ListNode* pDelNode = nullptr;
if (value == (*ppHead)->value)
{
pDelNode = *ppHead;
*ppHead = (*ppHead)->pNext;
}
else
{
ListNode* pCurNode = (*ppHead);
while (pCurNode->pNext && pCurNode->pNext->value != value)
{
pCurNode = pCurNode->pNext;
} if (nullptr != pCurNode->pNext && pCurNode->pNext->value == value)
{
pDelNode = pCurNode->pNext;
pCurNode->pNext = pCurNode->pNext->pNext;
}
} if (pDelNode)
{
printf("remove %d\n", pDelNode->value);
delete pDelNode;
pDelNode = nullptr;
} return *this;
} ListSolution& ListSolution::RemoveAll(ListNode** ppHead)
{
if (nullptr == ppHead
|| nullptr == *ppHead)
{
return *this;
} ListNode* pCurNode = *ppHead;
ListNode* pDelNode = nullptr;
while (pCurNode->pNext)
{
pDelNode = pCurNode->pNext;
pCurNode->pNext = pCurNode->pNext->pNext; printf("remove %d\n", pDelNode->value);
delete pDelNode;
pDelNode = nullptr;
} printf("remove %d\n", (*ppHead)->value);
delete *ppHead;
*ppHead = nullptr; return *this;
} void ListSolution::Print(ListNode* pHead)
{
ListNode* pCurNode = pHead;
while (pCurNode)
{
printf(" %d", pCurNode->value);
pCurNode = pCurNode->pNext;
} printf("\n");
}
#include "List.h"

int main()
{
ListSolution s;
ListNode* pHead = nullptr; s.AddToTail(&pHead, );
s.AddToTail(&pHead, );
s.AddToTail(&pHead, );
s.AddToTail(&pHead, );
s.AddToTail(&pHead, );
s.AddToTail(&pHead, );
s.Print(pHead);
s.RemoveNode(&pHead, );
s.RemoveNode(&pHead, );
s.RemoveNode(&pHead, );
s.Print(pHead);
s.RemoveAll(&pHead); s.AddToTail(nullptr, );
s.Print(nullptr);
s.RemoveNode(nullptr, );
s.RemoveAll(nullptr); if (nullptr == pHead)
{
printf("pHead == nullptr\n");
} return ;
}

Create a Listlink的更多相关文章

  1. 记一次tomcat线程创建异常调优:unable to create new native thread

    测试在进行一次性能测试的时候发现并发300个请求时出现了下面的异常: HTTP Status 500 - Handler processing failed; nested exception is ...

  2. Could not create SSL connection through proxy serve-svn

    RA layer request failedsvn: Unable to connect to a repository at URL xxxxxx 最后:Could not create SSL ...

  3. android 使用Tabhost 发生could not create tab content because could not find view with id 错误

    使用Tabhost的时候经常报:could not create tab content because could not find view with id 错误. 总结一下发生错误的原因,一般的 ...

  4. Create a Team in RHEL7

    SOLUTION VERIFIED September 13 2016 KB2620131 Environment Red Hat Enterprise Linux 7 NetworkManager ...

  5. Create a bridge using a tagged vlan (8021.q) interface

    SOLUTION VERIFIED April 27 2013 KB26727 Environment Red Hat Enterprise Linux 5 Red Hat Enterprise Li ...

  6. [转]nopCommerce Widgets and How to Create One

    本文转自:https://dzone.com/articles/what-are-nopcommerce-widgets-and-how-to-create-one A widget is a sta ...

  7. Git异常:fatal: could not create work tree dir 'XXX': No such file or directory

    GitHub实战系列汇总:http://www.cnblogs.com/dunitian/p/5038719.html ———————————————————————————————————————— ...

  8. SQL Server 在多个数据库中创建同一个存储过程(Create Same Stored Procedure in All Databases)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 遇到的问题(Problems) 实现代码(SQL Codes) 方法一:拼接SQL: 方法二: ...

  9. SharePoint 2013 create workflow by SharePoint Designer 2013

    这篇文章主要基于上一篇http://www.cnblogs.com/qindy/p/6242714.html的基础上,create a sample workflow by SharePoint De ...

随机推荐

  1. 简述 C、C++程序编译的内存分配情况【转】

    面试题 9:简述 C.C++程序编译的内存分配情况  C.C++中内存分配方式可以分为三种:  (1)从静态存储区域分配:  内存在程序编译时就已经分配好,这块内存在程序的整个运行期间都存在.速度快. ...

  2. qsort函数、sort函数【转】

    http://blog.163.com/yuhua_kui/blog/static/9679964420142195442766/ 先说明一下:qsort和sort,只能对连续内存的数据进行排序,像链 ...

  3. 浅入浅出EmguCv(三)EmguCv打开指定视频

    打开视频的思路跟打开图片的思路是一样的,只不过视频是由一帧帧图片组成,因此,打开视频的处理程序有一个连续的获取图片并逐帧显示的处理过程.GUI同<浅入浅出EmguCv(二)EmguCv打开指定图 ...

  4. [2014.01.27]wfGifAnimator 动画GIF组件 3.0

    组件支持设置GIF帧延时和获取GIF的帧延迟. 组件支持添加或插入或更新帧(支持bmp/jpg/gif/wmf/emf/ico格式).删除帧.清空帧操作. 组件支持GIF动画缩放大小. 组件支持绘制线 ...

  5. Android数据库的运用

    很简单的应用,所以我直接简单明了.     android中数据库的运用:    1.定义类继承SQLiteOpenHelper   ps:   public class DBHelper exten ...

  6. 彻底卸载 RAD Studio 2009/2010/XE

    彻底卸载 RAD Studio 2009/2010/XE: 控制面板-->添加/删除程序中执行了卸载操作以后, 还需要做以下工作: 1. 删除以下目录(注意有些目录是隐藏的): %ALLUSER ...

  7. 优化Select 语句的原则

    优化Select 语句的原则 -摘抄<SQL Server 2005 性能监测与优化> Select 语句是数据库应用系统中最常用的语句之一,Select 语句设计的好坏直接影响到应用程序 ...

  8. html: title换行方法 如a链接标签内title属性鼠标悬停提示内容换行

    换行代码符合分别为: “&#;”和“&#;” <a href="0.shtml" title="第一排 第二排 第三排">title ...

  9. IIS7下使用4.0框架集成模式URLRewriter重写中文URL乱码问题

    解决方法: 1 url中中文先通过Server.UrlEncode编码 2 修改网站编码 3.更改URLRewriter.net中源码 找到RewriterEngine类中 private strin ...

  10. npm镜像

    npm config set registry https://registry.npm.taobao.org // 配置后可通过下面方式来验证是否成功 npm config get registry ...