链表问题
#include<stdio.h>#include<stdlib.h>
#define null 0
#define n 9
struct list
{
int number;
struct list *next;
};
struct list *node;
int key;
void create(list *node);
void *insert(list *node,int key);
void print(list *node);
void main()
{
list *head;
head=(list *)malloc(sizeof(list));
create(head);
print(head);
printf("\n------------------------------------------------------\n");
insert(head,n);
print(head);
}
void create(list *node)
{
printf("(nter the number -999 to end)********");
scanf("%d",&node->number);
if(node->number==-999)
{
node->next=null;
}
else
{
node->next=(list *)malloc(sizeof(list));
create(node->next);
}
return;
}
void *insert(list *node,int key)
{
struct list *h;
while(node->next!=null)
{
if(node->number<=key&&node->next->number>=key)
{
printf("ok find it and prepare insert it\n");
h=(list *)malloc(sizeof(list));
h->number=key;
node->next=h;
h->next=node->next->next;
}
insert(node->next,key);
}
return;
}
void print(list *node)
{
if(node->next!=null)
{
printf("%d-->",node->number);
if(node->next->next==null)
printf("%d",node->next->number);
print(node->next);
}
return;
}
运行结果是这样的
肯定是红色部分插入函数的错误 希望大家帮我看看什么原因 谢谢!