C++实现单链表(Singly-Linked List)追加、遍历、删除
2015-01-27 00:13
483 查看
<pre name="code" class="cpp">#include <iostream> using namespace std; struct Node { int data; struct Node* next; }; class List { Node* head; public: List() { head = NULL;} void Print(); int GetSize(); void Append(int addData); void EraseOne(int eraseData); void EraseOne_V2(int eraseData); }; void List::Print() { Node* tmp = head; //list is empty if (tmp == NULL) { cout << "Empty List!\n"; return; } //list is not empty for(; tmp != NULL; tmp = tmp->next) { cout << tmp->data << " --> "; } cout << "NULL\n"; } int List::GetSize() { Node* tmp = head; //list is empty if(tmp == NULL) { return 0; } //list is not empty int num(0); for(; tmp != NULL; tmp = tmp->next) { ++num; } return num; } void List::Append(int addData) { //create a new node Node* newNode = new Node; newNode->data = addData; newNode->next = NULL; Node* tmp = head; //list is empty if(tmp == NULL) { head = newNode; //有可能引入重复释放指针的问题,可以使用智能指针。 return; } //list is not empty, locate the last node for(; tmp->next != NULL; tmp = tmp->next) { } tmp->next = newNode; } void List::EraseOne_V2(int eraseData) { Node* tmp = head; //空链表 if (tmp == NULL) { return; } //只有一个节点的链表 if (tmp->next == NULL) { if (tmp->data == eraseData) { delete tmp; head = NULL; } return; } //有两个及以上节点的链表,待删节点正是首节点 if (tmp->data == eraseData) { head = head->next; delete tmp; tmp = NULL; } //有两个及以上节点的链表,待删节点不是首节点 for (; tmp != NULL; tmp = tmp->next) { if ((tmp->next != NULL) && (tmp->next->data == eraseData)) //找到待删节点的前一个节点 { Node* tmp2 = tmp->next; tmp->next = tmp->next->next; delete tmp2; break; } } } void List::EraseOne(int eraseData) { Node* tmp = head; //////////////////// //list is empty //////////////////// if(tmp == NULL) { return; } //////////////////// //list is not empty //////////////////// int locate(-1), idx(0); for(; tmp != NULL; tmp = tmp->next) { ++idx; if(tmp->data == eraseData) { locate = idx - 1; break; } } if(locate == -1) // eraseData not found { return; } int listSize = GetSize(); if(listSize == 1) // only one node in the list { delete head; head = NULL; } else // more than one node in the list { // eraseData is the 0th node if(locate == 0) { tmp = head; head = head->next; delete tmp; //注意这里释放tmp(并没有用new申请空间给tmp)的空间是可行的,我的理解是因为tmp是结构体指针(相对于基本数据类型指针)。但是不能重复释放。 tmp = NULL; } //eraseNode is the last node if(locate == listSize - 1) { tmp = head; int idx(0); for(; tmp != NULL; tmp = tmp->next) { ++idx; if(idx == locate - 1) { Node* toDel = tmp->next; tmp->next = NULL; delete toDel; } } } // eraseData is intermediate node if(locate > 0 && locate < listSize - 1) { tmp = head; int idx(0); for(; tmp != NULL; tmp = tmp->next) { ++idx; if(idx == locate - 1) { Node* toDel = tmp->next; tmp->next = tmp->next->next; delete toDel; } } } } // more than one node in the list } int main (int argc, char** argv) { // New list List list; list.Print(); list.EraseOne_V2(100); list.Append(2); list.Print(); list.EraseOne_V2(2); list.Print(); // Append nodes to the list list.Append(100); list.Print(); list.Append(200); list.Print(); list.Append(300); list.Print(); list.Append(300); list.Print(); list.Append(500); list.Print(); // Delete nodes from the list list.EraseOne_V2(400); list.Print(); list.EraseOne_V2(100); list.Print(); list.EraseOne_V2(200); list.Print(); list.EraseOne_V2(500); list.Print(); list.EraseOne_V2(100); list.Print(); list.EraseOne_V2(300); list.Print(); return 0; }
程序输出结果:
100 --> NULL 100 --> 200 --> NULL 100 --> 200 --> 300 --> NULL 100 --> 200 --> 300 --> 300 --> NULL 100 --> 200 --> 300 --> 300 --> 500 --> NULL 100 --> 200 --> 300 --> 300 --> 500 --> NULL 100 --> 300 --> 300 --> 500 --> NULL 100 --> 300 --> 300 --> 500 --> NULL 100 --> 300 --> NULL 300 --> NULL Empty List!
相关文章推荐
- 单向链表 (Singly linked list) 的C++实现
- 372. 在O(1)时间复杂度删除链表节点 (delete-node-in-the-middle-of-singly-linked-list)(c++)----lintcode面试题之链表
- C++ Linked list: Traversing a Linked list(遍历链表)
- c++实现单链表创建,删除,遍历,插入,修改操作
- 双向链表的C++实现 Implement of Doubly Linked List
- [C++]Remove Linked List Elements 删除链表中的元素
- 单链表的C++实现 Implement of Linked List
- LintCode Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- 基于jdk的list 和 队列 linkedList 和父亲长子兄弟链表模型 实现的 树 的前序遍历,后序遍历和层次遍历
- lintcode 容易题:Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- 单链表——The headless singly linked list
- MyLinkedList(链表的实现)
- 【C/C++】List链表的功能实现模型
- c++ 链表操作:添加、遍历、删除、查找
- 单链表的实现---SingleLinkedList
- 设计一个整型链表类list,能够实现链表节点的插入、删除、以及链表数据的输出操作。
- 遍历Linux kernel的链表时删除节点的方法 list_for_each_safe
- c++下list在遍历时删除节点的方法
- 用结构体实现链表的创建、遍历、结点插入、结点删除、链表删除-----带菜单选项
- C++链表的创建、插入、删除、查找、合并、排序、修改等操作的实现