求助:节点插入单链表无法正常运行

#include  <stdio.h>
#include  <malloc.h>

struct node
{
  int num;
  struct node* next;
};

/* create LinkList  */
struct node* create(int n);  
{   
   struct node* head,*p;
   p=head=NULL;
   int i;
   for(i=0;i<n;i++)
   {            
      struct node* point=(struct node*)malloc(sizeof(struct node));
      point->num=i;
      if(0==i)
      {
         head=point;
      }
      else
      {
         p->next=point;
      }
         p=point;
         point->next=NULL;
   }
  return head;
}



/*head is LinkList head node , p is input node */
struct node* insert_node(struct node* head , struct node* p)  
{
     if(!head)
     {
       head=p;
       p->next=NULL;
     }
     else
     {
        struct node* p1,*p2;
        p1=head;
        while(p->num > p1->num && p1->next != NULL)
        {
          p2=p1;
          p1=p1->next;
        }

         if(p->num < p1->num)
         {
             if(head==p1)
             {
                head=p;
                p->next=p1;
             }   
             else
             {
                p2->next=p;
                p->next=p1;
             }
          }
          else
          {
                p1->next=p;
                p->next=NULL;
          }
     }



  struct node* foreach;
  for( foreach=head ; foreach!= NULL ; foreach = foreach->next )
  {
       printf("%d\t",foreach->num);
  } 

}

int main()
{
  create(9);
  struct node* pp=(struct node*)malloc(sizeof(struct node));
  pp->num=5;
  insert_node(head,5);
    return 0;
 }

正常运行结果应该是:0 1 2 3 4 5 5 6 7 8, 但目前运行结果是:0 1 2 3 4 5 5,后续的三位数 6 7 8 无法显示出来,请帮助解决,谢谢.

阅读 1.3k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进