您的位置:首页 > 其它

队列(内核为单链表).c

2017-03-13 20:04 148 查看
#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;

void Init_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;

Init_Queue_List(&queue_list);

//En_Queue_List(&queue_list, 33);
En_Queue_List(&queue_list, -90);
//En_Queue_List(&queue_list, 21);
En_Queue_List(&queue_list, 68);
//En_Queue_List(&queue_list, 5);
//En_Queue_List(&queue_list, 133);

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");
}
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);

return 0;
}

void Init_Queue_List(PQUEUE_LIST_T pQueue_List)
{
PNODE_T pBase = (PNODE_T)MALLOC(NODE_T, 1);
if(NULL == pBase){
printf("Allocation Fail!\n");
}else{
pBase->pNext = NULL;
pBase->data = 0;
pQueue_List->pFront = pBase;
pQueue_List->pRear = pQueue_List->pFront;
}

}

bool En_Queue_List(PQUEUE_LIST_T pQueue_List, int val)
{
PNODE_T pNew = (PNODE_T)MALLOC(NODE_T, 1);
if(NULL == pNew){
printf("Allocation Fail!\n");
return false;
}else{
pNew->pNext = NULL;
pNew->data = 0;

pQueue_List->pRear->data = val;
pQueue_List->pRear->pNext = pNew;
pQueue_List->pRear = pNew;
return true;
}
}

void Traverse_Queue_List(PQUEUE_LIST_T pQueue_List)
{
PNODE_T p = pQueue_List->pFront;

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: