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)\)的复杂度访问数据元素.但是,若要在表的中间部分插入(或删除)某一个元素时,需要将后续的数据元素进行移动,复杂度 ...
随机推荐
- 做外贸,独立B2C商城好,还是平台好
随着跨境电商热的来临,越来越多的国内企业选择进军跨国电商,那么企业要想进军以互联网跨国销售,通常会通过两种途径,一种是建立独立运营的B2C商城,还有一种是依托alibaba,dhgate,aliexp ...
- JAVA - 优雅的记录日志(log4j实战篇) (转)
写在前面 项目开发中,记录错误日志有以下好处: 方便调试 便于发现系统运行过程中的错误 存储业务数据,便于后期分析 在java中,记录日志有很多种方式: 自己实现 自己写类,将日志数据,以io操作方式 ...
- HLG 2163 方格取数 (最大网络流)
题目链接: m=ProblemSet&a=showProblem&problem_id=2163">点击打开链接 Description : 给你一个n*n的格子的棋 ...
- HADOOP2.6
LINUX下HADOOP2.6.0集群环境的搭建 本文旨在提供最基本的,可以用于在生产环境进行Hadoop.HDFS分布式环境的搭建,对自己是个总结和整理,也能方便新人学习使用. 基础环境 JDK的安 ...
- POJ 2002 点hash
Squares Time Limit: 3500MS Memory Limit: 65536K Total Submissions: 15489 Accepted: 5864 Descript ...
- C++ Primer 学习笔记_62_重载操作符与转换 --调用操作符和函数对象
重载操作符与转换 --调用操作符和函数对象 引言: 能够为类类型的对象重载函数调用操作符:一般为表示操作的类重载调用操作符! struct absInt { int operator() (int v ...
- 浅析JAVA设计模式之工厂模式(一)
1 工厂模式简单介绍 工厂模式的定义:简单地说,用来实例化对象,取代new操作. 工厂模式专门负责将大量有共同接口的类实例化.工作模式能够动态决定将哪一个类实例化.不用先知道每次要实例化哪一个类. 工 ...
- windows phone 7 通过Post提交URL到服务器,从服务器获取数据(比如登陆时候使用)
原文:windows phone 7 通过Post提交URL到服务器,从服务器获取数据(比如登陆时候使用) HttpWebRequest myRequest = (HttpWebRequest)Web ...
- 树上第k小,可持久化线段树+倍增lca
给定一颗树,树的每个结点都有权值, 有q个询问,每个询问是 u v k ,表示u到v路径上第k小的权值是多少. 每个结点所表示的线段树,是父亲结点的线段树添加该结点的权值之后形成的新的线段树 c[ro ...
- Shine we together: A innovative dating site using 2012 Nobel Laureate Roth's algorithm
Abstract Our dating site introduced scoring and its related functionalities innovatively, conforming ...