#include<stdio.h>
#include<stdlib.h>
#include <conio.h>
typedef struct node
{
int num;
struct node *link;
}Node;
void
creat(int *num,int len);
void sll_insert(Node **head,int value);
void show(Node *temp);
Node *head;
int main(void)
{
int num[10]={1,3,4,6,7,10,12,15,18,20};
int insert_number;
head=NULL;
creat(num,10);
printf("before insert\n");
show(head);
printf("Enter the insert number.\n");
scanf("%d",&insert_number);
sll_insert(&head,insert_number);
printf("After insert number\n");
show(head);
}
//创建有序链表函数接口
void creat(int *num,int len)
{
Node *ptr,*ptr1;
register int i;
head=(Node *)malloc(sizeof(Node));
if(!head)
{
printf("malloc head NULL");
exit(1);
}
head->num=num[0];//创建链表
ptr=head;
for(i=1;i<len;i++)
{
ptr1=(Node *)malloc(sizeof(Node));
if(!ptr1)
{
printf("malloc ptr1 error.\n");
exit(1);
}
ptr1->num=num[i];
ptr->link=ptr1;//链接
ptr1->link=NULL;
ptr=ptr1;
//移动到下一个结点
}
}
//插入接口函数
void sll_insert(Node **head,int value)
{
Node *current;
Node *previous;
Node *new_node;
current=*head;
previous=NULL;
while(current!=NULL&¤t->num<value)
{
previous=current;
current=current->link;
}
new_node=(Node *)malloc(sizeof(Node));
if(!new_node)
{
printf("malloc new_node error");
exit(1);
}
new_node->num=value;
new_node->link=current;
if(!previous)
{
*head=new_node;
}
else
{
previous->link=new_node;
}
}
void show(Node *temp)
{
while(temp!=NULL)
{
printf("[%d]",temp->num);
temp=temp->link;
}
printf("\n");
}