在排序的链表中插入元素

时间:2019-02-12 21:59:24

标签: c linked-list singly-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)无法正常工作,它应该返回什么?

4 个答案:

答案 0 :(得分:1)

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

node* insert(node *head,int x)

答案 1 :(得分:1)

这是链接列表的一个示例,希望对您有所帮助。它包含用于插入,添加和打印节点的功能。在主要功能中,您可以找到不进行递归扫描的方式。

#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 */
}

答案 2 :(得分:0)

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

#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;
    }
}

答案 3 :(得分:0)

在“插入”函数中,else块中有return语句,因此仅当p1不等于NULL时,它才返回head:

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

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

相关问题