根据 listnode.h 中对链表 listNode 的定义,在 orderedList.hpp 中实现以下三个类: 抽象基类 OrderedList,表示一个整数链表,有以下方法: 构造函数:无参数,创建一个空的链表; void insert(int val) 向链表中插入一个元素,在 OrderedList 中该方法应当为纯虚函数; void printList() const 依次在屏幕上输出链表中的元素,元素之间用空格分隔,输出完整个链表后换行; 析构函数:释放链表所占用的空间。 此外,OrderedList还有一个保护成员:listNode * root; 具体类 AscendOrderedList,表示一个升序的链表,继承自 OrderedList,需要重载 void insert(int val) 函数,在插入的时候实现升序。 具体类 DescendOrderedList,表示一个降序的链表,继承自 OrderedList,需要重载 void insert(int val) 函数,在插入的时候实现降序。
时间: 2024-03-10 13:50:45 浏览: 36
好的,根据您的要求,我已经理解了您的问题。以下是 orderedList.hpp 文件的实现代码:
```
#ifndef ORDEREDLIST_H
#define ORDEREDLIST_H
#include "listNode.h"
class OrderedList {
protected:
listNode *root;
public:
OrderedList(): root(nullptr) {}
virtual ~OrderedList() {
while (root != nullptr) {
listNode *temp = root;
root = root->next;
delete temp;
}
}
virtual void insert(int val) = 0;
virtual void printList() const {
listNode *temp = root;
while (temp != nullptr) {
std::cout << temp->val << " ";
temp = temp->next;
}
std::cout << std::endl;
}
};
class AscendOrderedList : public OrderedList {
public:
void insert(int val) override {
listNode *newNode = new listNode(val);
if (root == nullptr || root->val > val) {
newNode->next = root;
root = newNode;
} else {
listNode *temp = root;
while (temp->next != nullptr && temp->next->val <= val) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
}
};
class DescendOrderedList : public OrderedList {
public:
void insert(int val) override {
listNode *newNode = new listNode(val);
if (root == nullptr || root->val < val) {
newNode->next = root;
root = newNode;
} else {
listNode *temp = root;
while (temp->next != nullptr && temp->next->val >= val) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
}
};
#endif
```
此代码实现了抽象基类 OrderedList,以及派生类 AscendOrderedList 和 DescendOrderedList。其中,AscendOrderedList 和 DescendOrderedList 分别实现了 insert 函数,以实现升序和降序的插入功能。同时,它们也继承了 OrderedList 的 printList 函数,以便输出整个链表中的元素。
需要注意的是,OrderedList 的析构函数中释放了链表所占用的空间,避免了内存泄漏的问题。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)