简体   繁体   English

我可以通过 c++ 中继承的 class 对链表进行排序吗?

[英]can I sort a linked-list by an inherited class in c++?

I implemented a linked list in a class with addToHead, addToTail, deleteFromHead, deleteFromTail, isEmpty, and display functions, and I want to sort the linked list in ascending order in an inherited class, so I made a class TSortedList with two functions; I implemented a linked list in a class with addToHead, addToTail, deleteFromHead, deleteFromTail, isEmpty, and display functions, and I want to sort the linked list in ascending order in an inherited class, so I made a class TSortedList with two functions; the sortList function that compares the elements of the linked list with each other, and the display() function that display the linked list after sorting it. sortList function 将链表的元素相互比较,display() function 在排序后显示链表。 But when I run the code nothing appear to me, however when I sort it through a function in the parent class, not the inherited class it works, so I do not know where is the problem但是当我运行代码时,我似乎什么都没有,但是当我通过父 class 中的 function 对它进行排序时,而不是继承的 class 它起作用了,所以我不知道问题出在哪里

#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;
   }

Your TSortedList sortt;你的TSortedList sortt; is empty;是空的; you never add anything to it.你永远不会添加任何东西。

What do you expect it to display?您希望它显示什么?

This should work as you expected:这应该可以按您的预期工作:

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;
}

Also, why are you deriving your TSortedList from Node?另外,为什么要从 Node 派生TSortedList Node?

class TSortedList: public LList, public Node

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM