define a class for a linked list and write a method to delete the nth node.
来源:互联网 发布:手机浏览器 域名劫持 编辑:程序博客网 时间:2024/05/16 19:26
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.
- Ch2-2: return the nth to the last node data of a singly linked list
- "same tree" and "Delete Node in a Linked List"
- Path Sum and Delete Node in a Linked List
- C++: delete a node from linked list
- leetcode: Delete Node in a Linked List
- LeetCode Delete Node in a Linked List
- Delete Node in a Linked List
- 【leetcode】Delete Node in a Linked List
- Delete Node in a Linked List
- Delete Node in a Linked List
- Leetcode: Delete Node in a Linked List
- 237 - Delete Node in a Linked List
- Delete Node in a Linked List
- [LeetCode]Delete Node in a Linked List
- Delete Node in a Linked List
- 【leetcode】Delete Node in a Linked List
- Delete Node in a Linked List
- Rails mapping array of hashes onto single hash
- 大学毕业感觉没学到啥东西。现在很想学安坐哇
- ADB通信
- hibernate中的update()和saveOrUpdate()的区别,session的load()和get()的区别
- android预览界面出错,Missing styles. Is the correct theme chosen for this layout?
- define a class for a linked list and write a method to delete the nth node.
- c/c++:二维数组作为参数传递
- 16位和32位代码段
- Integer源码 转2进制转16进制
- forward:hello 与 redirect:hello的区别
- cocos2d-x 3.0加载PhysicsEditor生成plist的加载器并生成CCPhysicsBody
- 通过公共函数为私有成员赋值
- string.xml
- 【史玉柱自述:我的营销心得】-成功的牛人其实都有许多你不知道的技能