#include "link.h"//create DoubleLink head node
DoubleLink_p DoubleLink_create()
{DoubleLink_p H = (DoubleLink_p)malloc(sizeof(DoubleLink));if(NULL == H){printf("失败");return NULL;}H -> len = 0;H -> next = H;H -> prior = H;printf("申请成功\n");return H;
}//创建数据结点
DoubleLink_p DoubleLink_node()
{DoubleLink_p p = (DoubleLink_p)malloc(sizeof(DoubleLink));if(NULL == p){printf("失败");return NULL;}printf("请输入数据结点存储的值:");scanf("%d", &p -> data);p -> next = NULL;p -> prior = NULL;return p;
}//尾插
void tail_add(DoubleLink_p H)
{//找到尾节点DoubleLink_p q = H;for(int i = 0; i < H -> len; i++)q = q -> next;//创建结点DoubleLink_p p = DoubleLink_node();//将结点链接到尾部if(H -> next == H){p -> next = H;p -> prior = H;H -> next = p;H -> prior = p; }else{p -> next = q -> next;p -> prior = q;q -> next -> prior = p;q -> next = p; }H -> len++;
}//尾删
void tail_delete(DoubleLink_p H)
{if(empty(H)){printf("空");return;}//找到最后一个数据的地址DoubleLink_p q = H;for(int i = 0; i < H -> len; i++)q = q -> next;//删除节点H -> prior = q -> prior;q -> prior -> next = q -> next;free(q);q = NULL;H -> len--;
}//销毁链表
void DoubleLink_delete(DoubleLink_p H)
{while(H -> next != H)tail_delete(H);free(H);H = NULL;printf("销毁成功\n");
}
//回显数据
void DoubleLink_show(DoubleLink_p H)
{if(empty(H)){printf("空\n");return ;}DoubleLink_p p = H -> next;printf("————");for(int i = 0; i < H -> len; i++){printf("%d",p -> data);p = p -> next;}printf("————");putchar(10);
}//判空
int empty(DoubleLink_p H)
{return H -> next == H;
}
#include "link.h"int main()
{DoubleLink_p H = DoubleLink_create();if(NULL == H)printf("申请失败");else{int i;do{printf("1.回显\t2.尾插\t3.尾删\t0.退出\n");printf("请输入指令选项:");scanf("%d", &i);switch(i){case 1: DoubleLink_show(H);break;case 2: tail_add(H);break;case 3: tail_delete(H);break;case 0: DoubleLink_delete(H);H = NULL;printf("已退出\n");break;}}while(0 != i);}return 0;
}
#ifndef __link_H__
#define __link_H__#include <stdio.h>
#include <stdlib.h>
#include <string.h>typedef int datatype;
typedef struct node
{union{datatype data;int len;};struct node *prior;struct node *next;
}DoubleLink, *DoubleLink_p;//创建头结点和数据节点
DoubleLink_p DoubleLink_create();
DoubleLink_p DoubleLink_node();//节点功能函数
void tail_add(DoubleLink_p H);
void tail_delete(DoubleLink_p H);
void DoubleLink_show(DoubleLink_p H);
int empty(DoubleLink_p H);
void DoubleLink_delete(DoubleLink_p H);
#endif
实现效果