1.单向循环链表
代码:
#include"loop.h"
//创建单向循环链表
loop_p create_loop_list()
{loop_p H = (loop_p)malloc(sizeof(loop));if(H==NULL){printf("空间申请失败\n");return NULL;}H->len=0;H->next=H;return H;
}
//创建节点
loop_p create_loop(datatype data)
{loop_p new = (loop_p)malloc(sizeof(loop));if(new==NULL){printf("空间申请失败\n");return NULL;}new->data=data;return new;
}
//判空
int empyt_loop(loop_p H)
{if(H==NULL){printf("入参为空,请检查\n");return -1;}return H->next==H?1:0;
}
//输出
void show_loop(loop_p H)
{if(H==NULL){printf("入参为空,请检查\n");return;}if(empyt_loop(H)){printf("链表为空\n");return;}loop_p p = H->next;while(p!=H){printf("%d->",p->data);p=p->next;}printf("NULL\n");
}
//头插
void insert_head(loop_p H,datatype data)
{if(H==NULL){printf("入参为空,请检查\n");return;}loop_p new = create_loop(data);new->next=H->next;H->next=new;H->len++;
}
//头删
void dele_head(loop_p H)
{if(H==NULL){printf("入参为空,请检查\n");return;}if(empyt_loop(H)){printf("链表为空,无需删除\n");return;}loop_p del = H->next;H->next = del->next;free(del);H->len--;
}
//尾插
void insert_tail(loop_p H,datatype data)
{if(H==NULL){printf("入参为空,请检查\n");return;}loop_p p = H->next;while(p->next!=H){p=p->next;}loop_p new = create_loop(data);new->next=p->next;p->next=new;H->len++;
}
//尾删
void dele_tail(loop_p H)
{if(H==NULL){printf("入参为空,请检查\n");return;}if(empyt_loop(H)){printf("链表为空,无需删除\n");return;}loop_p p = H;while(p->next->next!=H){p=p->next;}loop_p del = p->next;p->next H;free(del);H->len--;
}
//按位置插入
void insert_pos(loop_p H,datatype data,int pos)
{if(H==NULL){printf("入参为空,请检查\n");return;}if(pos<1||pos>H->len+1){printf("位置不合理\n");return;}loop_p p = H->next;loop_p new = create_loop(data);for(int i=1;i<pos-1;i++){p=p->next;}new->next = p->next;p->next = new;H->len++;
}
//按位置删除
void dele_pos(loop_p H,int pos)
{if(H==NULL){printf("入参为空,请检查\n");return;}if(empyt_loop(H)){printf("链表为空,无需删除\n");return;}if(pos<1||pos>H->len){printf("位置不合理\n");return;}loop_p p = H->next;for(int i=1;i<pos;i++){p=p->next;}loop_p del = p->next;p->next = del->next;free(del);H->len--;
}
//删除单向循环列表的头结点
void dele_head_loop(loop_p H)
{if(H==NULL){printf("入参为空,请检查\n");return;}loop_p p = H->next;while(p->next!=H){p=p->next;}p->next=H->next;free(H);return p->next;
}
//打印输出没有头的单向循环链表
void show_nohead_loop(loop_p fir)
{if(fir==NULL){printf("入参为空,请检查\n");return;}loop_p p = fir;do{ printf("%d->",p->data);p=p->next;}while(p!=fir);}
2.双向链表
代码:
#include"double.h"
//创建双向链表
double_p create_double()
{double_p H = (double_p)malloc(sizeof(node));if(H==NULL){printf("空间申请失败\n");return NULL;}H->len = 0;H->pri = NULL;H->next = NULL;return H;
}
//创建节点
double_p create_node(datatype data)
{double_p new = (double_p)malloc(sizeof(node));if(H==NULL){printf("空间申请失败\n");return NULL;}new->data = data;return new;
}
//头插
void insert_head(double_p H,datatype data)
{if(H==NULL){printf("入参为空,请检查\n");return;}double_p new = create_node(data);new->next = H->next;if(H->next!=NULL){H->next->pri = new;}H->next = new;new->pri = H;H->len++;
}
//判空
int empty_double(double_p H)
{if(H==NULL){printf("入参为空,请检查\n");return -1;}return H->next==NULL?1:0;
}
//输出
void show_double(double_p H)
{if(H==NULL){printf("入参为空,请检查\n");return;}if(empty_double(H)){printf("链表为空\n");return;}double_p p = H->next;while(p!=NULL){printf("%d->",p->data);p=p->next;}printf("NULL\n");
}
//头删
void dele_head(double_p H)
{if(H==NULL){printf("入参为空,请检查\n");return;}if(empty_double(H)){printf("链表为空,无需删除\n");return;}double_p del = H->next;H->next = del->next;del->next->pri = H;free(del);H->len--;
}
//尾插
void insert_tail(double_p H,datatype data)
{if(H==NULL){printf("入参为空,请检查\n");return;}double_p new = create_node(data);double_p p = H;while(p->next!=NULL){p=p->next;}p->next = new;new->pri = p;new->next = NULL;H->len++;
}
//按位置插入*
void insert_pos(double_p H,datatype data,int pos)
{if(H==NULL){printf("入参为空,请检查\n");return;}double_p new = create_node(data);double_p p = H->next;for(int i=0;i<pos-1;i++){p=p->next;}new->next = p->next;if(p->next!=NULL){p->next->pri=new;}p->next=new;new->pri=p;H->len++;
}
//按位置删除
void dele_pos(double_p H,int pos)
{if(H==NULL){printf("入参为空,请检查\n");return;}if(empty_double(H)){printf("链表为空,无需删除\n");return;}if(pos<0||pos>H->len){printf("位置不合理\n");return;}double_p p = H->next;for(int i=1;i<pos-1;i++){p=p->next;}double_p del = p->next;p->next = del->next;del->next->pri = p;free(del);H->len--;}
结果:
3.思维导图