C 插入链表

C 插入链表,c,linked-list,insertion,C,Linked List,Insertion,我编写了一个程序,可以按降序将节点插入到链表中。但是每当我用数字12,14,13,19,7按此顺序测试代码时。每当我输入7时,我取的7已经在列表中。但很容易看出,插入之前7不在列表中。给出此错误后,如果我通过输入2选择打印选项,我的程序进入无限循环。我看不出我的错误,我很困惑 #include <stdio.h> #include <stdlib.h> struct node { int content; struct node* nextLink; };

我编写了一个程序,可以按降序将节点插入到链表中。但是每当我用数字
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;
   }
}
#包括
#包括
结构节点{
智力内容;
结构节点*nextLink;
};
typedef结构节点;
作废打印(节点*);
int insertNode(节点**头部,int x);
内部主(空)
{
int-num,选择;
节点*头;
head=NULL;
做{
printf(“\n请按1插入,或按2打印,或按0退出\n”);
scanf(“%d”,选择(&C);
开关(选择){
案例0:
返回0;
打破
案例1:
printf(“输入要插入linkedlist的整数:”);
printf(“\n”);
scanf(“%d”和&num);
insertNode(&head,num);
打破
案例2:
打印头;
打破
违约:
printf(“您输入了一个无效的数字\n”);
返回0;
打破
}
}而(choice==1 | | choice==2);
返回0;
}
int insertNode(节点**头部,int i)
{
节点*新节点;
newNode=(NODE*)malloc(sizeof(NODE));
newNode->content=i;
节点*临时=*头部;
newNode->nextLink=NULL;
如果((*head==NULL)| |((*head)->内容)nextLink=临时;
}
否则{
做{
如果((临时->内容>i)和((临时->下一链接->内容)nextLink=临时->nextLink;
临时->nextLink=newNode;
返回;
}
else if(临时->内容==i){
printf(“待插入值已在列表中\n”);
返回;
}
临时=临时->下一步链接;
}while(临时->下一链接!=NULL);
如果(临时->内容==i){
printf(“待插入值已在列表中\n”);
返回;
}
临时->nextLink=newNode;
}
返回0;
}
无效打印(节点*头)
{
if(head==NULL){
printf(“\nLinkedList为空\n”);
}
while(head!=NULL){
printf(“%d”,标题->内容);
head=head->nextLink;
}
}

我编译并运行了它,除了一件事,它似乎工作得很好
insertNode
被定义为返回一个int,但其中3个返回语句是void返回。为了让它编译,我将它们改为
return0。如果您能够按原样编译和运行它,那么可能是堆栈被不一致的返回破坏了。

如果要插入的前两个值按降序排列,则代码将无法工作。它会给分割错误

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

所以在if条件之后

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

你的代码做得太多了。如果以不同的方式进行编码,则不存在特殊情况(例如在列表顶部插入、在列表尾部插入)


不会编译!!请发布正确的可编译代码,我想你要做的是NODE*temporary=head;我用了一个双指针,所以我应该这样写。哦,对不起,我没看到。对于您的示例,您的程序似乎运行得很好。我只是改变了回报;返回0;因为我得到了一个非常明显的编译错误。我在Ubuntu上使用了gcc,我没有因为返回语句而出现任何编译错误。谢谢大家。特别是对MacGucky。代码现在可以工作了,但是你如何通过只删除空行来更正代码?空行对编译器来说是个问题吗?这个问题是对MacGucky的,因为他编辑了我的代码,它就工作了。@美德:我看了他的编辑,没有看到任何会影响行为的变化。我可能遗漏了一些东西,但它看起来完全像是为了使代码更具可读性而进行的格式更改。所以我不知道是什么问题。您是否按照我的建议更改了退货声明?我很好奇这是否有任何影响。@优点:我只是通过我的代码格式化程序(uncrustify)运行它,并手动删除了一些空行。我编译并测试了您的代码和我的代码(在insertNode中所有
返回;
,其中更改为
返回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;
}