#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct node
{
int num;
struct node * next;
};
typedef struct node Node;
typedef struct node* Link;
void creat_Link(Link * head) //定义 头指针
{
*head = NULL;
}
void insert_node_head(Link *head,Link new_node) //头插
{
new_node->next = *head;
*head = new_node;
}
void display(Link head) //输出
{
Link p;
p = head;
if(head ==NULL)
{
printf("Link is empty!\n");
}
while(p!=NULL)
{
printf("num = %d\n",p->num);
p = p->next;
}
printf("\n");
}
void insert_node_tail(Link *head,Link new_node) //尾插
{
Link p;
p = *head;
if(NULL == p)
{
*head = new_node;
new_node->next = NULL;
}
else
{
while(p->next !=NULL)
{
p = p->next;
}
p->next = new_node;
new_node->next = NULL;
}
}
void insert_node_mid(Link * head,int loc,int number) //中间插
{
Link p,q;
Link new_node;
p = q = *head;
if(q == NULL)
{
printf("link is empyt!\n");
}
else
{
while(p->num != loc && p->next != NULL)
{
q = p;
p = p->next; //找到该指针
}
if(p->num == loc)
{
if(p == *head)
{
new_node->next = *head;
*head = new_node;
}
else
{
new_node = (Link)malloc(sizeof(Node)); //分配空间
new_node->num = number;
new_node->next = p;
q->next = new_node;
}
}
else
{
printf("none\n");
}
}
}
void delete_node(Link*head,int num) //删除结点
{
Link p,q;
p = q =*head;
if(p == NULL)
{
printf("link is empyt!\n"); //空指针
}
else
if((*head)->num == num) // 删除头
{
*head = (*head)->next;
free (p);
}
else
{
while(p!= NULL&& p->num != num)
{
q = p;
p = p->next;
}
if(p != NULL)
{
q->next = p->next;
free(p);
}
else //找不到
{
printf("no such node!\n"); //
}
}
}
int Link_len(Link head) // 长度
{
int cnt = 0;
while(head != NULL)
{
cnt++;
head = head->next;
}
return cnt;
}
void sort(Link* head) // 插排
{
int len;
len = Link_len(*head);
if(len<=1) // 1个结点
{
return;
}
else if(len ==2) //2个结点
{
Link p,q;
p = *head;
q = (*head)->next;
if(p->num > q->num)
{
*head = q;
q->next = p;
p->next = NULL;
}
}
else //=>3结点
{
Link p,q,x,y; //
q = (*head)->next;
if((*head)->num > q->num) //前两个比较
{
(*head)->next = q->next;
q->next = *head;
*head = q;
}
q = y = (*head)->next;
while(q->next !=NULL)
{
q = q->next;
x = *head;
p = x->next;
while(p!=q)
{
if(x ==*head) //后面的和头指针比较
{
if(x->num > q->num)
{
y->next = q->next;
q->next = x;
*head = q;
q = y;
break;
}
}
if(p->num > q->num) //正常比较
{
x->next = q;
y->next = q->next;
q->next = p;
q = y;
break;
}
x = p;
p = p->next;
}
y = q;
}
return;
}
}
void release(Link * head) //释放
{
Link p ;
while(*head != NULL)
{
p = *head;
*head = (*head)->next;
free(p);
}
}
int main()
{
srand(time(NULL));
Link new_node;
Link head;
int i,n,loc,num;
creat_Link(&head);
for(i=0;i<10;i++)
{
n = rand()%100;
new_node = (Link)malloc(sizeof(Node));
insert_node_head(&head,new_node);
new_node->num = n;
}
display(head);
// printf("please input a number to be deleted!\n");
// scanf("%d",&num);
//delete_node(&head,num);
// printf("\n");
// display(head);
printf("please input a number to be insert!\n");
scanf("%d%d",&loc,&num);
insert_node_mid(&head,loc,num);
display(head);
sort(&head);
display(head);
printf("\n");
//printf("\n");
// release(&head);
// display(head);
return 0;
}
单链表
最新推荐文章于 2022-03-10 20:41:07 发布