#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
typedef int ELEM_TYPE;
typedef struct CNode
{
int data;
struct CNode* next;
}CNode,*PClist;
void Init_clist(PClist pl);
bool Insert_head(PClist pl, int val);
bool Insert_tail(PClist pl, int val);
bool Insert_pos(PClist pl, int pos, int val);
bool Del_head(PClist pl);
bool Del_tail(PClist pl);
bool Del_pos(PClist pl, int pos);
bool Del_val(PClist pl, int val);
struct CNode* Search(PClist pl, int val);
bool IsEmpty(PClist pl);
int Get_length(PClist pl);
void Show(PClist pl);
void Clear(PClist pl);
void Destroy(PClist pl);
void Destroy2(PClist pl);
PClist Get_prior(PClist pl,int val);
PClist Get_next(PClist pl,int val);
#include"clist.h"
void Init_clist(PClist pl)
{
assert(pl != NULL);
if (pl == NULL)return;
pl->next = pl;
}
bool Insert_head(PClist pl, int val)
{
assert(pl != NULL);
CNode* pnewnode = (CNode*)malloc(sizeof(CNode) * 1);
if (pnewnode == NULL)exit(1);
pnewnode->data = val;
pnewnode->next = pl->next;
pl->next = pnewnode;
return true;
}
bool Insert_tail(PClist pl, int val)
{
assert(pl != NULL);
CNode* p = pl;
CNode* pnewnode = (CNode*)malloc(sizeof(CNode) * 1);
if (pnewnode == NULL)exit(1);
pnewnode->data = val;
while (p->next!= pl)
{
p = p->next;
}
pnewnode->next = pl;
p->next = pnewnode;
return true;
}
bool Insert_pos(PClist pl, int pos, int val)
{
assert(pl != NULL);
CNode* p = pl->next;
int i = 1;
while (i < pos)
{
p = p->next;
i++;
}
CNode* pnewnode = (CNode*)malloc(sizeof(CNode) * 1);
if (pnewnode == NULL)exit(1);
pnewnode->data = val;
if (p->next != pl)
{
pnewnode->next = p->next;
}
p->next = pnewnode;
return true;
}
bool Del_head(PClist pl)
{
assert(pl != NULL);
if(pl->next==pl)
{
return false;
}
CNode* p = pl->next;
pl->next = p->next;
free(p);
return true;
}
bool Del_tail(PClist pl)
{
assert(pl != NULL&&pl->next!=pl);
CNode* pre = pl;
CNode* p = pl->next;
while (p->next != pl)
{
pre = p;
p = p->next;
}
pre->next = pl;
free(p);
return true;
}
bool Del_pos(PClist pl, int pos)
{
assert(pl!=NULL || pos < 0 || pos >= Get_length(pl));
CNode* p = pl->next;
int i = 1;
while (i < pos)
{
p = p->next;
i++;
}
CNode* q = p->next;
p->next = q->next;
free(q);
return true;
}
bool Del_val(PClist pl, int val)
{
assert(pl != NULL);
CNode* pre = pl;
CNode* p = pre->next;
while (pre->next != pl)
{
p = pre->next;
if (p->data == val)
{
pre->next = p->next;
free(p);
return true;
break;
}
else
pre = pre->next;
}
return false;
}
struct CNode* Search(PClist pl, int val)
{
assert(pl != NULL);
CNode* p = pl->next;
while (p->next != pl)
{
if (p->data == val)
{
return p;
break;
}
else
p = p->next;
}
return NULL;
}
bool IsEmpty(PClist pl)
{
return pl->next == NULL;
return true;
}
int Get_length(PClist pl)
{
int count = 0;
CNode* p = pl->next;
while (p != NULL)
{
p = p->next;
count++;
}
return count;
}
void Show(PClist pl)
{
assert(pl != NULL);
CNode* p = pl->next;
while (p != pl)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void Destroy(PClist pl)
{
CNode* p = pl->next;
while (p != pl)
{
Del_pos(pl, 0);
p = p->next;
}
}
void Clear(PClist pl)
{
Destroy(pl);
}
void Destroy2(PClist pl)
{
assert(pl != NULL && pl->next != NULL);
CNode* p = pl->next;
CNode* q;
pl->next = NULL;
while (p != NULL)
{
q = p->next;
free(p);
p = q;
}
}
PClist Get_prior(PClist pl,int val)
{
assert(pl != NULL);
CNode* pre = pl;
CNode* p = pre->next;
while (pre->next != pl)
{
p = pre->next;
if (p->data == val)
{
return pre;
}
else
pre = pre->next;
}
return NULL;
}
PClist Get_next(PClist pl, int val)
{
assert(pl != NULL);
CNode* p = Search(pl,val);
return p=NULL?NULL:p->next;
}
int main()
{
CNode clist;
Init_clist(&clist);
for (int i = 0; i < 10 ; i++)
{
Insert_tail(&clist, i + 1);
}
Show(&clist);
Insert_pos(&clist,2,99);
Show(&clist);
Del_head(&clist);
Show(&clist);
Del_tail(&clist);
Show(&clist);
Del_pos(&clist,6);
Show(&clist);
Del_val(&clist, 99);
Show(&clist);
return 0;
}