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. STL学习总结

    STL就是Standard Template Library,标准模板库.这可能是一个历史上最令人兴奋的工具的最无聊的术语.从根本上说,STL是一些"容器"的集合.这些" ...

  2. Qt笔记——MOC(莫克)

    moc 代表 Meta-Object Compiler,"元对象编译器".Qt 程序在交由标准编译器编译之前,先要使用 moc 分析 C++ 源文件. 假设它发如今一个头文件里包括 ...

  3. 杭州电ACM1098——Ignatius&#39;s puzzle

    这个话题.简单的数学. 对于函数,f(x)=5*x^13+13*x^5+k*a*x,输入k,对于休闲x,一个数字的存在a,使f(x)是65可分. 对于休闲x. 因此,当x = 1时间,f(x) = 1 ...

  4. Java对象序列化/反序列化的注意事项

    Java对象序列化 对于一个存在Java虚拟机中的对象来说,其内部的状态只是保存在内存中.JVM退出之后,内存资源也就被释放,Java对象的内部状态也就丢失了.而在很多情况下,对象内部状态是需要被持久 ...

  5. poj3984(经典dfs)

    题目链接:http://poj.org/problem?id=3984 分析:直接深搜从起点到终点,如何取最短路线,其实只要优先向下或向右走即可. #include <cstdio> #i ...

  6. UVa 1292 - Strategic game (树形dp)

    本文出自   http://blog.csdn.net/shuangde800 题目链接: 点击打开链接 题目大意 给定一棵树,选择尽量少的节点,使得每个没有选中的结点至少和一个已选结点相邻. 思路 ...

  7. SE 2014年5月28日

    R1模拟总部,R2 与R3模拟分部 如图配置 (1)网络中目前只有两站点, R1 和R2 .同时R2为动态获取IP地址一方,要求使用要求使用 GRE over IPSec VPN 野蛮模式,保证R1和 ...

  8. 13.怎样自学Struts2之Struts2本地化[视频]

    13.怎样自学Struts2之Struts2本地化[视频] 之前写了一篇"打算做一个视频教程探讨怎样自学计算机相关的技术",优酷上传不了,仅仅好传到百度云上: http://pan ...

  9. Swift的属性,方法,下标脚本以及继承

    从这篇章节起,Swift编程语言指南大部分的重要内容在于概念,代码并非太多.理解Swift的面向对象理念,语法以及类结构,构造析构过程对于非常好的应用Swift语言将会有比較大的帮助. 属性 存储属性 ...

  10. shell统计

    for i in `ls -r *_*.csv`;do cat $i|echo $i": "`wc -l`;done>tongji.txt