编写程序link.h实现例9-6的链表类。在测试程序lab9_2.cpp中声明两个整型链表A和B,分别插入5个元素,然后把B中的元素加入A的尾部。
时间: 2024-03-05 21:51:14 浏览: 206
以下是link.h的程序代码:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
struct node {
int data;
node *next;
node(int x, node *t = nullptr) {
data = x;
next = t;
}
node(): next(nullptr) {}
~node() {}
};
class link {
private:
node *head, *tail;
int len;
public:
link() {
head = tail = nullptr;
len = 0;
}
~link() {
while (head != nullptr) {
node *p = head;
head = head->next;
delete p;
}
tail = nullptr;
len = 0;
}
int length() const {
return len;
}
bool empty() const {
return head == nullptr;
}
int front() const {
if (empty()) {
cerr << "List is empty!" << endl;
exit(EXIT_FAILURE);
}
return head->data;
}
int back() const {
if (empty()) {
cerr << "List is empty!" << endl;
exit(EXIT_FAILURE);
}
return tail->data;
}
void push_front(const int& x) {
head = new node(x, head);
if (tail == nullptr) {
tail = head;
}
++len;
}
void push_back(const int& x) {
node *p = new node(x);
if (tail == nullptr) {
head = tail = p;
} else {
tail->next = p;
tail = p;
}
++len;
}
void pop_front() {
if (empty()) {
cerr << "List is empty!" << endl;
exit(EXIT_FAILURE);
}
node *p = head;
head = head->next;
delete p;
--len;
if (empty()) {
tail = nullptr;
}
}
void pop_back() {
if (empty()) {
cerr << "List is empty!" << endl;
exit(EXIT_FAILURE);
}
if (head == tail) {
delete tail;
head = tail = nullptr;
len = 0;
return;
}
node *p = head;
while (p->next != tail) {
p = p->next;
}
delete tail;
tail = p;
tail->next = nullptr;
--len;
}
void insert(int k, const int& x) {
if (k < 0 || k > len) {
cerr << "Invalid index!" << endl;
exit(EXIT_FAILURE);
}
if (k == 0) {
push_front(x);
return;
}
if (k == len) {
push_back(x);
return;
}
node *p = head;
for (int i = 0; i < k - 1; ++i) {
p = p->next;
}
p->next = new node(x, p->next);
++len;
}
void erase(int k) {
if (k < 0 || k >= len) {
cerr << "Invalid index!" << endl;
exit(EXIT_FAILURE);
}
if (k == 0) {
pop_front();
return;
}
if (k == len - 1) {
pop_back();
return;
}
node *p = head;
for (int i = 0; i < k - 1; ++i) {
p = p->next;
}
node *q = p->next;
p->next = q->next;
delete q;
--len;
}
void print() const {
node *p = head;
while (p != nullptr) {
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
void merge(link &B) {
if (B.empty()) {
return;
}
if (empty()) {
head = B.head;
tail = B.tail;
len = B.len;
B.head = B.tail = nullptr;
B.len = 0;
return;
}
tail->next = B.head;
tail = B.tail;
len += B.len;
B.head = B.tail = nullptr;
B.len = 0;
}
};
```
在lab9_2.cpp中声明两个整型链表A和B,分别插入5个元素,然后把B中的元素加入A的尾部的程序如下:
```c++
#include <iostream>
#include "link.h"
using namespace std;
int main() {
srand(time(0));
link A, B;
for (int i = 0; i < 5; ++i) {
int x = rand() % 100 + 1;
A.push_back(x);
}
for (int i = 0; i < 5; ++i) {
int x = rand() % 100 + 1;
B.push_back(x);
}
cout << "A: ";
A.print();
cout << "B: ";
B.print();
A.merge(B);
cout << "After merging B into A:" << endl;
cout << "A: ";
A.print();
cout << "B: ";
B.print();
return 0;
}
```
其中,srand(time(0))是为了每次运行程序时生成的随机数不同。在测试时,可以将其注释掉。
阅读全文