思维导图:双向链表
循环链表思维导图
双向循环链表的 创建、判空、尾插、遍历、尾删、销毁的简单实现
分文件编译
创建
birCir_link.h
#ifndef __BIRCIR_LINK_H__
#define __BIRCIR_LINK_H__#include<stdio.h>
#include<stdlib.h>
#include<string.h>typedef int DataType;
typedef struct node{
union{
int len;//头节点的数据域
DataType data;//普通节点的数据域
};
struct node* next;//指针域,指向下一个节点
struct node* prior;//指针域,指向上一个节点
}birLink,*birLinkPtr;birLinkPtr create_link();
#endif
birCir_link.c
#include"birCir_link.h"
birLinkPtr create_link(){
birLinkPtr p =(birLinkPtr)malloc(sizeof(birLink));
if(p == NULL){
printf("failed to allocate memory");
return NULL;
}
p->len = 0;
p->next = p;
p->prior = p;
printf("create p success\n");
return p;
}
main.c
#include"birCir_link.h"
int main(){
birLinkPtr p =create_link();
}
效果
判空
birCir_link.h
#ifndef __BIRCIR_LINK_H__
#define __BIRCIR_LINK_H__#include<stdio.h>
#include<stdlib.h>
#include<string.h>typedef int DataType;
typedef struct node{
union{
int len;//头节点的数据域
DataType data;//普通节点的数据域
};
struct node* next;//指针域,指向下一个节点
struct node* prior;//指针域,指向上一个节点
}birLink,*birLinkPtr;birLinkPtr create_link();
int null(birLinkPtr p);
#endif
birCir_link.c
int null(birLinkPtr p){
if(p == NULL){
printf("error,p is NULL!");
return -1;
}
return p->next == p;
}
尾插/遍历
birCir_link.h
#ifndef __BIRCIR_LINK_H__
#define __BIRCIR_LINK_H__#include<stdio.h>
#include<stdlib.h>
#include<string.h>typedef int DataType;
typedef struct node{
union{
int len;//头节点的数据域
DataType data;//普通节点的数据域
};
struct node* next;//指针域,指向下一个节点
struct node* prior;//指针域,指向上一个节点
}birLink,*birLinkPtr;birLinkPtr create_link();
int null(birLinkPtr p);
int end_insert(birLinkPtr p,DataType t);
void select_link(birLinkPtr p);
#endif
birCir_link.c
int end_insert(birLinkPtr p,DataType t){
if(p == NULL){
printf("error, p is null\n");
return 0;
}
birLinkPtr h =(birLinkPtr)malloc(sizeof(birLink));
if(h == NULL){
printf("failed to allocate memory\n");
return 0;
}
h->data = t;
h->next = NULL;
h->prior = NULL;
birLinkPtr z = p;
while(z->next != p){
z = z->next;
}
h->next = p;
h->prior = z;
z->next = h;
p->len++;
return 1;
}void select_link(birLinkPtr p){
if(p == NULL || null(p)){
printf("error,p is null\n");
return;
}
birLinkPtr q = p;
for(int i=0;i<p->len;i++){
q = q->next;
printf("%d ",q->data);
}
putchar(10);
}
main.c
#include"birCir_link.h"
int main(){
birLinkPtr p =create_link();
end_insert(p,10);
end_insert(p,20);
end_insert(p,30);
end_insert(p,40);
end_insert(p,50);select_link(p);
return 0;
}
效果:
尾删
birCir_link.h
#ifndef __BIRCIR_LINK_H__
#define __BIRCIR_LINK_H__#include<stdio.h>
#include<stdlib.h>
#include<string.h>typedef int DataType;
typedef struct node{
union{
int len;//头节点的数据域
DataType data;//普通节点的数据域
};
struct node* next;//指针域,指向下一个节点
struct node* prior;//指针域,指向上一个节点
}birLink,*birLinkPtr;birLinkPtr create_link();
int null(birLinkPtr p);
int end_insert(birLinkPtr p,DataType t);
void select_link(birLinkPtr p);
int del_end(birLinkPtr p);
#endif
birCir_link.c
int del_end(birLinkPtr p){
if(p == NULL || null(p)){
printf("delete fail,p is null");
return 0;
}
birLinkPtr q = p;
for(int i=0;i<p->len;i++){
q = q->next;
}
q->prior->next = p;
free(q);
p->len--;
return 1;}
main.c
#include"birCir_link.h"
int main(){
birLinkPtr p =create_link();
end_insert(p,10);
end_insert(p,20);
end_insert(p,30);
end_insert(p,40);
end_insert(p,50);select_link(p);
del_end(p);
select_link(p);
return 0;
}
效果:
销毁
birCir_link.h
#ifndef __BIRCIR_LINK_H__
#define __BIRCIR_LINK_H__#include<stdio.h>
#include<stdlib.h>
#include<string.h>typedef int DataType;
typedef struct node{
union{
int len;//头节点的数据域
DataType data;//普通节点的数据域
};
struct node* next;//指针域,指向下一个节点
struct node* prior;//指针域,指向上一个节点
}birLink,*birLinkPtr;birLinkPtr create_link();
int null(birLinkPtr p);
int end_insert(birLinkPtr p,DataType t);
void select_link(birLinkPtr p);
int del_end(birLinkPtr p);
void del_free(birLinkPtr p);
#endif
birCir_link.c
void del_free(birLinkPtr p){
if(p == NULL || null(p)){
printf("free fail ,p is null");
return;
}
while(p->next != p){
del_end(p);
}
free(p);
p=NULL;
printf("free p success\n");
}
main.c
#include"birCir_link.h"
int main(){
birLinkPtr p =create_link();
end_insert(p,10);
end_insert(p,20);
end_insert(p,30);
end_insert(p,40);
end_insert(p,50);select_link(p);
del_end(p);
select_link(p);del_free(p);//调用销毁函数
p=NULL;//防止申请的p变成野指针
return 0;
}
效果: