2011-03-25 120 views
-1

我写了一个程序,按降序将节点插入到链表中。但是每当我按照这个顺序测试我的代码时,数字为12,14,13,19,7。每当我输入7时,我都拿到了7,已经在列表中。但是作为在我插入之前很容易看到7不在列表中。给出这个错误后,如果我通过输入2我的程序选择了打印选项,我的程序在无限循环中输入。我看不到我的错误,我很困惑。插入链表

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

struct node { 
    int content; 
    struct node* nextLink; 
}; 

typedef struct node NODE; 

void print (NODE*); 
int insertNode (NODE** head, int x); 

int main (void) 
{ 
    int num, choice; 
    NODE* head; 
    head = NULL; 

    do { 
     printf("\nPlease press 1 to insert or press 2 to print or press 0 to exit\n"); 
     scanf("%d", &choice); 
     switch (choice) { 
     case 0: 
      return 0; 
      break; 

     case 1: 
      printf("Enter an integer to insert into the linkedlist: "); 
      printf("\n"); 
      scanf("%d", &num); 
      insertNode(&head, num); 
      break; 

     case 2: 
      print(head); 
      break; 

     default: 
      printf("You entered an invalid number\n"); 
      return 0; 
      break; 
     } 
    } while (choice == 1 || choice == 2); 

    return 0; 
} 

int insertNode (NODE** head, int i) 
{ 
    NODE* newNode; 
    newNode   = (NODE*)malloc(sizeof(NODE)); 
    newNode->content = i; 
    NODE* temporary = *head; 
    newNode->nextLink = NULL; 

    if ((*head == NULL) || ((*head)->content) < i) { 
     *head    = newNode; 
     (*head)->nextLink = temporary; 
    } 
    else { 
     do { 
     if (((temporary->content) > i) && ((temporary->nextLink->content) < i)) { 
      newNode->nextLink = temporary->nextLink; 
      temporary->nextLink = newNode; 
      return; 
     } 
     else if (temporary->content == i) { 
      printf("To be inserted value is already in the list\n"); 
      return; 
     } 
     temporary = temporary->nextLink; 
     } while (temporary->nextLink != NULL); 

     if (temporary->content == i) { 
     printf("To be inserted value is already in the list\n"); 
     return; 
     } 

     temporary->nextLink = newNode; 
    } 
    return 0; 
} 

void print (NODE* head) 
{ 
    if (head == NULL) { 
     printf("\nLinkedList is empty \n"); 
    } 

    while (head != NULL) { 
     printf("%d ", head->content); 
     head = head->nextLink; 
    } 
} 
+2

不会编译!!请发表正确的可编译代码 – Sadique 2011-03-25 21:12:57

+0

我想你想做NODE * temporary = head;而不是*头 – Chris 2011-03-25 21:13:09

+0

不,我用那里有一个双指针,所以我应该这样写。 – virtue 2011-03-25 21:16:36

回答

0

我编译它并运行它,它似乎工作正常,除了一件事。 insertNode定义为返回一个int,但3个返回语句是无效返回。为了编译它,我将它们改为return 0;。如果你能够按原样编译并运行它,那么它可能是由于不一致的返回而被破坏。

+0

谢谢大家。尤其是MacGucky。代码现在可以正常工作,但是如何通过只删除空行来纠正代码?是否有很多空行是编译器的问题?这个问题是给MacGucky的,因为他编辑了我的代码并且工作正常。 – virtue 2011-03-25 21:34:17

+0

@virtue:我看着他的编辑,我没有看到任何会影响行为的变化。我可能错过了一些东西,但它看起来完全像格式更改,使代码更具可读性。所以我不知道问题是什么。我是否按照我的建议改变了回报声明?我很好奇,如果这有什么影响。 – 2011-03-25 21:56:01

+0

@virtue:我只是通过我的代码格式化程序(uncrustify)运行它,并手动删除了一些空行。我编译并测试了你的代码和我的代码(两者都改变​​了所有'return;'在insertNode中改变为'return 0;'),并且它们都按预期工作。我没有发现任何问题。 – MacGucky 2011-03-25 22:03:10

0

如果要插入的前两个值按降序排列,则您的代码将不起作用。它会给分段错误。

对于第二个元素的插入,你需要小心

所以经过如果条件

else if (temporary->content > i && temporary->nextLink==NULL) 
     (*head)->nextLink = newNode; 
0

你的代码是做得太多。如果以不同的方式对其进行编码,则没有特殊情况(例如顶部插入,插入尾部列表)。

int insertNode (NODE **head, int val) 
{ 
    NODE *newnode; 

    for (; *head; head = &(*head)->nextLink) { 
     if ((*head)->content == val) { 
      printf("To be inserted value (%d)is already in the list\n", val); 
      return 0; 
      } 
     if ((*head)->content > val) break; 
     } 
    newnode = malloc(sizeof *newnode); // Maybe check return here ;-) 
    newnode->content = val; 
    newnode->nextLink = *head; 
    *head = newnode; 
    return 1; 
}