#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
typedef int elemtype;
typedef struct CDNode
{
union
{
int data;
int length;
};
struct CDNode* next;
struct CDNode* prior;
}CDNode, * PCDNode;
void Init_list(PCDNode plist);
bool Insert_head(PCDNode plist, int val);
bool Insert_tail(PCDNode plist, int val);
bool Del_head(PCDNode plist);
bool Del_tail(PCDNode plist);
bool Insert_pos(PCDNode plist, int pos, int val);
bool Del_pos(PCDNode plist, int pos);
PCDNode Del_val(PCDNode plist, int val);
PCDNode prior(PCDNode plist, int val);
PCDNode Get_next(PCDNode plist, int val);
bool IsEmpty(PCDNode plist);
int Get_length(PCDNode plist);
void Clear(PCDNode plist);
void Destory(PCDNode plist);
void Destory2(PCDNode plist);
void Show(PCDNode plist);
*/
#include"ctwolist1.h"
void Init_list(PCDNode plist)
{
assert(plist != NULL);
if (plist == NULL)return;
plist->next = plist;
plist->prior = plist;
}
bool Insert_pos2(PCDNode plist, int pos, int val)
{
assert(plist != NULL);
if (pos<0 || pos>Get_length(plist))return false;
CDNode* s = (CDNode*)malloc(sizeof(CDNode));
CDNode* p = plist;
assert(s != NULL);
int i = 0;
s->data = val;
while (i < pos)
{
p = p->next;
i++;
}
s->next = p->next;
s->prior = p;
p->next->prior = s;
p->next = s;
return true;
}
bool Insert_head(PCDNode plist, int val)
{
Insert_pos2(plist, 0, val);
return true;
}
bool Insert_tail(PCDNode plist, int val)
{
Insert_pos2(plist, Get_length(plist), val);
return true;
}
bool Del_pos(PCDNode plist, int pos)
{
assert(plist != NULL);
CDNode* p = plist;
int i = 0;
while (i < pos)
{
p = p->next;
i++;
}
CDNode* q = p->next;
p->next = q->next;
q->next->prior = p;
free(q);
q = NULL;
return true;
}
bool Del_head(PCDNode plist)
{
Del_pos(plist, 0);
return true;
}
bool Del_tail(PCDNode plist)
{
assert(plist != NULL);
Del_pos(plist, Get_length(plist)-1);
return true;
}
PCDNode Get_prior(PCDNode plist, int val)
{
assert(plist != NULL);
CDNode* p = plist->next;
int i = 0;
while (p != plist)
{
if (p->data == val)
{
return p->prior;
break;
}
else
p = p->next;
}
return NULL;
}
PCDNode Get_next(PCDNode plist, int val)
{
assert(plist != NULL);
CDNode* p = plist->next;
int i = 0;
while (p != plist)
{
if (p->data == val)
{
return p->next;
break;
}
else
p = p->next;
}
return NULL;
}
PCDNode Del_val(PCDNode plist, int val)
{
assert(plist != NULL);
CDNode* p = Get_prior(plist, val);
assert(p != NULL);
CDNode* q = p->next;
p->next = q->next;
q->next->prior = p;
free(q);
q = NULL;
return NULL;
}
bool IsEmpty(PCDNode plist)
{
return plist->next == NULL;
}
int Get_length(PCDNode plist)
{
int count = 0;
CDNode* p = plist->next;
while (p != plist)
{
p = p->next;
count++;
}
return count;
}
void Clear(PCDNode plist)
{
Destory(plist);
}
void Destory(PCDNode plist)
{
assert(plist != NULL);
while (plist->next != plist)
{
CDNode* p = plist->next;
plist->next = p->next;
free(p);
}
plist->next = plist;
}
void Destory2(PCDNode plist)
{
CDNode* p = plist->next;
CDNode* q = NULL;
plist->next = plist;
if (p != plist)
{
q = p->next;
free(p);
p = q;
}
}
void Show(PCDNode plist)
{
assert(plist != NULL);
CDNode* p = plist->next;
while (p != plist)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
CDNode ps;
Init_list(&ps);
for (int i = 0; i < 10; i++)
{
Insert_head(&ps, i + 1);
}
Show(&ps);
printf("length=%d\n", Get_length(&ps));
Del_head(&ps);
Show(&ps);
printf("length=%d\n", Get_length(&ps));
Del_tail(&ps);
Show(&ps);
printf("length=%d\n", Get_length(&ps));
Del_val(&ps,8);
Show(&ps);
printf("length=%d\n", Get_length(&ps));
Destory2(&ps);
printf("length=%d\n", Get_length(&ps));
}