0%

Queue of Stack

链队列

链队列结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14

typedef int QElemType;

typedef struct QNode
{
QElemType data;
struct QNode *next;
} QNode, *QueuePtr;

typedef struct
{
QueuePtr front, rear;
} LinkQueue;

入队操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14

Status EnQueue(LinkQueue *Q, QElemType e)
{
QueuePtr s = (QueuePtr)malloc(sizeof(QNode));
if (!s)
exit(OVERFLOW);
s->data = e;
s->next = NULL;
Q->rear->next = s;
Q->rear = s;

return OK;
}

出队操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Status DeQueue(LinkQueue *Q, QElemType *e)
{
QueuePtr p;
if (Q->front == Q->rear)
return ERROR;
p = Q->front->next;
*e = p->data;
Q->front->next = p->next;
if (Q->rear == p)
Q->rear = Q->front;
free(p);

return OK;
}

Welcome to my other publishing channels