Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C++中使用线性链表排序_C++ - Fatal编程技术网

在C++中使用线性链表排序

在C++中使用线性链表排序,c++,C++,所以我试图建立一个线性链表,从用户那里获取信息,并按姓名字母顺序和出生日期将信息保存在两个排序列表中。到目前为止我有 struct node{ char* name; int birthDate; node *nameNext; node * dateNext; }; 其中每个节点将有两个指针指向相应的列表。我遇到的问题是如何引导head指针节点*head。当有两个不同的列表时,我该如何设置头部?我在想head->nameN

所以我试图建立一个线性链表,从用户那里获取信息,并按姓名字母顺序和出生日期将信息保存在两个排序列表中。到目前为止我有

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

其中每个节点将有两个指针指向相应的列表。我遇到的问题是如何引导head指针节点*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*。因为字符串文本中的字符是常量字符,所以您不希望用字符指向常量字符


如果你使用C++编译器支持C++ 11,编译器应该对这个东西提出警告:你需要两个磁头。但是你真的想把节点从列表中分离出来。std::sort和std::vector?在99%的情况下,链表无论如何都会慢一些。