简体   繁体   English

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

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

So I'm trying to build a linear linked list that takes info from users and saves the info in two sorted lists by name (alphabetically) and by birthdate. 因此,我正在尝试构建一个线性链接列表,该列表从用户那里获取信息,并将信息按名称(按字母顺序)和生日日期保存在两个排序的列表中。 So far I have 到目前为止,我有

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

where each node will have two pointers pointing to the appropriate list. 每个节点都有两个指向相应列表的指针。 The problem I'm having is how to direct the head pointer node *head. 我遇到的问题是如何定向头指针节点* head。 How do I set head when there are two different lists? 有两个不同的清单时,如何设置头? I'm thinking something like head->nameNext and head->dateNext but that would point to the second node of the lists if it work. 我在想像head-> nameNext和head-> dateNext之类的东西,但是如果可以的话,它将指向列表的第二个节点。 Please help! 请帮忙! Thanks in advance. 提前致谢。

if i got your question right, you're simply looking to sort your list in two ways (alphabetically and birthdate) note: i will use bubble sort to simplify the algorithm but you can use better one as you know 如果我的问题正确,那么您只是想以两种方式(字母顺序和生日)对列表进行排序注意:我将使用冒泡排序来简化算法,但是您可以使用更好的一种

#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();



}

note: because you're using C++ don't use char* to store string literals use std::string or const char *. 注意:因为您使用的是C ++,所以不要使用char *来存储字符串文字,请使用std :: string或const char *。 as the chars in string literals are const char so you don't want to point on const char with char if you're using a C++ compiler that support C++11 your compiler should generate a warning about such thing 因为字符串文字中的char是const char,所以如果您使用的是支持C ++ 11的C ++编译器,则不想用char指向const char。

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

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