#include<stdio.h>
#include<stdlib.h>
#include<time.h>
struct hnode
{
int num;
struct hnode * next;
};
typedef struct hnode Hnode;
typedef Hnode * Hlink;
enum result_val{RET_OK = 1000,RET_NO};
int is_malloc_ok(Hlink new_node)
{
if(new_node == NULL)
{
return RET_NO;
}
else
{
return RET_OK;
}
}
int create_node(Hlink * new_node)
{
*new_node = (Hlink)malloc((unsigned)sizeof(Hlink));
if(RET_NO == is_malloc_ok(*new_node))
{
return RET_NO;
}
else
{
return RET_OK;
}
}
void create_link(Hlink * head)
{
create_node(head);
(*head)->next = NULL;
}
void display_link(Hlink head)
{
Hlink p =NULL;
if(head == NULL)
{
printf("no link !\n");
}
else
{
p = head->next;
if(p == NULL)
{
printf("the link is empty !\n");
return;
}
else
{
while(p != NULL)
{
printf("%d\n",p->num);
p = p->next;
}
}
}
}
void insert_node_mind_sort(Hlink head,Hlink new_node)
{
Hlink p = NULL;
Hlink q = NULL;
p = (head)->next;
q = head;
while(p != NULL && p->num < new_node->num)
{
q = p;
p = p->next;
}
q->next = new_node;
new_node->next = p;
}
void make_empty(Hlink head)
{
Hlink p =NULL;
p = head->next;
while(head->next != NULL)
{
p = head->next;
head->next = head->next->next;
free(p);
}
}
void release_link(Hlink * head)
{
Hlink p = NULL;
make_empty(* head);
//p = *head;
//while(*head != NULL)
{
p = *head;
//*head = (*head)->next;
if((*head)->next == NULL)
free(p);
}
// free(p);
*head = NULL;
}
void insert_node_head(Hlink head,Hlink new_node)
{
Hlink p = NULL;
p = head->next;
new_node->next = p;
head->next = new_node;
}
void insert_node_tail(Hlink head,Hlink new_node)
{
Hlink p = NULL;
p = head;
while(p->next != NULL)
{
p = p->next;
}
p->next = new_node;
new_node->next = NULL;
}
void insert_node_mind_front(Hlink head,Hlink new_node,int loc)
{
Hlink p = NULL;
Hlink q = NULL;
p = head->next;
q = head;
while(p != NULL && p->num != loc)
{
q = p;
p = p->next;
}
/*if( p == NULL)
{
printf("no found\n");
}*/
/*if(p == head->next)
{
new_node->next =head->next;
head->next = new_node;
}*/
{
q->next = new_node;
new_node->next = p;
}
}
void insert_node_mind_behand(Hlink head,Hlink new_node,int loc)
{
Hlink p = NULL;
// Link q = NULL;
p = head;
while(p->next != NULL && p->num != loc)
{
//q = p;
p = p->next;
}
/*if(p == *head)
{
new_node->next = p->next;
*head = new_node;
}*/
//else
{
new_node->next = p->next;
p->next =new_node;
}
}
void reverse_node(Hlink head)
{
Hlink p1 = NULL;
Hlink p2 = NULL;
Hlink p3 =NULL;
p1 = head->next;
if( head ->next == NULL)
{
printf("the link is empty !\n");
}
else if((head)->next->next == NULL)
{
printf("is only one node \n");
}
else if(((head)->next)->next->next == NULL)
{
p2 = p1->next;
p2->next = p1;
(head)->next->next = NULL;
head->next = p2;
}
else
{
p2 = p1->next;
p3 = p2->next;
p2->next = p1;
p1->next = NULL;
head->next = p2;
while(p3 != NULL)
{
p2->next = p1;
p1 = p2;
p2 = p3;
p3 = p3->next;
}
p2->next = p1;
(head)->next =p2;
}
}
void delete_node(Hlink head,int num)
{
Hlink p = NULL;
Hlink q = NULL;
q = p = head;
while(p != NULL && p->num != num)
{
q = p;
p = p->next;
}
if(p == NULL)
{
printf("NO find\n");
}
else
{
q->next = p->next;
free(p);
}
}
int length_link(Hlink head)
{
Hlink p = NULL;
int count = 0;
p = head->next;
while(p != NULL)
{
count++;
p = p->next;
}
return count;
}
Hlink find(Hlink head,int num)
{
Hlink p = NULL;
p = head->next;
while(p != NULL && p->num != num)
{
p = p->next;
}
if(p == NULL)
{
printf("no found");
}
else
{
return p;
}
}
int main()
{
Hlink head = NULL;
Hlink new_node = NULL;
Hlink p = NULL;
int i;
int loc;
int num;
int count=0;
srand((unsigned)time(NULL));
create_link(&head);
for(i = 0; i < 10; i++)
{
if(RET_OK == create_node(&new_node))
{
new_node->num = rand() % 100;
//new_node->num = i+1;
insert_node_mind_sort(head,new_node);
//insert_node_head(head,new_node);
//insert_node_tail(head,new_node);
}
else
{
i--;
}
}
/*count = length_link(head);//长度
printf("the length is %d\n",count);*/
display_link(head);
/*printf("please input the num\n");//查找
scanf("%d",&num);
p = find(head,num);
printf("the result is %d\n",p->num);*/
/*printf("\nplease input the num\n");//删除
scanf("%d",&num);
delete_node(head,num);
printf("\n\n");*/
/*if(RET_OK == create_node(&new_node))//插入
{
printf("please input the loc\n");
scanf("%d",&loc);
printf("please input the num\n");
scanf("%d",&new_node->num);
printf("\n");
//insert_node_mind_front(head,new_node,loc);
insert_node_mind_behand(head,new_node,loc);
display_link(head);
}*/
// display_link(head);
/*printf("the reverse is \n");
reverse_node(head);//逆序
display_link(head);*/
display_link(head);make_empty(head);
release_link(&head);
display_link(head);
}
C语言带表头链表的使用,头尾中插,显示,删除,查找,逆序等功能的实现
最新推荐文章于 2022-06-13 00:02:13 发布