链表联合

时间:2015-08-21 19:16:00

标签: c pointers data-structures linked-list

我创建了一个程序来查找2个链表的联合。我的逻辑首先是将一个新列表插入list1内容放入此列表中,并仅插入list2中不在结果列表中的那些值。我的代码是:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

/* Linked list node */
struct node
{
    int data;
    struct node* next;
};
struct node* result;

struct node *newNode(int data)
{
    struct node *new_node = (struct node *) malloc(sizeof(struct node));
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

/* Function to insert a node at the beginning of the Doubly Linked List */
void push(struct node** head_ref, int new_data)
{
    /* allocate node */
    struct node* new_node = newNode(new_data);

    new_node->next=*head_ref;
    *head_ref=new_node;
}

struct node *union3(struct node *first, struct node *second, struct node *result)
{
    int flag = 0;
    struct node *temp = NULL;
    temp = first;
    struct node *temp2 = NULL;
    temp2 = second;
    int value;
    while (temp != NULL)
    {
        push(&result, temp->data);      // pushing first list values in result
        temp = temp->next;
    }
    while (second)
    {
        present(second->data, result); // comparing second list each member with result
        second = second->next;
    }
    return result;
}

void present(int data, struct node *result1)
{
    printf("The value in the beginning of present function is:");

    int flag = 0;
    while (result1)
    {
        if (result1->data == data)
        {
            flag++;
        }
        result1 = result1->next;
    }
    if (flag > 0)
    {
        printf("");
    }
    else
    {
        push(&result, data);
    }
}

void printList(struct node *node)
{
    while(node != NULL)
    {
        printf("%d ", node->data);
        node = node->next;
    }
    printf("\n");
}

/* Drier program to test above function */
int main(void)
{
    struct node* first = NULL;
    struct node* second=NULL;
   // struct node* result=NULL;
    struct node* union2=NULL;
    // create first list 7->5->9->4->6
    push(&first, 6);
    push(&first, 4);
    push(&first, 9);
    push(&first, 5);
    push(&first, 7);
    printf("First list is:");
    printList(first);
    push(&second,6);
    push(&second,4);
    push(&second,9);
    push(&second,11);
    push(&second,12);
    printf("second list is");
    printList(second);
    printf("their union is:");
    union2=union3(first,second,result);
    printf("Union of 2 lists is:");
    printList(union2);
    return 0;
} 

基本上我的逻辑是正确的但结果变量会出现问题。即使我将结果作为全局变量,当它进入present()函数时,其中推入的list1值会在其中丢失。任何人都可以告诉为什么输出只显示list1内容:

output:6 4 9 5 7

2 个答案:

答案 0 :(得分:1)

使用你的算法如果list1有重复项,它们将显示在最终结果中,但如果list2有重复项,它们将不会显示在你可能不想要的最终结果中。

另外我认为你的意思是使用temp2而不是秒:

while(second)
{
present(second->data,result);    //comparing second list each member with result                       
second=second->next;
}

最后这花了我一些时间,但我发现了你的错误:

push(&result,data); 

应该是 1

的结果

希望这有帮助!

答案 1 :(得分:0)

捕捉! :)你还需要添加一个为列表释放已分配内存的函数。

First list is: 7 5 9 4 6 
second list is: 12 11 9 4 6 
Union of 2 lists is: 7 5 9 4 6 12 11 

程序输出

push

此外,您可以编写函数col-*,这样就不会将重复的值添加到列表中。

相关问题