c++ - 我可以通过 C++ 中的继承类对链表进行排序吗?
问题描述
我在一个有addToHead、addToTail、deleteFromHead、deleteFromTail、isEmpty和显示函数的类中实现了一个链表,我想在继承的类中对链表进行升序排序,所以我做了一个有两个函数的类TSortedList;sortList 函数将链表的元素相互比较, display() 函数在排序后显示链表。但是当我运行代码时,什么都没有出现,但是当我通过父类中的函数而不是继承的类对其进行排序时,它可以工作,所以我不知道问题出在哪里
#include <iostream>
using namespace std;
class Node {
public:
Node() {
next = 0;
//write your modification here
}
Node(int el, Node *ptr = 0) { //write your modification for the constructor arguments here
info = el;
next = ptr;
}
int info;
Node *next;
//write your modification here
};
class LList {
protected:
Node *head, *tail;
public:
LList() {
head = tail = 0;
}
~LList(){
for (Node *p; !isEmpty(); ) {
p = head->next;
delete head;
head = p;
}
}
int isEmpty() {
return head == 0;
}
virtual void addToHead(int el){
head = new Node(el,head);
if (tail == 0)
tail = head;
}
virtual void addToTail(int el){
if (tail != 0) { // if list not empty;
tail->next = new Node(el);
}
else head = tail = new Node(el);
}
int deleteFromHead(){ // delete the head and return its info;
int el = head->info;
Node *tmp = head;
if (head == tail) // if only one node in the list;
head = tail = 0;
else head = head->next;
delete tmp;
return el;
}
int deleteFromTail(){ // delete the tail and return its info;
int el = tail->info;
if (head == tail) { // if only one node in the list;
delete head;
head = tail = 0;
}
else { // if more than one node in the list,
Node *tmp; // find the predecessor of tail;
for (tmp = head; tmp->next != tail; tmp = tmp->next);
delete tail;
tail = tmp; // the predecessor of tail becomes tail;
tail->next = 0;
}
return el;
}
bool isInList(int el) const{
Node *tmp;
for (tmp = head; tmp != 0 && !(tmp->info == el); tmp = tmp->next);
return tmp != 0;
}
virtual void displayList(){
if (head == 0) // if empty list;
return;
Node *tmp = head;
for (tmp = head; tmp != 0; tmp = tmp->next){
cout<<tmp->info<<endl;
}
}
};
class TSortedList: public LList, public Node
{
protected:
Node *current = head, *index = 0;
int temp;
public:
void sortList() {
if(head == 0) {
return;
}
else {
while(current != 0) {
//Node index will point to node next to current
index = current->next;
while(index != 0) {
//If current node's data is greater than index's node data, swap the data betweenthem
if(current->info > index->info) {
temp = current->info;
current->info = index->info;
index->info = temp;
}
index = index->next;
}
current = current->next;
}
}
}
void display() {
//Node current will point to head
Node *current = head;
if(head == 0) {
return;
}
while(current != 0) {
//Prints each node by incrementing pointer
cout<<current->info<<endl;
current = current->next;
}
cout<<"\n"<<endl;
}
};
int main()
{
//Adds data to the list
LList myList;
myList.addToHead(1);
myList.addToHead(7);
myList.addToHead(3);
TSortedList sortt;
sortt.sortList();
sortt.display();
return 0;
}
解决方案
你TSortedList sortt;
是空的;你永远不会添加任何东西。
您希望它显示什么?
这应该可以按您的预期工作:
int main()
{
//Adds data to the list
TSortedList myList;
myList.addToHead(1);
myList.addToHead(7);
myList.addToHead(3);
myList.display(); // this should be in original order
myList.sortList();
myList.display(); // this should be sorted
return 0;
}
另外,你为什么TSortedList
要从Node?
class TSortedList: public LList, public Node
推荐阅读
- java - @ConditionalOnBean 不能与千分尺 MeterRegistry 一起正常工作
- batch-file - 如何让 .vbs 或 .bat 文件成为垃圾照片?
- php - Woocommerce 要求提供已提供的电子邮件
- r - 直接通过 R 中的一些数据点绘制曲线(ggplot)
- apache - 我无法在任何 linux vm 机器上播放受 DRM 保护的破折号视频,但相同的代码正在其他共享托管 apache 服务器上运行
- java - 创建项目后 Eclipse IDE 无法打开
- python - 如何将类别添加到命令 discord.py
- python - tkinter 即使在删除后仍将 \n 保留在文本小部件中
- azure-devops - 将 SSIS 添加到 ADO 构建服务器以进行构建和部署
- c++ - 返回/打印所选文件的路径