首页 > 解决方案 > 我可以通过 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;
   }

标签: c++inheritancesingly-linked-list

解决方案


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

推荐阅读