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. c++一些语法模板

    函数模板特 template <class T> int compare(T v1,T v2) { if(v1<v2) return -1; else if(v1>v2) re ...

  2. 深入理解Tomcat系列之二:源码调试环境搭建(转)

    前言 最近对Tomcat的源码比较感兴趣,于是折腾了一番.要调试源码首先需要搭建环境,由于参考了几篇帖子发现都不怎么靠谱,最后还是折腾出来了,然而却花了足足一天的时间去搭建这个环境.发现都不是帖子的问 ...

  3. Ipsec transport mode and turnnel mode

     隧道(tunnel)模式:用户的整个IP数据包被用来计算AH或ESP头,AH或ESP头以及ESP加密的用户数据被封装在一个新的IP数据包中.通常,隧道模式应用在两个安全网关之间的通讯. 传输(t ...

  4. SVN的switch命令

    语法就不说了,文档有的是,主要是两个常用的用法: . 切换资源库(svn sw --relocate) [plain] view plaincopy svn sw --relocate <fro ...

  5. 2)JS动态生成HTML元素的爬取

    2)JS动态生成HTML元素的爬取 import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.W ...

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

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

  7. cocoa动态方法决议及消息转发

    假设给一个对象发送不能响应的消息,同一时候又没有进行动态方法决议,又没实现消息转发,那么就会引发以下的crash信息 2014-07-30 15:47:54.434 MethodNotFind[171 ...

  8. java获取当前日期时间代码总结

    1.获取当前时间,和某个时间进行比较.此时主要拿long型的时间值.  方法如下: 要使用 java.util.Date .获取当前时间的代码如下  代码如下 复制代码 Date date = new ...

  9. 安卓---项目中插入百度地图sdk

    百度地图 应用里面 自带地图 搜房网 下载百度地图的sdk 熟悉api 注冊百度开发人员的账号 2.12 仅仅要有一个ak就能够 高版本号须要提供应用程序的包名和签名返回开发人员的序列号 使用百度地图 ...

  10. Google Earth数据存储、管理、表现及开发机制

    Google Earth数据存储.管理.表现及开发机制 一.    Google Earth(Map)介绍 1.1    Google Earth介绍 在众多的地理信息服务提供商中,Google是较早 ...