12.17双向链表,循环链表
循环单向链表
1.头文件test.h
#ifndef __TEST_H_
#define __TEST_H_#include<stdio.h>
#include<stdlib.h>typedef struct node
{union{int len;int data;};struct node *next;
}looplink,*looplinkPtr;//创建
looplinkPtr create();//判空
int empty();
//申请节点
looplinkPtr create_node();
//尾插
int tail_add(looplinkPtr H,int e);//遍历
void show(looplinkPtr H);
//尾删
void tail_del(looplinkPtr H);
//销毁
void my_free(looplinkPtr H);#endif
2.源文件test.c
#include"test.h"//创建
looplinkPtr create()
{looplinkPtr H=(looplinkPtr)malloc(sizeof(looplink));if (NULL==H){printf("创建失败");return NULL;}H->len=0;H->next=H;printf("创建成功\n");return H;
}
//判空
int empty(looplinkPtr H)
{if(NULL==H){printf("判空失败");return -1;}return H->len==0;}
//申请节点
looplinkPtr create_node(int e)
{looplinkPtr p=(looplinkPtr)malloc(sizeof(looplink));if (NULL==p){printf("申请失败\n");return NULL;}p->data=e;p->next=NULL;return p;}
//尾插
int tail_add(looplinkPtr H,int e)
{if(NULL==H){printf("尾插失败");return 0;}
//申请节点,封装数据looplinkPtr p = create_node(e);//定义一个指针,指向最后一个节点looplinkPtr q=H;while(q->next !=H){q=q->next ;}//尾插q->next=p;p->next=H;H->len++;return 1;}
//遍历
void show(looplinkPtr H)
{if(NULL==H||empty(H)){printf("遍历失败");return;}looplinkPtr p=H;for(int i=0;i<H->len;i++){p=p->next ;printf("%d ",p->data);}putchar(10);
}//尾删
void tail_del(looplinkPtr H)
{if (NULL==H||empty(H)){printf("删除失败\n");return;}//定义一个指针,指向最后一个节点的前一个节点looplinkPtr q=H;for(int i=0;i<H->len-1;i++){q=q->next;}free(q->next);q->next=H;H->len--;return ;}//销毁
void my_free(looplinkPtr H)
{if (NULL==H){printf("销毁失败");return ;}while (H->len>0){tail_del (H);}free(H);H=NULL;printf ("销毁成功\n");
}
3.测试文件main.c
#include "test.h"int main()
{looplinkPtr H=create();tail_add (H,10);tail_add (H,20);tail_add (H,30);tail_add (H,40);tail_add (H,50);show(H);tail_del (H);tail_del (H);show(H);my_free(H);return 0 ;
}
双向循环链表
1.头文件test.h
#ifndef __TEST_H__
#define __TEST_H__#include<stdio.h>
#include<stdlib.h>typedef struct node
{union{int len;int data;};struct node *next;struct node *prior;
}doublelooplink,*doublelooplinkptr;//创建
doublelooplinkptr create();//判空
int empty(doublelooplinkptr H);
//申请节点doublelooplinkptr create_node(int e);
//尾插
int tail_add(doublelooplinkptr H,int e);
//遍历
void show(doublelooplinkptr H);
//尾删
int tail_del (doublelooplinkptr H);
//销毁
void my_free(doublelooplinkptr H);#endif
2.源文件test.c
#include "test.h"//创建
doublelooplinkptr create()
{doublelooplinkptr H=(doublelooplinkptr)malloc(sizeof(doublelooplink));if(NULL==H){printf("创建失败\n");return NULL;}H->len==0;H->next=H;H->prior=H;printf("创建成功\n");return H;}
//判空
int empty(doublelooplinkptr H)
{if (NULL==H){printf("判空失败\n");return -1;}return H->len==0;
}//申请节点doublelooplinkptr create_node(int e)
{doublelooplinkptr p=(doublelooplinkptr)malloc (sizeof(doublelooplink));if (NULL==p){printf("申请失败\n");return NULL;}p->data=e;p->next=NULL;p->prior=NULL;return p;}
//尾插
int tail_add(doublelooplinkptr H,int e)
{if (NULL==H){printf("尾插失败\n");return 0;}//申请节点 封装数据doublelooplinkptr p=create_node (e);//定义一个指针doublelooplinkptr q=H;while(q->next!= H){q=q->next;}//尾插p->next=H;p->prior=q;q->next=p;H->prior=p;H->len++;return 1;
}
//遍历
void show(doublelooplinkptr H)
{if (NULL==H){printf("遍历失败");return;}doublelooplinkptr p=H;for (int i=0;i<H->len;i++){p=p->next;printf("%d ",p->data);}putchar(10);
}
//尾删
int tail_del (doublelooplinkptr H)
{ if (NULL==H||empty(H)){printf("删除失败\n");return 0;}doublelooplinkptr q=H;
for (int i=0;i<H->len-1;i++){q=q->next;}free(q->next);q->next=H;H->prior =q->next->prior; H->len--;return 1;}
//销毁
void my_free(doublelooplinkptr H)
{if (NULL==H){printf("销毁失败");return ;}while (H->len>0){tail_del (H);}free(H);H=NULL;printf ("销毁成功\n");
}
3.测试文件main.c
#include "test.h"int main()
{doublelooplinkptr H=create();tail_add(H,10);tail_add(H,20);tail_add(H,30);tail_add(H,40);show(H);tail_del(H);tail_del(H);show(H);my_free(H); return 0;
}