既然已经知道了如何将一个节点插入到linked list, 那么当然也就知道如何删除节点了。
delete也分为3中情况(与插入不同)。
(1)linked list 为空链表的时候, 无法执行删除操作。
(2)delete from the head
例如下面:
由于搜索的时候, head就和节点匹配, 我们推出遍历程序。 将head 指向下一个节点, 删除curr(其实删除的是curr指向的动态节点内存):
(3)delete from somewhere beyond the head.
例如:
需要调整指针, trial指向curr指向节点的下一个指针, 然后删除curr指针(实际删除的是指向的节点):
下面打开NewLinkedList 工程, 做如下修改, 只post 改动了的文件。
ContactList.h文件如下:
/*
*ContactList.h
*
* created on Jul 6, 2014
* Author: ***
*
*
*/
#ifndef CONTACT_LIST_H
#define CONTACT_LIST_H
#include "Contact.h" // the first thing to do is to include the node header file
class ContactList {
public:
ContactList(); // prototype for constructor
void AddToHead(const std::string&);//reference, 避免复制, 更快, const, 所以不让修改
void printList();
void insert(const std::string& inputName);
void deleteNode(const std::string&);
private:
Contact* head;
int length;
};
#endif /*end of CONTACT_LIST_H*/
ContactList.cpp 如下:
// an implementation of ContactList.h
#include "ContactList.h"
using namespace std;
ContactList::ContactList():head(0), length(0) {
}
void ContactList::AddToHead(const string& name) {
Contact* newOne = new Contact(name);
if(head == 0) {
head = newOne;
}
else {
newOne -> next = head;
head = newOne;
}
length++;
}
void ContactList::printList() {
Contact* tp;
tp = head;
while(tp != NULL) {
cout << tp -> name << endl;
tp = tp -> next;
}
}
void ContactList::insert(const string& inputName) {
Contact* newNode = new Contact(inputName);//create a node in the heap
//case1 - empty list
if(head == 0) {
head = newNode;
}
else {
Contact* curr = head;
Contact* trail = 0;
//Traverse the list to find insert location
while(curr != 0) {
if(curr -> name >= newNode ->name) {
break;
}
else {
trail = curr;
curr = curr -> next;
}
}
//case2 - insert at head(not empty)
if(curr == head) {
newNode -> next = head;
head = newNode;
}
else {
//case3 - insert somewhere after the head(not empty)
newNode -> next = curr;
trail -> next = newNode;
}
}
length++; // incrementing the size
}
void ContactList::deleteNode(const string& inputName) {
//case1 - empty list
if(head == 0) {
cout << "empty list, cannot delete " << endl;
}
else {
Contact* curr = head;
Contact* trail = 0;
// traverse list to find the node to delete
while(curr != 0) {
if(curr -> name == inputName) {
break;
}
else {
trail = curr;
curr = curr -> next;
}
}
//case2 -Node with name "inputName" not found in list
if(curr == 0) {
cout << "Contact with name: " << inputName << " not found." << endl;
}
else {
//case3 - delete from the head
if(head == curr) {
head = head -> next;
}
// case 4 - delete beyond head
else {
trail -> next = curr ->next;
}
delete curr;
length--; //别忘了
}
}
}
测试应用程序改动如下:
#include "ContactList.h"
using namespace std;
int main() {
ContactList* cl1 = new ContactList;
string name;
while(true) {
cout << "Enter the name of the contact, or q to quit:";
cin >> name;
if(name == "q")
break;
cl1->insert(name); // so that we can have a sorted linked list
}
while(true) {
cout << "Enter the name of the contact to delete, or q to quit:";
cin >> name;
if(name == "q")
break;
cl1-> deleteNode(name); // so that we can have a sorted linked list
}
cl1 -> printList();
return 0;
}
运行结果如下: