C - Merging 2 linked lists into the first - unexpected output

时间:2017-04-06 16:59:27

标签: c singly-linked-list

Edit 2: I thought of using a pointer to previous node *pre1 and *pre2, with pointer to current node *cur1 and *cur2. But I will prefer not to because 1) it is quite confusing, and 2) I feel I will be unable to maintain the code after some time has passed.

Edit 1: Added insertNode function

I have 2 linked lists of integers and I am trying to insert values from the second into the first at alternate positions, so index 0, 2, 4 etc will be integers from linked list 1 and 1, 3, 5 etc will be from linked list 2.

Input

Linked list 1: 1, 2, 3

Linked list 2: 99, 88, 77

Supposed Output

Linked list 1: 1, 99, 2, 88, 3, 77

Linked list 2: Empty

Instead I am getting

Linked list 1: 1, 2, 3

Linked list 2: 99, 88, 99, 88, 77

That is, my code is inserting into llist2 even though I passed llist1 into the function (and haven't written code for llist2 at all).


I have the functions createMerged and insertNode

void createMerged(LinkedList *llist1, LinkedList *llist2);

Its inputs are the 2 linked list, and modifies them.


int insertNode(LinkedList *llist, int index, int value);

It is a black box function which will insert a new node with value at the given index (first index is 0) into the given linked list


This is my code now

int main() {
    createMerged(&llist1, &llist2); 
    printf("Linked list 1: ");
    printList(&llist1);
    printf("Linked list 2: ");
    printList(&llist2);
}

void createMerged(LinkedList *llist1, LinkedList *llist2) {
    int curIndex = 0;   //Counter for while loop
    ListNode *cur1, *cur2;      
    cur1 = llist1->head;
    cur2 = llist2->head;

    if (llist1->size == 0 || llist2->size == 0) return;    //empty list

    while (cur1 != NULL) {
        if (curIndex == 0) {

 //Odd output here!
        insertNode(&llist1, 1, cur2->item);  //if index is 0, insert into 1    
        } else {
        insertNode(&llist1, curIndex * 2, cur2->item);  //else, insert into index*2
        }
        cur1 = cur1->next;
        cur2 = cur2->next;
        curIndex++;
    }
}

insertNode is a black box function which works without any issue. It is from the book Computer Science by Forouzan and Gilberg

int insertNode(LinkedList *llist, int index, int value){

    ListNode *pre, *cur;

    if (llist == NULL || index < 0 )
        return -1;

    // Insert in empty or as first node
    if (llist->head == NULL || index == 0){
        cur = llist->head;
        llist->head = malloc(sizeof(ListNode));
        llist->head->item = value;
        llist->head->next = cur;
        return 0;
    }

    // Inserting in middle
    else {
        cur = pre->next;
        pre->next = malloc(sizeof(ListNode));
        pre->next->item = value;
        pre->next->next = cur;
        return 0;
    }
    return -1;
}

To recap: I am passing llist1 into insertNode, but somehow it modifies llist2 even though I have not called llist2 in the code at all.

0 个答案:

没有答案