简体   繁体   English

通过查找功能在地图中搜索键值没有正确发生

[英]searching in map through find function for key value is not happening properly

In the code below, I am performing search in map through key value but am not able to get the desired results though common elements are present in both lists and find with map should give expected output.在下面的代码中,我正在通过键值在地图中执行搜索,但无法获得所需的结果,尽管两个列表中都存在公共元素,并且使用地图查找应该给出预期的输出。 Is there any issues while performing find with map?使用地图执行查找时是否有任何问题? is that right approach ?这是正确的方法吗?

input输入

list1:10->15->4->20 list2:10->2->4->8列表1:10->15->4->20 列表2:10->2->4->8

expected output: common->data=4 common->data=10预期输出:common->data=4 common->data=10

 #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <map>
    using namespace std;
    struct node
    {
        int data;
        struct node* next;
    };
    /* A utility function to insert a node at the beginning of 
       a linked list*/
    void push(struct node** head_ref, int new_data);
    /* A utility function to insert a node at the begining of a linked list*/
    void push (struct node** head_ref, int new_data)
    {
        /* allocate node */
        struct node* new_node =
            (struct node*) malloc(sizeof(struct node));
        /* put in the data */
        new_node->data = new_data;
        /* link the old list off the new node */
        new_node->next = (*head_ref);
        /* move the head to point to the new node */
        (*head_ref) = new_node;
    }
     /*insert the head1 into map and find the head2 in map*/
    int create_hash(struct node* head1,struct node* head2)
    {
      int flag=0;
     map<node*,bool> intersect;

    while(head1!=NULL)
    {
    printf("first_list->data=%d\n",head1->data);
    intersect[head1]=true;
    head1=head1->next;
    }
    while(head2!=NULL)
    {
    printf("second_list->data=%d\n",head2->data);
    if (intersect.find(head2)!= intersect.end())
           printf("common->data=%d\n",head2->data); 
           flag=1;
           head2=head2->next;
    }
    if(flag==1)
    {
    return 0;
    }
    return -1;
    }
    /* Drier program to test above function*/
    int main()
    {
        /* Start with the empty list */
        struct node* head1 = NULL;
        struct node* head2 = NULL;
        int ret_val;
        struct node* unin = NULL;
        /*create a linked lits 10->15->4->20 */
        push (&head1, 20);
        push (&head1, 4);
        push (&head1, 15);
        push (&head1, 10);
        /*create a linked list 10->2->4->8 */
        push (&head2, 10);
        push (&head2, 2);
        push (&head2, 4);
        push (&head2, 8);
        ret_val = create_hash (head1, head2);
        return 0;
    }

You populate the intersect map with pointers to nodes in the first list, and then search that map for pointers to nodes in the second list.您使用指向第一个列表中节点的指针填充intersect映射,然后在该映射中搜索指向第二个列表中节点的指针。 Since nodes are not shared between lists, the search will never succeed.由于列表之间不共享节点,因此搜索永远不会成功。

In the context of this implementation, the map should contain the data values, not node pointers.在此实现的上下文中,映射应包含数据值,而不是节点指针。

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

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