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. HLG 2163 方格取数 (最大网络流)

    题目链接:  m=ProblemSet&a=showProblem&problem_id=2163">点击打开链接 Description : 给你一个n*n的格子的棋 ...

  2. 《Java并发编程实战》第十六章 Java内存模型 读书笔记

    Java内存模型是保障多线程安全的根基,这里不过认识型的理解总结并未深入研究. 一.什么是内存模型,为什么须要它 Java内存模型(Java Memory Model)并发相关的安全公布,同步策略的规 ...

  3. 利用Gearman实现并发查询(Multi-Query)

    这个样例是想从数据库查询出几个结果集,一般的做法是,一个接一个的发送查询,然后汇总结果进行输出. 以下我们利用Gearman的gearman_client_run_tasks实现并发的查询,gearm ...

  4. 【 D3.js 入门系列 --- 8 】 对话操作(事件)

    本人的个人博客为: www.ourd3js.com csdn博客为: blog.csdn.net/lzhlzz 转载请注明出处,谢谢. 这一节介绍怎样进行对话的操作,如鼠标单击,鼠标滑过等. 对一个被 ...

  5. Unity3d之MiniJson与LitJson之间的较量

    由于项目不得不用到json来解析服务器端传来的数据,于是不得不选择一种在unity3d上面可用的json.开始根据网上推荐LitJson,于是下载下来源码,导入项目: 经过测试可以用:但是移植到ipa ...

  6. 【Java】运用JDBC实现一个注册、登录系统的编写

    数据库的建立 首先,建立一个数据库,存储注册成功的账户信息. 其SQL的DDL语句如下: CREATE TABLE `jdbctest` ( `id` int(10) NOT NULL auto_in ...

  7. 《JavaScript设计模式与开发实践》读书笔记之享元模式

    1. 享元模式 享元模式是一种用于性能优化的模式,享元模式的核心是运用共享技术来有效支持大量细粒度的对象 1.1 传统的文件上传方法 以文件上传为例,文件上传功能可以选择依照队列,一个一个的排队上传, ...

  8. 让ecshop显示商品销量或者月销量

    首先,ecshop的信息显示模块在. ./includes/lib_goods.php文件 在其末尾添加下面这个函数 月销量:(和总销量二选一) function ec_buysum($goods_i ...

  9. Oracle JDBC版本区别(转)

    oracle\product\11.2.0\dbhome_1\jdbc\lib ojdbc5.jar ojdbc5dms.jar ojdbc5dms_g.jar ojdbc5_g.jar ojdbc6 ...

  10. Flipping Game(枚举)

    Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard input ...