1、问题

define a class for a linked list and write a method to delete the nth node.

2、算法

template <typename C>

struct Node{

C content ;

Node<C>* next ;

}

template <typename T>

class List{

private:

Node<T>* head ;

unsigned int size ;

public:

List()

{

size = 0 ;

head = NULL ;

}

Node<T>* getHead() { return head ; } ;

bool insert(const T& data)

{

Node<T>* node = new Node<T> ;

node->content = data ;

node->next = head;

head = node ;

size++ ;

return true ;

}

bool insert(const T&data, int pos)

{

if( pos > size )

{

return false ;

}else if( pos == 0 )

{

return insert(data) ;

}

Node<T>* node = new Node<T> ;

node->content = data ;

node->next = NULL ;

unsigned int idx = 1 ;

Node<T>* frontNode = head ;

while(idx < pos)

{

idx++ ;

frontNode = frontNode->next ;

}

node->next = frontNode->next ;

frontNode ->next = node ;

size++ ;

return true ;

}

bool remove( int pos )

{

if( pos > size )

{

return false ;

}

unsigned int idx = 0 ;

Node<T>* frontNode = NULL ;

Node<T>* node = head ;

while(idx < pos)

{

idx++ ;

frontNode = node ;

node = node->next ;

}

if( frontNode  )

{

frontNode->next = node->next ;

}else{

head = head->next ;

}

delete node ;

size-- ;

return true ;

}

}

define a class for a linked list and write a method to delete the nth node.的更多相关文章

  1. [Linked List]Remove Nth Node From End of List

    Total Accepted: 84303 Total Submissions: 302714 Difficulty: Easy Given a linked list, remove the nth ...

  2. remove Nth Node from linked list从链表中删除倒数第n个元素

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  3. 函数定义从零开始学C++之从C到C++(一):const与#define、结构体对齐、函数重载name mangling、new/delete 等

    今天一直在学习函数定义之类的问题,下午正好有机会和大家共享一下. 一.bool 类型 逻辑型也称布尔型,其取值为true(逻辑真)和false(逻辑假),存储字节数在不同编译系统中可能有所不同,VC+ ...

  4. const与#define、结构体对齐、函数重载name mangling、new/delete 等

    一.bool 类型 逻辑型也称布尔型,其取值为true(逻辑真)和false(逻辑假),存储字节数在不同编译系统中可能有所不同,VC++中为1个字节. 声明方式:bool result; result ...

  5. Reverse Linked List II [LeetCode]

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  6. 237. Delete Node in a Linked List(C++)

    237. Delete Node in a Linked Lis t Write a function to delete a node (except the tail) in a singly l ...

  7. 关于链表的一些重要操作(Important operations on a Linked List)

    上篇博文中讨论了链表的一些基本操作: 链表的基本操作(Basic Operations on a Linked List) 然而,为创建一个多功能的链表,在深度学习之前我们还需要了解更多的链表操作. ...

  8. 链表的基本操作(Basic Operations on a Linked List)

    链表可以进行如下操作: 创建新链表 增加新元素 遍历链表 打印链表 下面定义了对应以上操作的基本函数. 创建新链表 新链表创建之后里面并没有任何元素,我们要为数据在内存中分配节点,再将节点插入链表.由 ...

  9. 【LeetCode题解】链表Linked List

    1. 链表 数组是一种顺序表,index与value之间是一种顺序映射,以\(O(1)\)的复杂度访问数据元素.但是,若要在表的中间部分插入(或删除)某一个元素时,需要将后续的数据元素进行移动,复杂度 ...

随机推荐

  1. postgresql优化数据的批量插入

    原文:http://www.cnblogs.com/mchina/archive/2012/08/11/2537393.html 有以下几种方法用于优化数据的批量插入. 1. 关闭自动提交:      ...

  2. TCP/UDP简介

    TCP/UDP简介 Socket小白篇-附加TCP/UDP简介 Socket 网络通信的要素 TCP和UDP Socket的通信流程图 1.Socket 什么是Socket Socket:又称作是套接 ...

  3. hdu4521(线段树+dp)

    传送门:小明系列问题——小明序列 题意:有n个数,求间距大于d的最长上升序列. 分析:dp[i]表示在i点以a[i]结束距离大于d的最长上升序列,然后每更新到第i点时,取i-d之前小于a[i]的数为结 ...

  4. POJ 3458 Colour Sequence(简单题)

    [题意简述]:事实上题意我也没有特别看懂.可是依据它少许的题目描写叙述加上给的例子.就大胆的做了例如以下的推測: 就是说,如今给出一串字符s.然后紧接着给出可见的字符串visible还有隐藏的字符串h ...

  5. C++实现链栈的基本操作

    之前对顺序栈写了基本操作,认为有必要也动手练练栈的链表实现. 对于链栈,一般不会出现栈满的情况. 链栈头文件定义例如以下: #ifndef CSTOCK_H_ #define CSTOCK_H_ ty ...

  6. Android编程心得-图片自适应心得

    在Android 的开发过程中,我们知道存放图片资源的文件夹是drawable,与它类似的名字的文件夹drawble-hdpi,drawable-ldpi,drawable-mdpi,drawable ...

  7. 读书笔记:《重来REWORK》

    读书笔记:<重来REWORK> <重来Rework--更为简单有效的商业思维>这本书是看了别人的书单而购买的,初 拿到这本书翻看时,感觉有两点与平常的书不同,一是每个小节非常短 ...

  8. Linux 命令学习之dpkg命令详解

    dpkg是一个Debian的一个命令行工具,它可以用来安装.删除.构建和管理Debian的软件包. 下面是它的一些命令解释: 1)安装软件 命令行:dpkg -i <.deb file name ...

  9. 寻找失踪的整数数组(Find the missing integer)

    排列a包含N分子,其元素属于[0,N]之间,且不存在反复的元素.请你找出数组中缺失的元素(由于[0,N]之间有N+1个元素.而数组仅仅能存储N个元素.所以必定缺少一个元素).当中对数组的操作满足下列的 ...

  10. 学习 easyui 之二:jQuery 的 ready 函数和 easyloader 的加载回调函数

    Ready 事件不一定 ready 使用 easyloader 的时候,必须要注意到脚本的加载时机问题,easyloader 会异步加载模块,所以,你使用的模块不一定已经加载了.比如下面的代码. &l ...