#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *pNext;
} NODE, *PNODE;
typedef struct Queue
{
PNODE pFront;
PNODE pRear;
} QUEUE, *PQUEUE;
void init(PQUEUE);
void show(PQUEUE);
void push(PQUEUE, int);
bool pop(PQUEUE, int *);
bool isEmpty(PQUEUE);
int main(void)
{
PQUEUE pQ = (PQUEUE)malloc(sizeof(QUEUE));
init(pQ);
push(pQ, 1);
push(pQ, 2);
push(pQ, 3);
int val;
pop(pQ, &val);
pop(pQ, &val);
show(pQ);
return 0;
}
void init(PQUEUE pQueue)
{
pQueue->pRear = pQueue->pFront = NULL;
}
void push(PQUEUE pQueue, int val)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
pNew->data = val;
pNew->pNext = NULL;
if (isEmpty(pQueue))
{
pQueue->pFront = pNew;
}
else
{
pQueue->pRear->pNext = pNew;
}
pQueue->pRear = pNew;
}
bool pop(PQUEUE pQueue, int *val)
{
if (isEmpty(pQueue))
{
printf("队列为空");
return false;
}
*val = pQueue->pFront->data;
PNODE pTemp = pQueue->pFront;
pQueue->pFront = pTemp->pNext;
free(pTemp);
pTemp = NULL;
return true;
}
void show(PQUEUE pQueue)
{
while (pQueue->pFront != NULL)
{
printf("%d", pQueue->pFront->data);
pQueue->pFront = pQueue->pFront->pNext;
}
}
bool isEmpty(PQUEUE pQueue)
{
if (pQueue->pFront == NULL)
{
return true;
}
return false;
}
C语言队列的算法操作
最新推荐文章于 2022-09-24 20:22:29 发布