#define NULL ((void *)0)
#define MALLOC(dataType, n) malloc( sizeof(dataType)*n )
#define SCANF(pVal, val) scanf("%d", &val)
#define FREE(Point) free(Point)
#define EXIT(val) exit(val)
typedef struct Node{
int data;
struct Node *pNext;
}NODE_T, *PNODE_T;
PNODE_T Create_Loop_List(void);
void Traverse_Loop_List(PNODE_T pHead);
bool Insert_Loop_List(PNODE_T pHead, int pos, int val);
bool Delete_Loop_List(PNODE_T pHead, int pos, int *val);
void Sort_Loop_List(PNODE_T pHead, int length);
int Loop_List_length(PNODE_T pHead);
int main(void)
{
int val;
PNODE_T pHead;
pHead = Create_Loop_List();
Insert_Loop_List(pHead, 1, 22);
Insert_Loop_List(pHead, 1, 44);
Insert_Loop_List(pHead, 1, 55);
Insert_Loop_List(pHead, 1, 66);
Insert_Loop_List(pHead, 1, 33);
Insert_Loop_List(pHead, 1, 77);
Insert_Loop_List(pHead, 1, 88);
Insert_Loop_List(pHead, 1, 99);
Traverse_Loop_List(pHead);
Sort_Loop_List(pHead, Loop_List_length(pHead));
Traverse_Loop_List(pHead);
if(Delete_Loop_List(pHead, 1, &val)){
printf("Delete Success, val = %d\n", val);
}else{
printf("Delete Fail!\n");
}
if(Delete_Loop_List(pHead, 6, &val)){
printf("Delete Success, val = %d\n", val);
}else{
printf("Delete Fail!\n");
}
if(Delete_Loop_List(pHead, 2, &val)){
printf("Delete Success, val = %d\n", val);
}else{
printf("Delete Fail!\n");
}
Traverse_Loop_List(pHead);
return 0;
}
PNODE_T Create_Loop_List(void)
{
PNODE_T pHead = (PNODE_T)MALLOC(NODE_T, 1);
if(NULL == pHead){
printf("Allocation Fail!\n");
EXIT(-1);
}else{
PNODE_T pTail = (PNODE_T)MALLOC(NODE_T, 1);
if(NULL == pHead){
printf("Allocation Fail!\n");
EXIT(-1);
}else{
pTail->data = 0;
pTail->pNext = pHead;
pHead->pNext = pTail;
}
return pHead;
}
}
void Traverse_Loop_List(PNODE_T pHead)
{
PNODE_T p = pHead->pNext;
printf("Traverse Result:\n");
while(p != pHead)
{
printf("%xH: %d\n", p,p->data);
p = p->pNext;
}
return;
}
int Loop_List_length(PNODE_T pHead)
{
int cnt = 0;
PNODE_T p = pHead->pNext;
while(p != pHead)
{
p = p->pNext;
cnt++;
}
return cnt;
}
//pos位置的节点前面插入一个新节点,pos大于0
bool Insert_Loop_List(PNODE_T pHead, int pos, int val)
{
int i = 0;
PNODE_T p = pHead;
while((p->pNext != pHead)&&(i < pos-1))
{
p = p->pNext;
i++;
}
//printf("- Insert_Loop_List -%xH, i:%d, pos:%d\n",p,i,pos);
if((p->pNext == pHead)||(i > pos-1)) return false;
PNODE_T pNew = (PNODE_T)MALLOC(NODE_T, 1);
pNew->data = val;
pNew->pNext = p->pNext;
p->pNext = pNew;
return true;
}
//删除循环链表中pos位置上的结点,把该结点的数据域值赋值给(*val)
bool Delete_Loop_List(PNODE_T pHead, int pos, int *val)
{
int i = 0;
PNODE_T p = pHead;
while((p->pNext != pHead)&&(i < pos-1))
{
p = p->pNext;
i++;
}
if((p->pNext == pHead)||(i > pos-1)) return false;
PNODE_T pBuff = p->pNext;
(*val) = p->pNext->data;
p->pNext = p->pNext->pNext;
FREE(pBuff);
return true;
}
bool Is_Empty(PNODE_T pHead)
{
if(NULL == pHead->pNext)
return true;
else
return false;
}
void Sort_Loop_List(PNODE_T pHead, int length)
{
if( Is_Empty(pHead) ){
printf("List is empty!\n");
}else{
int i, j, t;
PNODE_T p, q;
for(i=0,p=pHead->pNext; i<length-1; i++,p=p->pNext)
{
for(j=i+1,q=p->pNext; j<length; j++,q=q->pNext)
{
if(p->data > q->data)
{
t = p->data;
p->data = q->data;
q->data = t;
}
}
}
}
return;
}