简体   繁体   English

C++ 双向链表按字母顺序和值添加节点

[英]C++ Doubly linked list adding node in alphabetical order and value

My task is a create doubly linked list and sort them according to their data, if new written node's data is equal to the one of the nodes' data in my doubly linked list, i should sort them in a alphabetical order but I am stuck in the function strcmp(temp->name, newnode->name) , For example, I am trying to check if these values entered in order我的任务是创建双向链表并根据它们的数据对它们进行排序,如果新写入的节点的数据等于我的双向链表中的节点数据之一,我应该按字母顺序对它们进行排序,但我被困在function strcmp(temp->name, newnode->name) ,例如,我试图检查这些值是否按顺序输入

  1. Christian 250克里斯蒂安 250
  2. Tom 200汤姆 200
  3. Alex 250亚历克斯 250

My sorted doubly linked list give the output as我的排序双向链表将 output 设为

  1. Alex 250亚历克斯 250
  2. Christian 250克里斯蒂安 250
  3. Tom 200汤姆 200

Here is example of my code这是我的代码示例

   struct node {
    int data;
    string name;
    node* left;
    node* right;

    node(int i = 0, string s = "", node* l = nullptr, node* r = nullptr) : data(i), name(s), left(l), right(r) {}
};

struct node* head = NULL;
struct node* tail = NULL;

at the top of the program在程序的顶部

void insert(int newdata, string name)  // Used for insertion at the end of the linked list and make 
{                                      // the connection for each of the node
    node* newnode = new node();
    node* nodehead = head;
    newnode->data = newdata;
    newnode->name = name;
    newnode->right = NULL;

    if( head == NULL)
    {
        newnode -> left = NULL;
        head = tail = newnode;
    }
    else 
    {
        while (nodehead->right != NULL)
        {
            nodehead = nodehead->right;     
        }
            nodehead->right = newnode; // make the connection for each of them
            newnode->left = nodehead;
            tail = newnode; // and newly created node is our tail

            sorting(newnode); // then go the function to sort them 
    }
   
    cout << "New node is added " << newnode->name << " " << newnode->data << endl;
}

then sort them based on their comparison of their data and if they are equal i should check based on their alphabetical order然后根据他们对数据的比较对它们进行排序,如果它们相等,我应该根据它们的字母顺序检查

 void sorting( node *  &newnode) // ı call sorting function in the insertion function
    {
        node* temp = head;
        node* temp2 = newnode;
    
        int numtemp;
    
        while (temp != nullptr)
        {
            node* temp2 = temp->right;
            while (temp2 != nullptr)
            {       
                if (temp2->data > temp->data) // comparison of their data if newnode's data is larger 
                {
                    string strtemp = "";
                    numtemp = temp->data; // Change their number
                    temp->data = temp2->data;
                    temp2->data = numtemp;
    
                    strtemp = temp->name; // Change their name
                    temp->name = temp2->name;
                    temp2->name = strtemp;
                }
                else if (temp2->data = temp->data) // if newly data is equal to linked list data
                {
                    int ret;
                    ret =  strcmp(temp2->name, temp->name); // i tried to check their string comparison but it did not work
                }
                temp2 = temp2->right;
            }
            temp = temp->right;
        }        
    }

I'd recommend that you define a comparison on the node itself, and use that in sorting.我建议您在节点本身上定义一个比较,并在排序中使用它。 Keep in mind that if you are comparing through pointers, you'll need to dereference (compare the nodes, not the pointers).请记住,如果您通过指针进行比较,则需要取消引用(比较节点,而不是指针)。

The standard library already has a way to do comparison based on multiple terms.标准库已经有一种基于多个术语进行比较的方法。 https://en.cppreference.com/w/cpp/utility/tuple/tie https://en.cppreference.com/w/cpp/utility/tuple/tie

using std::tie ensures that you meet https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings使用 std::tie 确保您满足https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings

I am, of course, assuming that std::string's comparison meets your need for alphabetical.当然,我假设 std::string 的比较满足您对字母顺序的需求。 If that's not the case, this may need a minor tweek.如果不是这样,这可能需要一个小周。

#include <string>
#include <iostream>

struct node {
    int data;
    std::string name;
    node* left;
    node* right;

    node(int i = 0, std::string s = "", node* l = nullptr, node* r = nullptr) 
        : data(i), name(s), left(l), right(r) 
    {}

    bool operator< (const node & rhs) {
        return std::tie(data, name) < std::tie(rhs.data, rhs.name);
    }
};

int main(){
    // simplistic demo
    node a { 21, "green" };
    node b { 21, "yellow" };
    node * p1 = &a;
    node * p2 = &b;
    bool less = (*p1) < (*p2);
    if (p1->data == p2->data) {
        if (*p1 < *p2) {
            std::cout << p1->name << " < " << p2->name << std::endl;
        } else {
            std::cout << p1->name << " >= " << p2->name << std::endl;
        }
    }
}

ı basically add in the else if part if the values are same and change names but appreantly it changes whole list of numbers如果值相同并更改名称,我基本上添加 else if 部分,但显然它会更改整个数字列表

void sorting( node *  &newnode) 
{

    node* temp = head;
    node* temp2 = newnode;

    int numtemp;


    while (temp != nullptr)
    {
        node* temp2 = temp->right;
        while (temp2 != nullptr)
        {

            if (temp2->data > temp->data)
            {
                string strtemp = "";
                numtemp = temp->data; // num change
                temp->data = temp2->data;
                temp2->data = numtemp;

                strtemp = temp->name; // name change
                temp->name = temp2->name;
                temp2->name = strtemp;

            }
            else if (temp2->data == temp->data) // here is the change 
            {
               if(temp2->name > temp->name ) // make comparison but im not sure wheteris logical or not 
               {
                   string strtemp = "";
                   strtemp = temp->name; // name change
                   temp->name = temp2->name;
                   temp2->name = strtemp;
               
               }
                
                 
                
            }

            temp2 = temp2->right;
        }

        temp = temp->right;
    }

   

}

Your comparison operator is bad:您的比较运算符不好:

              if(temp2->name < temp->name ) // make comparison but im not sure wheteris logical or not 
               {
                   string strtemp = "";
                   strtemp = temp->name; // name change
                   temp->name = temp2->name;
                   temp2->name = strtemp;
               
               }

Should be < .应该是< .

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

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