#ifndef HEAD_H__
#define HEAD_H__
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int status;
typedef int boolean;
typedef int ElementType;
#define MAX 100
void view(ElementType *);
status compare(ElementType ,ElementType);
void buildSet(ElementType a[],int length,int beg,int end);
void printSet(ElementType a[],int length);
void swap(ElementType *e1,ElementType *e2);
void quickSort(ElementType a[],int p,int r);
void randomizedQuickSort(ElementType a[],int p,int r);
#endif
#ifndef DOUBLECYCLELINKLIST_H__
#define DOUBLECYCLELINKLIST_H__
#include"head.h"
typedef struct dCLinkNode{
ElementType elem;
struct dCLinkNode *prior,*next;
}dCLinkNode,*dCLinkList;
status initDCLinkList(dCLinkList *L);
status destroyDCLinkList(dCLinkList *L);
status clearDCLinkList(dCLinkList L);
status empty(dCLinkList L);
int length(dCLinkList L);
status getElement(dCLinkList L,int i,ElementType *e);
int locateElement(dCLinkList L,ElementType e,status (*compare)(ElementType,ElementType));
status priorElement(dCLinkList L,ElementType cur_e,ElementType *pre_e);
status nextElement(dCLinkList L,ElementType cur_e,ElementType *next_e);
dCLinkList getElementPointer(dCLinkList L,int i);
status myInsert(dCLinkList L,int i,ElementType e);
status myDelete(dCLinkList L,int i,ElementType *e);
void myTraverse(dCLinkList L,void (*view)(ElementType *));
void myReTraverse(dCLinkList L,void(*view)(ElementType*));
#endif
#include"doubleCycleLinkList.h"
status initDCLinkList(dCLinkList *L)
{
*L=(dCLinkList)malloc(sizeof(dCLinkNode));
if(!(*L))
exit(ERROR);
(*L)->next=(*L)->prior=*L=*L;
return OK;
}
status destroyDCLinkList(dCLinkList *L)
{
dCLinkList q,p=(*L)->next;
while(p!=*L)
{
q=p->next;
free(p);
p=q;
}
free(*L);
*L=NULL;
return OK;
}
status clearDCLinkList(dCLinkList L)
{
dCLinkList q,p=L->next;
while(p!=L)
{
q=p->next;
free(p);
p=q;
}
L->next=L->prior=L;
return OK;
}
status empty(dCLinkList L)
{
if(L->next==L && L->prior==L)
return TRUE;
else
return FALSE;
}
int length(dCLinkList L)
{
dCLinkList p=L->next;
int i=0;
while(p!=L)
{
p=p->next;
i++;
}
return i;
}
status getElement(dCLinkList L,int i,ElementType *e)
{
dCLinkList p=L->next;
int k=1;
if(i<1 || i>length(L))
return ERROR;
while(p!=L && k<i)
{
p=p->next;
k++;
}
if(!p || k>i)
return ERROR;
*e=p->elem;
return OK;
}
int locateElement(dCLinkList L,ElementType e,status (*compare)(ElementType,ElementType))
{
dCLinkList p=L->next;
int i=0;
while(p!=L)
{
i++;
if(compare(p->elem,e))
return i;
p=p->next;
}
return 0;
}
status priorElement(dCLinkList L,ElementType cur_e,ElementType *pre_e)
{
dCLinkList p=L->next;
while(p!=L && p->elem!=cur_e)
p=p->next;
if(!p)
return ERROR;
*pre_e=p->prior->elem;
return OK;
}
status nextElement(dCLinkList L,ElementType cur_e,ElementType *next_e)
{
dCLinkList p=L->next;
while(p!=L && p->elem!=cur_e)
p=p->next;
if(!p)
return ERROR;
*next_e=p->next->elem;
return OK;
}
dCLinkList getElementPointer(dCLinkList L,int i)
{
int j;
dCLinkList p=L;
for(j=1;j<=i;j++)
p=p->next;
return p;
}
status myInsert(dCLinkList L,int i,ElementType e)
{
dCLinkList p=L->next;
dCLinkList q;
int k=1;
if(i<1 || i>length(L)+1)
return ERROR;
q=(dCLinkList)malloc(sizeof(dCLinkNode));
q->elem=e;
while(p!=L && k<i)
{
p=p->next;
k++;
}
q->prior=p->prior;
p->prior->next=q;
p->prior=q;
q->next=p;
return OK;
}
status myDelete(dCLinkList L,int i,ElementType *e)
{
dCLinkList p=L->next;
int k=1;
if(i<1 || i>length(L))
return ERROR;
while(p!=L && k<i)
{
p=p->next;
k++;
}
if(!p || k>i)
return ERROR;
*e=p->elem;
p->next->prior=p->prior;
p->prior->next=p->next;
return OK;
}
void myTraverse(dCLinkList L,void (*view)(ElementType *e))
{
dCLinkList p=L->next;
while(p!=L)
{
view(&p->elem);
p=p->next;
}
}
void myReTraverse(dCLinkList L,void(*view)(ElementType*))
{
dCLinkList p=L->prior;
while(p!=L)
{
view(&p->elem);
p=p->prior;
}
}
#include"head.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void view(ElementType *e)
{
printf("%d\t",*e);
}
status compare(ElementType e1,ElementType e2)
{
if(e1==e2)
return TRUE;
else
return FALSE;
}
void buildSet(ElementType a[],int length,int beg,int end)
{
int i;
srand((unsigned)time(NULL));
for(i=0;i<length;i++)
a[i]=rand()%(end-beg+1)+beg;
}
void printSet(ElementType a[],int length)
{
int i,count=1;
for(i=0;i<length;i++,count++)
{
printf("%d\t",a[i]);
if(count%10==0)
printf("\n");
}
}
void swap(ElementType *e1,ElementType *e2)
{
ElementType tmp;
tmp=*e1;
*e1=*e2;
*e2=tmp;
}
#include<stdio.h>
#include"head.h"
#include"doubleCycleLinkList.h"
/*
status initDCLinkList(dCLinkList *L);
status destroyDCLinkList(dCLinkList *L);
status clearDCLinkList(dCLinkList L);
status empty(dCLinkList L);
int length(dCLinkList L);
status getElement(dCLinkList L,int i,ElementType *e);
int locateElement(dCLinkList L,ElementType e,status (*compare)(ElementType,ElementType));
status priorElement(dCLinkList L,ElementType cur_e,ElementType *pre_e);
status nextElement(dCLinkList L,ElementType cur_e,ElementType *next_e);
dCLinkList getElementPointer(dCLinkList L,int i);
status myInsert(dCLinkList L,int i,ElementType e);
status myDelete(dCLinkList L,int i,ElementType *e);
void myTraverse(dCLinkList L,void (*view)(ElementType *));
void myReTraverse(dCLinkList L,void(*view)(ElementType*));
*/
int main()
{
ElementType a[MAX],e,pre_e,next_e;
int i,index;
dCLinkList L,p;
if(initDCLinkList(&L))
printf("initialize the double cycle link list and the address is : %d\n",L);
if(empty(L))
printf("the list is empty\n");
printf("the length of the list is :%d\n",length(L));
printf("generate the random array\n");
buildSet(a,MAX,0,100);
printSet(a,MAX);
for(i=0;i<MAX;i++)
myInsert(L,length(L)+1,a[i]);
printf("insert all the array's element into the list :\n");
myTraverse(L,view);
if(getElement(L,1,&e))
printf("the first element is : %d\n",e);
index=locateElement(L,55,compare);
printf("the 55's index is : %d\n",index);
if(priorElement(L,55,&pre_e))
printf("the prior element of 55 is : %d\n",pre_e);
if(nextElement(L,55,&next_e))
printf("the next element of 55 is : %d\n",next_e);
printf("delete the first 20's element and sort them in reverse\n");
for(i=0;i<20;i++)
{
myDelete(L,20-i,&e);
printf("%d\t",e);
}
printf("after delete the elements\n");
myTraverse(L,view);
p=getElementPointer(L,2);
printf("the pointer's element is %d\n",p->elem);
printf("and the next element is :%d \n",p->next->elem);
printf("and the prior element is :%d\n",p->prior->elem);
printf("using the prior pointer to traverse the list in reverse\n");
myReTraverse(L,view);
putchar('\n');
clearDCLinkList(L);
printf("clear the list and the length is %d\n",length(L));
destroyDCLinkList(&L);
printf("destroy the list and the address of L is %d\n",L);
return 0;
}