1.自己实现单向循环链表的功能
- //loop_list.c
-
- #include"loop_list.h"
- //创建单向循环链表
- loop_p create_head()
- {
- loop_p H=(loop_p)malloc(sizeof(loop_list));
- if(H==NULL)
- {
- printf("空间申请失败\n");
- return NULL;
- }
- H->len=0;
- H->next=H;
- return H;
- }
-
- //创建结点
- loop_p create_node(datatype data)
- {
- loop_p new=(loop_p)malloc(sizeof(loop_list));
- if(new==NULL)
- {
- printf("空间申请失败\n");
- return NULL;
- }
- new->data = data;
- return new;
- }
-
- //输出
- void out_put_loop(loop_p H)
- {
- if(H==NULL)
- {
- printf("入参为空,请检查\n");
- return;
- }
- loop_p p=H->next;
- while(p!=H)
- {
- printf("%d->",p->data);
- p=p->next;
- }
- printf("%c",'H');
- putchar(10);
- }
-
- //头插
- void insert_head(loop_p H,datatype data)
- {
- if(H==NULL)
- {
- printf("入参为空,请检查\n");
- return;
- }
- loop_p new=create_node(data);
- new->next=H->next;
- H->next=new;
- H->len++;
- }
-
- //按位置插入
- void insert_pos(loop_p H,datatype data,int pos)
- {
- if(H==NULL)
- {
- printf("入参为空,请检查\n");
- return;
- }
- if(pos<=0||pos>H->len+1)
- {
- printf("位置不合理\n");
- return;
- }
- loop_p p=H;
- for(int i=0;i
-1;i++) - {
- p=p->next;
- }
- loop_p new=create_node(data);
- new->next=p->next;
- p->next=new;
- H->len++;
- }
-
- //尾删
- void del_tail(loop_p H)
- {
- if(H==NULL)
- {
- printf("入参为空,请检查\n");
- return;
- }
- loop_p p=H;
- while(p->next->next!=H)
- {
- p=p->next;
- }
- loop_p del=p->next;
- p->next=p->next->next;
- free(del);
- H->len--;
- }
-
- //按位置删除
- void del_pos(loop_p H,int pos)
- {
- if(H==NULL)
- {
- printf("入参为空,请检查\n");
- return;
- }
- if(pos<=0||pos>H->len)
- {
- printf("位置不合理\n");
- return;
- }
- loop_p p=H;
- for(int i=0;i
-1;i++) - {
- p=p->next;
- }
- loop_p del=p->next;
- p->next = p->next->next;
- free(del);
- H->len--;
- }
- //loop_list.h
-
- #ifndef __LOOP_LIST_H__
- #define __LOOP_LIST_H__
- #include
- #include
- #include
-
- typedef int datatype;
- typedef struct loop_list
- {
- union
- {
- int len;
- datatype data;
- };
- struct loop_list *next;
- }loop_list,*loop_p;
-
- //创建单向循环链表
- loop_p create_head();
- //创建结点
- loop_p create_node(datatype data);
- //输出
- void out_put_loop(loop_p H);
- //头插
- void insert_head(loop_p H,datatype data);
- //按位置插入
- void insert_pos(loop_p H,datatype data,int pos);
- //尾删
- void del_tail(loop_p H);
- //按位置删除
- void del_pos(loop_p H,int pos);
-
- #endif
- //main.c
-
- #include"loop_list.h"
- int main(int argc, const char *argv[])
- {
- loop_p H=create_head();
- insert_head(H,89);
- insert_head(H,55);
- insert_head(H,7);
- out_put_loop(H);
- insert_pos(H,12,2);
- out_put_loop(H);
- del_tail(H);
- out_put_loop(H);
- del_pos(H,1);
- out_put_loop(H);
- return 0;
- }

2.复习前面顺序表和链表的代码,重写链表逆置函数
- void overturn_link(link_p H)
- {
- if(H==NULL)
- {
- printf("入参为空,请检查\n");
- return;
- }
- if(link_empty(H))
- {
- printf("链表为空\n");
- return;
- }
- if(H->next->next==NULL)
- {
- printf("表中只有一个元素,无需翻转\n");
- return;
- }
- link_p p=H->next->next;
- H->next->next=NULL;
- link_p q=p->next;
- while(p!=NULL)
- {
- p->next=H->next;
- H->next=p;
- p=q;
- if(q!=NULL)
- {
- q=q->next;
- }
- }
- }