循环链表.c

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值