define a class for a linked list and write a method to delete the nth node.
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.的更多相关文章
- [Linked List]Remove Nth Node From End of List
Total Accepted: 84303 Total Submissions: 302714 Difficulty: Easy Given a linked list, remove the nth ...
- 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 ...
- 函数定义从零开始学C++之从C到C++(一):const与#define、结构体对齐、函数重载name mangling、new/delete 等
今天一直在学习函数定义之类的问题,下午正好有机会和大家共享一下. 一.bool 类型 逻辑型也称布尔型,其取值为true(逻辑真)和false(逻辑假),存储字节数在不同编译系统中可能有所不同,VC+ ...
- const与#define、结构体对齐、函数重载name mangling、new/delete 等
一.bool 类型 逻辑型也称布尔型,其取值为true(逻辑真)和false(逻辑假),存储字节数在不同编译系统中可能有所不同,VC++中为1个字节. 声明方式:bool result; result ...
- 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-> ...
- 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 ...
- 关于链表的一些重要操作(Important operations on a Linked List)
上篇博文中讨论了链表的一些基本操作: 链表的基本操作(Basic Operations on a Linked List) 然而,为创建一个多功能的链表,在深度学习之前我们还需要了解更多的链表操作. ...
- 链表的基本操作(Basic Operations on a Linked List)
链表可以进行如下操作: 创建新链表 增加新元素 遍历链表 打印链表 下面定义了对应以上操作的基本函数. 创建新链表 新链表创建之后里面并没有任何元素,我们要为数据在内存中分配节点,再将节点插入链表.由 ...
- 【LeetCode题解】链表Linked List
1. 链表 数组是一种顺序表,index与value之间是一种顺序映射,以\(O(1)\)的复杂度访问数据元素.但是,若要在表的中间部分插入(或删除)某一个元素时,需要将后续的数据元素进行移动,复杂度 ...
随机推荐
- c#中的jQuery——HtmlAgilityPack
原文:c#中的jQuery--HtmlAgilityPack c#中是否有javascript中的jQuery类库? jQuery在访问和操作HTML 的DOM的便捷是前端开发工程师的一种福音,在c# ...
- hdu4059 The Boss on Mars
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 在RHEL上安装Thrift(支持C++)的若干问题 » 编码无悔 / Intent & Focused
在RHEL上安装Thrift(支持C++)的若干问题 » 编码无悔 / Intent & Focused [原创]在RHEL上安装Thrift(支持C++)的若干问题 2010年12月1 ...
- dia 在Linux(ubuntu)下无法输入中文的解决办法 .
我是执行一下命令安装的 sudo apt-get install dia sudo apt-get install dia 打开软件后发现不能输入中文,网上搜索一圈后找到以下解决方案 sudo vim ...
- Ipsec transport mode and turnnel mode
隧道(tunnel)模式:用户的整个IP数据包被用来计算AH或ESP头,AH或ESP头以及ESP加密的用户数据被封装在一个新的IP数据包中.通常,隧道模式应用在两个安全网关之间的通讯. 传输(t ...
- BGP的状态机制
Idle 状态:即空闲状态,不接受任何BGP的连接,等待Start事件的产生,如果有start事件产生,若有start事件产生,系统开启ConnectRetry定时器,向邻居发起TCP连接,并将状态变 ...
- poj 3280 Cheapest Palindrome ---(DP 回文串)
题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...
- java注解(一)
虽然平时有使用注解,不过没有深入了解,今天无聊,重新从基础深入了解整理下: java注解是附加在代码中的一些元信息,用于一些工具在编译.运行时进行解析和使用,起到说明.配置的功能. 注解不会也 ...
- windows下php开发环境的搭建
环境搭建软件组合为:Apache2.2.9+mysql5.2.32+php5.2.6 下载地址如下 http://download.csdn.net/detail/xttxqjfg/5670455 ...
- cocos2d-x 音乐/音效设置
cocos2d-x 游戏中声音 有两种 一种是背景音乐一种是音效 载入音乐 或者音效的时候 我们须要先缓存声音 #include "SimpleAudioEngine.h" usi ...