循环队列(内核为链表).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)

typedef struct Node{
	int data;
	struct Node * pNext;
}NODE_T, *PNODE_T;

typedef struct Queue_List{
	PNODE_T pFront;
	PNODE_T pRear;
}QUEUE_LIST_T, *PQUEUE_LIST_T;

boolean Create_Queue_List(PQUEUE_LIST_T pQueue_List);
void Traverse_Queue_List(PQUEUE_LIST_T pQueue_List);
bool En_Queue_List(PQUEUE_LIST_T pQueue_List, int val);
bool Out_Queue_List(PQUEUE_LIST_T pQueue_List, int * val);

int main(void)
{
	int val;
	QUEUE_LIST_T queue_list;

	Create_Queue_List(&queue_list);
	//Traverse_Queue_List(&queue_list);

	En_Queue_List(&queue_list, 33);
	En_Queue_List(&queue_list, 3);
	En_Queue_List(&queue_list, 123);
	En_Queue_List(&queue_list, -3);

	Traverse_Queue_List(&queue_list);
	
	if(Out_Queue_List(&queue_list, &val)){
		printf("Out Queue success,val = %d\n", val);
	}else{
		printf("Out Queue Fail!\n");
	}
	if(Out_Queue_List(&queue_list, &val)){
		printf("Out Queue success,val = %d\n", val);
	}else{
		printf("Out Queue Fail!\n");
	}
	
	Traverse_Queue_List(&queue_list);
	printf("\n");
	
	return 0;
}

bool Create_Queue_List(PQUEUE_LIST_T pQueue_List)
{
    PNODE_T pBase = (PNODE_T)MALLOC(NODE_T, 1);
	if(NULL == pBase){
		printf("Allocation Fail!\n");
		return false;
	}else{
		pBase->pNext = NULL;
		pBase->data = 0;
		pQueue_List->pFront = pBase;
		pQueue_List->pRear = pQueue_List->pFront;
		
		for(int i=1; i<8; i++)
		{
		    PNODE_T pNew = (PNODE_T)MALLOC(NODE_T, 1);
			if(NULL == pNew){
				printf("Allocation Fail!\n");
				return false;
			}else{
				pNew->pNext = pQueue_List->pFront;
				pNew->data = 0;
				
				pQueue_List->pRear->pNext = pNew;
				pQueue_List->pRear = pNew;
			}
    }
		pQueue_List->pRear = pQueue_List->pFront;
	}
   
	return true;

}

bool Is_Full(PQUEUE_LIST_T pQueue_List)
{
	if(pQueue_List->pRear->pNext == pQueue_List->pFront){
		printf("Queue List Space full!\n");
		return true;
	}
	else
		return false;

}

bool En_Queue_List(PQUEUE_LIST_T pQueue_List, int val)
{
	if( Is_Full(pQueue_List) ){
		printf("En Queue List Fail!\n");
		return false;
	}else{
		pQueue_List->pRear->data = val;
		pQueue_List->pRear = pQueue_List->pRear->pNext;
		return true;
	}
}

void Traverse_Queue_List(PQUEUE_LIST_T pQueue_List)
{
	PNODE_T p = pQueue_List->pFront;
	
	printf("Traverse Result:\n");
	while(p != pQueue_List->pRear)
	{
		printf("%xH: %d\n", p,p->data);
		p = p->pNext;
	}
	return;

}

bool Is_Empty(PQUEUE_LIST_T pQueue_List)
{
	if(pQueue_List->pFront == pQueue_List->pRear)
		return true;
	else
		return false;
}

bool Out_Queue_List(PQUEUE_LIST_T pQueue_List, int * val)
{
	if( Is_Empty(pQueue_List) ){
		printf("Queue List is empty!\n");
		return false;
	}else{
		(*val) = pQueue_List->pFront->data;
		pQueue_List->pFront = pQueue_List->pFront->pNext;
		return true;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值