注意:以下所有鏈表均包含空的頭結點。
1 單鏈表(SingleLinkedList)
1.1 代碼
#include "stdafx.h"
#include <iostream>
using namespace std;
//節點類
class Node {
public:
int data;
Node * next;
//兩個參數的構造函數
Node(const int info, Node * nextValue) {
data = info;
next = nextValue;
}
//一個參數的構造函數
Node(Node * nextValue) {
next = nextValue;
}
};
//單鏈表類
class SingleLinkedList {
private:
Node *head, *tail;
//返回線性表指向第p個元素的[指針值]
Node *setPos(const int i) {
int count = 0;
if (i == -1) {
return head;
}
Node *p = new Node(head->next);
while (p != NULL && count < i) {
p = p->next;
count++;
}
return p;
}
public:
SingleLinkedList() {
head = tail = new Node(NULL);
}
~SingleLinkedList() {
Node *tmp;
while (head != NULL) {
tmp = head;
head = head->next;
delete tmp;
}
}
//判斷鏈表是否爲空
bool isEmpty() {
if (head->next == NULL)
return true;
else
return false;
}
//將鏈表內容清除
void clear() {
Node *tmp;
while (head != NULL) {
tmp = head;
head = head->next;
delete tmp;
}
head = tail = new Node(NULL);
}
//返回當前鏈表長度
int length() {
int count = 0;
Node *p = new Node(head->next);
while (p != NULL) {
p = p->next;
count++;
}
return count;
}
//在表尾添加一元素
bool append(const int value) {
Node *p = new Node(value, tail->next);
tail->next = p;
tail = p; //tail重置
return true;
}
//在位置i上插入一個元素value
bool insert(const int i, const int value) {
Node *p, *q;
if ((p = setPos(i - 1)) == NULL) {
cout << "Illegle location of insertion!" << endl;
return false;
}
q = new Node(value, p->next); //q是待插入元素
p->next = q;
if (p == tail) //插入點在鏈尾
tail = q;
return true;
}
//在位置i上刪除該元素
bool drop(const int i) {
Node *p, *q;
if ((p = setPos(i - 1)) == NULL || p == tail) {
cout << "Illegle location of delete operation!" << endl;
return false;
}
q = p->next; //q是待刪除元素
if (q == tail) {
tail = p;
p->next = NULL;
delete q;
}
else if (q != NULL) {
p->next = q->next;
delete q;
}
}
//通過value返回位置p的元素值
bool getValue(const int p, int & value) {
if (p == -1) {
value = NULL;
return false;
}
Node *q = head;
for (int i = 0; i < p; i++) {
q = q->next;
}
value = q->data;
return true;
}
//查找值爲value的元素並通過p返回其第一次出現時的位置
bool getPos(int &p, const int value) {
Node *q;
p = 0;
for (q = head; q != NULL; q = q->next, ++p) {
if (q->data == value) {
return true;
break;
}
}
return false;
}
};
1.2 測試
int main()
{
SingleLinkedList test;
int tmp;
if (test.isEmpty())
cout << "Empty linked list." << endl;
else
cout << "Linked list is Not Empty." << endl;
cout << "Length: " << test.length() << endl; //空表頭,長度一開始就是1
test.append(1);
test.append(2);
test.append(3);
test.getValue(0, tmp);
cout << "Value: " << tmp << endl;
test.getValue(1, tmp);
cout << "Value: " << tmp << endl;
test.getValue(2, tmp);
cout << "Value: " << tmp << endl;
test.getValue(3, tmp);
cout << "Value: " << tmp << endl;
if (test.isEmpty())
cout << "Empty linked list." << endl;
else
cout << "Linked list is Not Empty." << endl;
cout << "Length: " << test.length() << endl;
test.insert(2, 4);
test.getPos(tmp, 4);
cout << "Positon: " << tmp << endl;
test.getValue(2, tmp);
cout << "Value: " << tmp << endl;
test.drop(2);
return 0;
}
1.3 輸出
Empty linked list.
Length: 1
Value: 0
Value: 1
Value: 2
Value: 3
Linked list is Not Empty.
Length: 4
Positon: 2
Value: 4
2 雙鏈表(DoubleLinkedList)
這部分代碼相較於1.1節的內容修改不大,主要變化體現在insert()
和drop()
方法內部以及類DoubleLinkedList
中函數的形參變化上。
測試用例和輸出結果同1.2和1.3節,故省略。
#include "stdafx.h"
#include <iostream>
using namespace std;
//節點類
class Node {
public:
int data;
Node * next;
Node * prev;
//包含值和前後指針三個參數的構造函數
Node(const int info, Node * preValue, Node * nextValue) {
data = info;
prev = preValue;
next = nextValue;
}
//包含前後指針兩個參數的構造函數
Node(Node * preValue, Node * nextValue) {
prev = preValue;
next = nextValue;
}
};
//雙鏈表類
class DoubleLinkedList {
private:
Node *head, *tail;
//返回線性表指向第p個元素的[指針值]
Node *setPos(const int i) {
int count = 0;
if (i == -1) {
return head;
}
Node *p = new Node(head, head->next);
while (p != NULL && count < i) {
p = p->next;
count++;
}
return p;
}
public:
DoubleLinkedList() {
head = tail = new Node(NULL, NULL);
}
~DoubleLinkedList() {
Node *tmp;
while (head != NULL) {
tmp = head;
head = head->next;
delete tmp;
}
}
//判斷鏈表是否爲空
bool isEmpty() {
if (head->next == NULL)
return true;
else
return false;
}
//將鏈表內容清除
void clear() {
Node *tmp;
while (head != NULL) {
tmp = head;
head = head->next;
delete tmp;
}
head = tail = new Node(NULL, NULL);
}
//返回當前鏈表長度
int length() {
int count = 0;
Node *p = new Node(head, head->next);
while (p != NULL) {
p = p->next;
count++;
}
return count;
}
//在表尾添加一元素
bool append(const int value) {
Node *p = new Node(value, tail, tail->next);
tail->next = p;
tail = p; //tail重置
return true;
}
//在位置i上插入一個元素value
bool insert(const int i, const int value) {
Node *p, *q;
if ((p = setPos(i - 1)) == NULL) {
cout << "Illegle location of insertion!" << endl;
return false;
}
q = new Node(value, p, p->next); //q是待插入元素
q->prev = p;
q->next = p->next;
p->next = q;
q->prev->next = q;
if (p == tail) //插入點在鏈尾
q = tail;
return true;
}
//在位置i上刪除該元素
bool drop(const int i) {
Node *p; //p本身就是待刪除元素
if ((p = setPos(i - 1)) == NULL || p == tail) {
cout << "Illegle location of delete operation!" << endl;
return false;
}
if (p == tail) {
tail = p->prev;
p->prev->next = NULL;
delete p;
}
else if (p != NULL) {
p->prev->next = p->next;
p->next->prev = p->prev;
p->prev = NULL;
p->next = NULL;
delete p;
}
}
//通過value返回位置p的元素值
bool getValue(const int p, int & value) {
if (p == -1) {
value = NULL;
return false;
}
Node *q = head;
for (int i = 0; i < p; i++) {
q = q->next;
}
value = q->data;
return true;
}
//查找值爲value的元素並通過p返回其第一次出現時的位置
bool getPos(int &p, const int value) {
Node *q;
p = 0;
for (q = head; q != NULL; q = q->next, ++p) {
if (q->data == value) {
return true;
break;
}
}
return false;
}
};