#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;
}
}
循环队列(内核为链表).c
最新推荐文章于 2023-05-20 11:20:45 发布