- 实验目的
-
- 掌握有序链表的基本操作:插入、删除、查找。
- 掌握链表遍历器的使用方法。
- 禁止采用先数组排序,然后了对排序的数组创建链表办法。
-
- 实验内容
- 输入n个不为零的整数作为节点元素值,遇到0代表输入结束(不创建元素值为0的节点),创建第一个有序链表。输出整个链表。如果输入的第一个数为0,则创建空链表(没有任何元素的链表)。如果是空链表,则输出内容为<null>(以下相同)。
- 再次输入n个不为零的整数作为节点元素值,遇到0代表输入结束(不创建元素值为0的节点),创建第二个有序链表。输出整个链表。
- 使用链表遍历器实现上面两个有序链表的合并,输出合并后的有序链表。
- 输入一个整数,将该数插入到合并后的有序链表正确位置,输出整个链表。
- 输入一个整数,在链表中查找这个数,找到第一个后将该节点删除,输出整个链表。
- 输入一个整数,在合并后的链表中进行搜索,输出其在链表中的第一次出现的位置。如果不存在输出0。
#include <bits/stdc++.h>
using namespace std;
struct chainNode {
int element;
chainNode *next;
chainNode() {
element = NULL;
next = NULL;
}
chainNode(const int &element) {
this->element = element;
this->next = NULL;
}
chainNode(const int &element, chainNode *next) {
this->element = element;
this->next = next;
}
};
class chain {
public:
chain(int capacity = 10) {
firstNode = NULL;
listSize = 0;
}
void insert(const int &iElement);
void output() const;
void find(int iElement);
void deletion(int &iElement);
public:
chainNode *firstNode;
int listSize;
};
void chain::output() const {
if (listSize == 0) {
cout << "<null>" << endl;
} else {
cout << firstNode->element;
chainNode *temp = firstNode->next;
while (temp) {
cout << "," << temp->element;
temp = temp->next;
}
cout << endl;
}
}
void chain::find(int iElement) {
if (listSize == 0) {
cout << 0 << endl;
return;
} else {
int index = 1;
chainNode *temp = firstNode;
while (temp) {
if (temp->element == iElement) {
cout << index << endl;
return;
}
temp = temp->next;
index++;
}
cout << 0 << endl;
}
}
void chain::deletion(int &iElement) {
if (listSize == 0) {
return;
} else if (firstNode->element == iElement) {
firstNode = firstNode->next;
listSize--;
return;
} else {
chainNode *temp = firstNode;
while (temp) {
if (temp->next->element == iElement) {
temp->next = temp->next->next;
return;
}
temp = temp->next;
}
listSize--;
}
}
void chain::insert(const int &iElement) {
chainNode *temp = new chainNode(iElement);
if (listSize == 0) {
firstNode = temp;
} else {
chainNode *node = firstNode;
chainNode *p;
int index = 0;
for (; index < listSize && node->element < iElement; index++) {
p = node;
node = node->next;
}
if (index == 0) {
temp->next = node;
firstNode = temp;
} else {
p->next = temp;
temp->next = node;
}
}
listSize++;
}
void combine(chain &list1, chain &list2) {
if (list1.listSize == 0 && list2.listSize == 0) {
cout << "<null>" << endl;
return;
} else if (list1.listSize == 0) {
list1 = list2;
} else if (list2.listSize == 0) {
list1.output();
return;
} else {
chainNode *p = list1.firstNode;
chainNode *q = list2.firstNode;
chainNode *temp;
while (p->element > q->element) {
temp = p;
p = q;
q = q->next;
p->next = temp;
}
while (p->next && q->next) {
if (p->next->element < q->element) {
p = p->next;
} else {
temp = q;
q = q->next;
temp->next = p->next;
p->next = temp;
p = p->next;
}
}
if (q->next) {
p->next = q->next;
}
list1.listSize += list2.listSize;
list1.insert(q->element);
list1.output();
}
}
int main() {
cout << "Input1" << endl;
chain list1(10);
int elmt = 0;
cin >> elmt;
while (elmt) {
list1.insert(elmt);
cin >> elmt;
}
cout << "Output1" << endl;
list1.output();
cout << "Input2" << endl;
chain list2(10);
cin >> elmt;
while (elmt) {
list2.insert(elmt);
cin >> elmt;
}
cout << "Output2" << endl;
list2.output();
cout << "Combine" << endl;
combine(list1, list2);
cout << "Insert" << endl;
cin >> elmt;
list1.insert(elmt);
cout << "Insertion" << endl;
list1.output();
cout << "Delete" << endl;
cin >> elmt;
list1.deletion(elmt);
cout << "Deletion" << endl;
list1.output();
cout << "Find" << endl;
cin >> elmt;
cout << "Position" << endl;
list1.find(elmt);
cout << "End";
return 0;
}