在已排序的链表中插入一个元素

insert an element in a sorted linked list

谁能帮我调试这个程序,我是初学者,找不到解决方案,我收到错误:

incompatible types when returning type 'node * {aka struct linked_list *}' but 'node {aka struct linked_list}' was expected

本程序在排序链表中插入一个元素

struct linked_list
{
    int number;
    struct linked_list *next;
};

typedef struct linked_list node;

int main()
{
    int n;
    node *head;
    void create(node *p);
    node insert(node *p, int n);
    void print(node *p);
    head= (node *)malloc(sizeof(node));
    create(head);
    printf("\n");
    printf("original list : ");
    print(head);
    printf("\n\n");
    printf("input the number to be inserted: ");
    scanf("%d",&n);
    head=insert(head, n);
    printf("\n");
    printf("new list after insertion : ");
    print(head);*/
    return 0;
}

void create(node *list)
{
    printf("input a number : \n");
    printf("type -999 at the end");
    scanf("%d", &list->number);
    if(list->number == 999)
        list->next= NULL;
    else
        list->next=(node*)malloc(sizeof(node));
        create(list->next);
    return;
}

void print(node *list)
{
    while(list->next != 0)
    {
        printf("%d",list->number);
        list= list->next;

    }
    return;
}

node insert(node *head,int x)
{
    node *p1,*p2,*p;
    p1=NULL;
    p2=head;
    for(;p2->number<x;p2=p2->next)
    {
        p1=p2;
        if(p2->next==NULL)
            p2=p2->next;
            break;
    }
    p=(node*)malloc(sizeof(node));
    p->number=x;
    p->next=p2;
    if(p1==NULL)
    {
        head=p;
    }
    else
    {
        p1->next=p;
        return head;
}

node insert(node *head,int x) 不能很好地工作它应该 return 是什么?

您正在从 insert() 返回指针,因此正确的签名是

node* insert(node *head,int x)

这里是一个链表的例子,希望对你有所帮助。它包含用于插入、添加和打印节点的函数。在主要功能中,您可以找到如何在不递归的情况下扫描列表。

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


typedef struct tagNode
{
   int number;
   struct tagNode *next; 
} NODE;


typedef struct tagList
{
    NODE *head;   /* pointer to first node in list */
    NODE *last;   /* pointer to last node in list */
} LIST;



/* func proto's */
NODE* create_node();
NODE* insert_node(LIST* l,NODE *p, int n);
NODE* add_node(LIST* l, NODE *p, int n);
void print_node(NODE *p);


int main()
{ 
  LIST list = {NULL,NULL}; /* init list with NULLs */

    /* add some nodes to list */    
    for( int i = 0; i < 10 ; i++ )
    {
       add_node(&list, create_node(),i * 5 );
    }

    NODE* p = list.head;
    for( ;p != NULL; p = p->next )
    {
       print_node(p);
    }

    /* insert some nodes */
    insert_node(&list, create_node(),33);
    insert_node(&list, create_node(),23);
    insert_node(&list, create_node(),13);

    /* print list after inserts */
    for(p = list.head; p != NULL; p = p->next )
    {
       print_node(p);
    }
}


/* create empty node */
NODE* create_node()
{
  NODE *p = malloc(sizeof(NODE));
  p->next = NULL;
  p->number = 0;
}

/* add node to end of list */
NODE* add_node(LIST* list, NODE* p, int num)
{ 
  if(list->last == NULL )
  { 
     printf("add first\n");
     list->last = p;
     list->head = p;
     p->number = num;
  }
  else if( list->last->number < num )
  { 
    printf("add num %d\n",num);
    list->last->next = p;
    list->last = p;
    p->number  = num;
    p->next = NULL; /* terminate list */
  }
  return list->last;
}

void print_node(NODE *p)
{ 
    printf("node number: %d\n",p->number);
}

NODE* insert_node(LIST* l, NODE *q,int num)
{ 
    /* scan list ... */ 
    for( NODE* p = l->head;p != NULL; p = p->next )
    {  
        if( p->next->number > num )
        {
            q->next = p->next;
            p->next = q; 
            q->number = num;
            return q;  /* indicate success */
        }        
    } 
    return NULL;  /* indicate failure */
}

感谢大家的帮助,成功了!这是它的正确版本

#include <stdio.h>
#include <stdlib.h>
#define NULL 0


struct linked_list
{
    int number;
    struct linked_list *next;
};

typedef struct linked_list node;

int main()
{
    int n;
    node *head;
    void create(node *p);
    node *insert(node *p, int n);
    void print(node *p);
    head= (node *)malloc(sizeof(node));
    create(head);
    printf("\n");
    printf  ("original list : ");
    print(head);
    printf("\n\n");
    printf("input the number to be inserted: ");
    scanf("%d",&n);
    head=insert(head, n);
    printf("\n");
    printf("new list after insertion : ");
    print(head);
    return 0;
}

void create(node *list)
{
    printf("input a number : \n");
    printf("(type -999 at the end): ");
    scanf("%d", &list->number);
    if(list->number == -999)
        {list->next= NULL;}
    else
        {list->next=(node *)malloc(sizeof(node));
        create(list->next);}
    return;
}

void print(node *list)
{
    while(list->next != NULL)
    {
        printf("%d->",list->number);
        list= list->next;

    }
    return;
}

node *insert(node *head,int x)
{
    node *p1,*p2,*p;
    p1=NULL;
    p2=head;
    for(;p2->number<x;p2=p2->next)
    {
        p1=p2;
        if(p2->next->next==NULL)
        {p2=p2->next;
            break;
        }
    }
    p=(node *)malloc(sizeof(node));
    p->number=x;
    p->next=p2;
    if(p1==NULL)
    {
        head=p;
    }
    else
    {
        p1->next=p;


    return head;
    }
}

在 "insert" 函数中,您在 else 块中有 return 语句,因此只有当 p1 不等于 NULL 时,它才会 return head:

if(p1==NULL)
{
    head=p;
}
else
{
    p1->next=p;
return head;
}

在 else 块的“}”之后移动 return 语句,函数将 return 头指针正确。