2013-04-22 43 views
2

的代碼:爲什麼我無法在此鏈接列表的索引0(頭部)插入新節點?

/* 
* File: problem5.c 
* Author: levihackwith 
* Description: Write a Pop() function that is the inverse of Push(). Pop() takes a non-empty list, deletes the head node, and returns the head node's data. 
*/ 

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

struct node { // Our custom node data type 
    int data; 
    struct node *next; 
}; 

/* 
* Adds a node to a linked list. 
* 
* This method was taken from the Appendix of the LinkedListProblems.pdf file from Stanford University. 
*/ 
void Push(struct node** headRef, int newData) { 
    struct node* newNode = malloc(sizeof (struct node)); // allocate node 
    newNode->data = newData; 
    newNode->next = (*headRef); 
    (*headRef) = newNode; 
}; 

void InsertNth(struct node** headRef, int insertAt, int newData) { 
    struct node* newNode = malloc(sizeof (struct node)); // allocate node 
    struct node* current = *headRef; 
    int index = 0; 

    newNode->data = newData; 

    while (current != NULL) { 
     if (insertAt == 0 && index == 0) { 
      newNode->next = (*headRef); 
      (*headRef) = newNode; 
      current = *headRef; 
      printf("Current's data is now %d at index %d \n\n", current->data, index); 
     } else { 
      if (index == (insertAt - 1)) { 
       printf("I inserted %d at index %d \n", newData, insertAt); 
       newNode->next = current->next; 
       current->next = newNode; 
      } 
     } 
     current = current->next; 
     index++; 
    } 
} 

/* 
* Builds a linked list of a given size. 
*/ 
struct node* BuildList(int numNodes) { 
    struct node* head = NULL; // Start with the empty list 
    int i; 

    for (i = numNodes; i >= 1; i--) { 
     Push(&head, i); // Use Push() to add all the data 
    } 
    return (head); 
}; 

int main(void) { 

    struct node* myLinkedList; 
    struct node* current; 
    int currentIndex = 0; 
    int valToInsert = 45; 
    int insertIndex = 0; 

    myLinkedList = BuildList(5); 
    current = myLinkedList; 

    InsertNth(&myLinkedList, insertIndex, valToInsert); 
    while (current != NULL) { 
     printf("The value at index %d is %d \n", currentIndex, current->data); 
     currentIndex++; 
     current = current->next; 
    } 

    return 0; 
}; 

輸出

Current's data is now 45 at index 0 

The value at index 0 is 1 
The value at index 1 is 2 
The value at index 2 is 3 
The value at index 3 is 4 
The value at index 4 is 5 

上述輸出是用於當該指數在零插入新值。以下是在索引1處插入時的輸出。

I inserted 45 at index 1 

The value at index 0 is 1 
The value at index 1 is 45 
The value at index 2 is 2 
The value at index 3 is 3 
The value at index 4 is 4 
The value at index 5 is 5 

正如您所看到的,0不會像預期的那樣工作,而是1。我究竟做錯了什麼?

回答

2

我在做什麼錯?

在致電InsertNth之前,您正在設置current。當您撥打InsertNth插入0時,myLinkedList將會更改,因此current將指向第二個節點。

移動current = myLinkedList;到後InsertNth調用來解決這個問題:

myLinkedList = BuildList(5); 
InsertNth(&myLinkedList, insertIndex, valToInsert); 
current = myLinkedList; 
+0

優秀的答案。感謝您的編輯,使其更清晰。 – 2013-04-22 00:25:58

1

開關以下兩行:

current = myLinkedList; 
InsertNth(&myLinkedList, insertIndex, valToInsert); 

在此爲了要設置current到的第一部分的頭列表中,然後添加更多的東西到頭上。這改變了頭部,但current仍然有其原始價值。

相關問題