#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct Node
{
int data;
struct Node* prior;
struct Node* next;
}Node;
typedef struct circle_doublelist
{
Node header;
int length;
}cir_doublelist;
void InitList(cir_doublelist* );
int ListEmpty(cir_doublelist* );
int ListLength(cir_doublelist* );
Node* CreateList(cir_doublelist*, int );
void ListTest(cir_doublelist*, int );
void ListTest_Reverse(cir_doublelist*, int );
void ListInsert(cir_doublelist* ,int, ElemType );
void ListDelete(cir_doublelist*, int, ElemType* );
void GetElem(cir_doublelist*, int, ElemType* );
void ClearList(cir_doublelist* );
void DestroyList(cir_doublelist* );
#include <stdio.h>
#include <stdlib.h>
#include "circle_doublelist.h"
void InitList(cir_doublelist* L)
{
L->length = 0;
L->header.next = NULL;
L->header.prior = NULL;
}
int ListEmpty(cir_doublelist* L)
{
if(L->length == 0)
return 1;
else
return 0;
}
int ListLength(cir_doublelist *L)
{
return L->length;
}
Node* CreateList(cir_doublelist* L, int n)
{
if(n<=0) return NULL;
Node* pCur = &L->header;
Node* head = &L->header;
for(int i = 0; i < n; i++)
{
Node* pNew = (Node*)malloc(sizeof(Node));
if(!pNew) return NULL;
printf("请输入链表中位于 %d 处的值: \n", i);
scanf("%d", &pNew->data);
if(i == 0)
{
pCur->next = pNew;
pCur = pCur->next;
L->length++;
}
else
{
pCur->next = pNew;
pNew->prior = pCur;
pNew->next = head->next;
head->next->prior = pNew;
pCur = pCur->next;
L->length++;
}
}
return head;
}
void ListTest(cir_doublelist* L, int n)
{
if(L->length == 0)
return;
Node* pCur = &L->header;
int i = 0;
for(i = 0; i < n; i++)
{
pCur = pCur->next;
printf("The value is: %d\n", pCur->data);
}
printf("\n");
}
void ListTest_Reverse(cir_doublelist* L, int n)
{
if(L->length == 0)
return;
Node* pCur = &L->header;
int i = 0;
pCur = pCur->next->prior;
for(i = 0; i < n; i++)
{
printf("The value is: %d\n", pCur->data);
pCur = pCur->prior;
}
printf("\n");
}
void GetElem(cir_doublelist* L, int pos, ElemType* e)
{
if(pos < 0|| pos >= L->length)
return;
if(L->length == 0)
return;
Node* pCur = &L->header;
int i=0;
for(i = 0; i < pos + 1; i++)
pCur = pCur->next;
*e = pCur->data;
}
void ListDelete(cir_doublelist* L, int pos, ElemType* e)
{
if(pos < 0 || pos >= L->length)
return;
if(L->length == 0)
return;
Node* pCur = &L->header;
Node* head = &L->header;
Node* tail = &L->header;
int i = 0;
for(i = 0; i < L->length; i++)
tail = tail->next;
for(i = 0; i < pos; i++)
pCur = pCur->next;
Node* pDel = (Node*)malloc(sizeof(Node));
if(!pDel) return;
if(pos == 0)
{
pDel = pCur->next;
*e = pDel->data;
pCur->next = pDel->next;
pDel->next->prior = tail;
tail->next = pDel->next;
free(pDel);
L->length--;
return;
}
if(pos == L->length - 1)
{
pDel = pCur->next;
*e = pDel->data;
pCur->next = head->next;
head->next->prior = pCur;
tail = pCur;
free(pDel);
L->length--;
return;
}
pDel = pCur->next;
pCur->next = pDel->next;
pDel->next->prior = pCur;
free(pDel);
L->length--;
}
void ListInsert(cir_doublelist* L, int pos, ElemType e)
{
if(pos < 0 || pos > L->length)
return;
Node* pCur = &L->header;
Node* head = &L->header;
Node* tail = &L->header;
int i = 0;
for(i = 0; i < L->length; i++)
tail = tail->next;
for(i = 0; i < pos; i++)
pCur = pCur->next;
Node* pNew = (Node*)malloc(sizeof(Node));
pNew->data = e;
if(pos == 0)
{
pNew->next = pCur->next;
pCur->next->prior = pNew;
pCur->next = pNew;
pNew->prior = tail;
tail->next = pNew;
L->length++;
return;
}
if(pos == L->length)
{
pCur->next = pNew;
pNew->prior = pCur;
pNew->next = head->next;
tail->next = pNew;
L->length++;
return;
}
pNew->next = pCur->next;
pCur->next->prior = pNew;
pNew->prior = pCur;
pCur->next = pNew;
L->length++;
}
void ClearList(cir_doublelist* L)
{
while(L->length)
{
int tmp;
ListDelete(L, 0, &tmp);
}
}
void DestroyList(cir_doublelist* L)
{
ClearList(L);
free(L);
}