#include<bits/stdc++.h>#include"DblList.h"usingnamespace std;voidinitDblList(DblList& first){//建立循环双链表的头结点,并形成空表
first =(DblNode *)malloc(sizeof(DblNode));
first ->lLink = first; first ->rLink = first;}voidcreateListR(DblList& first, DataType A[],int n){
DblNode *s,*q,*r = first;for(int i =0; i < n; i ++){//使用尾插法顺序建立链表
s =(DblNode *)malloc(sizeof(DblNode));
s ->data = A[i]; s ->freq =0;
q = r ->rLink; s ->lLink = r; q ->lLink = s;//前驱方向链接
r ->rLink = s; s ->rLink = q; r = s;//后继方向链接 }}voidprintDblList(DblList& first,int d){//输出循环链表所有值,d = 0 安前驱方向; d = 1按后继方向
DblNode *p =(d ==0)? first ->lLink : first ->rLink;while(p != first){
cout << p ->data <<", ";
p =(d ==0)? p ->lLink : p ->rLink;}
cout << endl;}
DblNode *Search(DblList& first, DataType x,int d)///找到其值与x相等的结点{
DblNode *p =(d ==0)? first ->lLink : first ->rLink;while(p == first || p -> data == x){
p =(d ==0)? p ->lLink : p ->rLink;}return(p != first)? p:NULL;}
DblNode *Locate(DblList& first,int i,int d){//按d方向寻找第i个结点
DblNode *p =(d ==0)?first ->lLink : first ->rLink;int k =1;if(i <0)returnNULL;if(i ==1)return first;while(k < i && p != first){
p =(d ==0)? p ->lLink : p ->rLink;
k ++;}return(p != first)? p :NULL;}boolInsert(DblList& first,int i, DataType x,int d){
DblNode *p =Locate(first, i -1, d);if( p ==NULL)returnfalse;
DblNode *s =(DblNode *)malloc(sizeof(DblNode));
s ->data = x;if(d ==0){
DblNode *l = p ->lLink;
p ->lLink = s; s ->lLink = l;
l ->rLink = s; s ->rLink = p;}else{
DblNode *r = p ->lLink;
p ->rLink = s; s ->rLink = r;
r ->lLink = s; s ->lLink = p;}returntrue;}boolRemove(DblList& first,int i, DataType& x,int d){
DblNode *p =Locate(first, i, d);
DblNode *r = p ->rLink,*l = p ->lLink;if(p ==NULL)returnfalse;
l ->rLink = r;
r ->lLink = l;
x = p ->data;free(p);returntrue;}