双向循环链表的实现【C语言】
表示
双向循环链表的实现
#pragma once
typedef int elemType;
typedef struct Node
{
union {
int length;
elemType data;
};
struct Node *prior;
struct Node *next;
}DoubleCircleList;
void InitDoubleCircleList(DoubleCircleList* head);
bool InsertDoubleCircleList(DoubleCircleList* head,elemType value,int pos);
bool InsertDoubleCircleListHead(DoubleCircleList* head,elemType value);
bool InsertDoubleCircleListRear(DoubleCircleList* head,elemType value);
bool DeleteDoubleCircleList(DoubleCircleList* head,int pos);
bool DeleteDoubleCircleListHead(DoubleCircleList* head);
bool DeleteDoubleCircleListRear(DoubleCircleList* head);
int GetLength(DoubleCircleList* head);
bool IsEmpty(DoubleCircleList* head);
void DestoryDoubleCircleList(DoubleCircleList* head);
void ShowList(DoubleCircleList* head);
#include<stdio.h>
#include<stdlib.h>
#include"double_circle_list.h"
static DoubleCircleList* ApplyNewNode(DoubleCircleList* pri,DoubleCircleList* nex,int value)
{
DoubleCircleList *new_node = (DoubleCircleList*)malloc(sizeof(DoubleCircleList));
if (new_node == NULL) return NULL;
new_node->data = value;
new_node->next = nex;
new_node->prior =pri;
return new_node;
}
static bool InsertNodeAfter(DoubleCircleList* point,elemType value)
{
DoubleCircleList *new_node = ApplyNewNode(point,point->next ,value);
if (new_node == NULL) return false;
point->next->prior = new_node;
point->next = new_node;
return true;
}
void InitDoubleCircleList(DoubleCircleList* head)
{
if(head==NULL) exit(0);
head->next=head->prior=head;
head->length =0;
}
bool InsertDoubleCircleList(DoubleCircleList* head,elemType value,int pos)
{
if(head==NULL) exit(0);
if(pos<0) return false;
pos %= (head->length + 1);
DoubleCircleList *p=head;
while(pos)
{
p=p->next ;
pos--;
}
if(InsertNodeAfter(p,value))
{
head->length ++;
return true;
}
return false;
}
bool InsertDoubleCircleListHead(DoubleCircleList* head,elemType value)
{
return InsertDoubleCircleList(head,value,0);
}
bool InsertDoubleCircleListRear(DoubleCircleList* head,elemType value)
{
if(head==NULL) exit(0);
DoubleCircleList *p=head->prior ;
if (InsertNodeAfter(p, value))
{
head->length++;
return true;
}
return false;
}
bool DeleteDoubleCircleList(DoubleCircleList* head,int pos)
{
if(head==NULL) exit(0);
if (pos < 0 || head->length == 0) return false;
pos %= head->length;
DoubleCircleList *p=head;
while(pos)
{
p=p->next ;
pos--;
}
DoubleCircleList *q=p->next ;
q->next->prior = p;
p->next = q->next;
free(q);
head->length--;
return true;
}
bool DeleteDoubleCircleListHead(DoubleCircleList* head)
{
return DeleteDoubleCircleList(head,0);
}
bool DeleteDoubleCircleListRear(DoubleCircleList* head)
{
if(head==NULL) exit(0);
DoubleCircleList *p=head->prior ;
p->next->prior =p->prior ;
p->prior->next =p->next ;
free(p);
head->length--;
return true;
}
int GetLength(DoubleCircleList* head)
{
if(head==NULL) exit(0);
return head->length ;
}
bool IsEmpty(DoubleCircleList* head)
{
if(head==NULL) exit(0);
return head->length ==0;
}
void DestoryDoubleCircleList(DoubleCircleList* head)
{
if(head==NULL) exit(0);
while(!IsEmpty(head))
{
DeleteDoubleCircleListHead(head);
}
}
void ShowList(DoubleCircleList* head)
{
if(head==NULL) exit(0);
DoubleCircleList *p=head->next ;
int len=head->length ;
printf("正向打印:\n");
while(len)
{
printf("%d ",p->data );
p=p->next ;
len--;
}
printf("逆向打印:\n");
while(p!=head)
{
printf("%d ",p->data );
p=p->prior ;
}
printf("\n");
}
int main()
{
DoubleCircleList head;
InitDoubleCircleList(&head);
for(int i=1;i<6;i++)
{
InsertDoubleCircleListHead(&head,i);
}
ShowList(&head);
DeleteDoubleCircleList(&head,3);
ShowList(&head);
printf("链表长度:%d\n",GetLength(&head));
return 0;
}