簡體   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; sortList function 將鏈表的元素相互比較,display() function 在排序后顯示鏈表。 但是當我運行代碼時,我似乎什么都沒有,但是當我通過父 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;
   }

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

另外,為什么要從 Node 派生TSortedList Node?

class TSortedList: public LList, public Node

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM