单向循环链表:
1、设计节点
2、初始化空链表
3、新建一个节点
4、插入节点
5、删除节点
6、移动节点
7、查找节点
8、遍历链表
代码:
#include<stdlib.h>
#include<stdbool.h>
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
typedef struct node
{
int data;
struct node *next;
}NODE,*PNODE;
PNODE init_list(void)
{
PNODE head = (PNODE)malloc(sizeof(NODE));
head->next = head;
return head;
}
PNODE make_node(PNODE head,int data)
{
PNODE new = malloc(sizeof(NODE));
new->data = data;
new->next = head;
}
bool insert_head(PNODE p,PNODE new)
{
new->next = p->next;
p->next = new;
printf("insert succes!\n");
return true;
}
PNODE find_list(PNODE p,int n)
{
PNODE t = p;
while(t->next != p)
{
t=t->next;
if(n == t->data)
{
break;
}
}
printf(“find succes!\n”);
return t == p?NULL:t;
}
void delete_list(PNODE delete)
{
PNODE t = delete;
while(t->next != delete)
{
t=t->next;
}
t->next = delete->next;
delete->next = NULL;
printf(“delete succes!\n”);
}
void move_node(PNODE p,PNODE pos)
{
delete_list§;
insert_head(pos,p);
printf(“move_node succes!\n”);
}
void show_list(PNODE head)
{
PNODE t=head;
while(t->next != head)
{
t=t->next;
printf("%d\t",t->data);
}
printf("\n");
}
int main()
{
PNODE test,new,new1,find1,find2;
test = init_list();
new = make_node(test,3);
insert_head(test,new);
new1 = make_node(test,2);
insert_head(test,new1);
show_list(test);
find1 = find_list(test,3);
find2 = find_list(test,2);
move_node(find2,find1);
show_list(test);
delete_list(find1);
show_list(test);
return 0;
}