一. 链表(Linked-List):
链表(linked list) :由一系列节点构成,每个节点满足以下条件:
- 包含一段数据(任意类型);
- 指向链表的下一个节点
表头(Head):指向第一个节点
最后一个节点指向 NULL
定义一个链表可以定义两种结构体:Node 和 List
二. 基本操作(Basic Operation):
- 新建一个链表(constructor):List(void);
- 摧毁一个链表(destructor):~List(void);
- 判断链表是否为空(determine whether or not the list is empty):bool IsEmpty();
- 向链表中输入n个指定节点(insert n nodes to build a new list):void CreateList(int n);
- 向链表中插入节点(insert a new node at a particular position):int InsertNode(int index, double x);
- 查找指定数据所在位置(find a node with a given value):int FindNode(double x);
- 删除指定数据返回其所在位置(delete a node with a given value):int DeleteNode(double x);
- 删除所有节点(delete all of the node):void DeleteAll();
- 遍历所有节点(print all the nodes in the list):void DisplayList();
三. C++代码:
#include <iostream>
using namespace std;
class Node {
public:
double data; // data
Node* next; // pointer to next
};
class List {
public:
List(void);// constructor
~List(void);// destructor
bool IsEmpty();
void CreateList(int n);
int InsertNode(int index, double x);
int FindNode(double x);
int DeleteNode(double x);
void DeleteAll();
void DisplayList();
private:
Node* head;
};
List::List(void) {
head = new Node;
}
List::~List(void) {
delete head;
}
bool List::IsEmpty() {
if (head->next == NULL) {
return true;
}
return false;
}
void List::CreateList(int n) {
Node* currNode = head;
cout << "Type the " << 1 << "st value: " ;
cin >> currNode->data;
for (int i = 1; i < n;i++) {
Node* newNode = new Node;
cout << "Type the " << i + 1 << "th value: " ;
cin >> newNode->data;
newNode->next = NULL;
currNode->next = newNode;
currNode = newNode;
}
}
int List::InsertNode(int index, double x) {
if(index < 0)
return -1;
int currIndex = 1;
Node* currNode = head;
while(currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if(index > 0 && currNode == NULL)
return -1;
Node* newNode = new Node;
newNode->data = x;
if(index == 0) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return 0;
}
int List::FindNode(double x) {
Node* currNode = head;
int currIndex = 1;
while(currNode && currNode->data != x) {
currNode = currNode->next;
currIndex++;
}
if(currNode)
return currIndex-1;
return -1;
}
int List::DeleteNode(double x) {
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while(currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if(currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else{
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
void List::DeleteAll()
{
Node* currNode = head->next;
Node* newNode = new Node;
while (currNode != NULL)
{
newNode = currNode;
currNode = currNode->next;
head->next = currNode;
newNode->next = NULL;
delete newNode;
}
head = NULL;
}
void List::DisplayList() {
int num = 0;
Node* currNode = head;
while(currNode != NULL){
cout << currNode->data << endl;
currNode = currNode->next;
num++;
}
cout<< "Number of nodes in the list: " << num << endl;
}
int main(void){
List list;
list.CreateList(3);
list.DisplayList();
list.DeleteAll();
list.DisplayList();
list.InsertNode(0, 10.0);
list.InsertNode(1, 5.0);
list.InsertNode(-1, 5.0);
list.InsertNode(0, 6.0);
list.InsertNode(8, 4.0);
list.DisplayList();// print all the elements
if(list.FindNode(5.0) >= 0)
cout << "5.0 found" << endl;
else
cout << "5.0 not found" << endl;
if(list.FindNode(4.5) >= 0)
cout << "4.5 found" << endl;
else
cout << "4.5 not found" << endl;
list.DeleteNode(10.0);
list.DisplayList();
return 0;
}