簡體   English   中英

在C ++中使用線性鏈表進行排序

[英]Sorting using Linear Linked List in C++

因此,我正在嘗試構建一個線性鏈接列表,該列表從用戶那里獲取信息,並將信息按名稱(按字母順序)和生日日期保存在兩個排序的列表中。 到目前為止,我有

struct node{ 
       char* name; 
       int birthDate; 
       node *nameNext; 
       node * dateNext;
  }; 

每個節點都有兩個指向相應列表的指針。 我遇到的問題是如何定向頭指針節點* head。 有兩個不同的清單時,如何設置頭? 我在想像head-> nameNext和head-> dateNext之類的東西,但是如果可以的話,它將指向列表的第二個節點。 請幫忙! 提前致謝。

如果我的問題正確,那么您只是想以兩種方式(字母順序和生日)對列表進行排序注意:我將使用冒泡排序來簡化算法,但是您可以使用更好的一種

#include <iostream>


struct node{
    const char* name;
    int birthdate;
    node*next;
};

struct sort_data{
private:
    node *name_root = nullptr; // alphabetically head/root pointer
    node *date_root = nullptr; // birthdate head/root pointer
public:
    void push(const char*name,int birthdate); // push data;
    void sort_by_birth(); // sort the birth linked list
    void sort_by_alphabet(); // sort the alphabet linked list
    void print_birth(); // print the data of the birth linked list
    void print_alph(); // print of the data of the alphabet linked list
};


void sort_data::push(const char*name,int birthdata) {
    node*Name = new node; // allocate a node for the alphabet list
    node*Date = new node; // allocate a node for the date list

    Name->name = Date->name = name; 
    Name->birthdate = Date->birthdate = birthdata;
    Name->next = name_root;
    Date->next = date_root;

    name_root = Name;
    date_root = Date;

}

void sort_data::sort_by_birth() {
    node*i = date_root;
    node*j;

    if(!i) // if i == nullptr 
        return;

    while(i){ // while(i!=nullptr)
        j = i->next;
        while(j){
            if(i->birthdate > j->birthdate){
                std::swap(i->birthdate,j->birthdate);
                std::swap(i->name,j->name);
            }
            j = j->next;
        }
        i = i->next;
    }
}

void sort_data::sort_by_alphabet() {

    node*i = name_root;
    node*j;

    if(!i)
        return;

    while(i){
        j = i->next;
        while(j){
            if(i->name[0] > j->name[0]){
                std::swap(i->birthdate,j->birthdate);
                std::swap(i->name,j->name);
            }
            j = j->next;
        }
        i = i->next;
    }
}

void sort_data:: print_birth(){
    node*temp = date_root;

    while(temp){
        std::cout << temp->name << " " << temp->birthdate << std::endl;
        temp = temp->next;
    }

}

void sort_data::print_alph() {

    node*temp = name_root;

    while(temp){
        std::cout << temp->name << " " << temp->birthdate << std::endl;
        temp = temp->next;
    }

}




int main(){


    sort_data obj;
    obj.push("jack",1997);
    obj.push("daniel",1981);
    obj.push("maria",1995);
    obj.push("john",2008);
    obj.sort_by_alphabet();
    obj.sort_by_birth();
    std::cout << "alphabetically : \n" ;
    obj.print_alph();

    std::cout << "by birthdate : \n";
    obj.print_birth();



}

注意:因為您使用的是C ++,所以不要使用char *來存儲字符串文字,請使用std :: string或const char *。 因為字符串文字中的char是const char,所以如果您使用的是支持C ++ 11的C ++編譯器,則不想用char指向const char。

暫無
暫無

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

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