队列:先进先出(FIFO)。队尾入队,队头出队。
1.顺序队列
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef int dataType;
typedef struct
{
dataType data[MAXSIZE];
int front, rear;
/* front:指向队列中第一个元素
rear:指向队列中最后一个元素下一位置
*/
}SeQueue;
/* 建立空队列 */
SeQueue *initSeQueue()
{
SeQueue *Q;
Q = (SeQueue *)malloc(sizeof(SeQueue));
Q->front = 0;
Q->rear = 0;
return Q;
}
int length(SeQueue *Q)
{
return Q->rear - Q->front;
}
int isEmpty(SeQueue *Q)
{
if (length(Q) == 0) return 1;
else return -1;
}
/* 入队 */
int inQueue(SeQueue *Q, dataType x)
{
if (length(Q) == MAXSIZE) return -1;
else
{
Q->data[Q->rear] = x;
Q->rear++;
}
}
/* 出队 */
int outQueue(SeQueue *Q, dataType *elem)
{
if (isEmpty(Q) == 1) return -1;
else
{
*elem = Q->data[Q->front];
Q->front++;
return 1;
}
}
int main()
{
SeQueue *sq;
int i;
dataType x;
sq = initSeQueue();
printf("队列长度:%d\n", length(sq));
for (i = 0; i < 10; i++)
{
inQueue(sq, i);
}
printf("队列长度:%d\n", length(sq));
printf("队中元素依次为:");
for (i = 0; i < 10; i++)
{
outQueue(sq, &x);
printf("%d, ", x);
}
printf("队列长度:%d\n", length(sq));
return 0;
}
2.循环队列
队列 |
循环队列 |
随着入队出队的进行,rear、front后移,当指针移到最后,出现假溢出,浪费存储空间 |
通过循环移动指针,解决假溢出 |
2.1两种方法判满:
(1)设置计数器,num。num == 0,空;num==MAXSIZE,满。
(2)少用一元素空间。(rear+1)%MAXSIZE==front,满。
2.1判空:rear == front
下述代码使用方法2.1.(2)
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef int dataType;
typedef struct
{
dataType data[MAXSIZE];
int front, rear;
}CQueue;
/* 建立空队列 */
CQueue *initCQueue()
{
CQueue *Q;
Q = (CQueue *)malloc(sizeof(CQueue));
Q->front = 0;
Q->rear = 0;
return Q;
}
int length(CQueue *Q)
{
return (Q->rear - Q->front + MAXSIZE) % MAXSIZE;
}
int isFull(CQueue *Q)
{
if ((Q->rear + 1) % MAXSIZE == Q->front) return 1;
else return 0;
}
/* 入队 */
int inQueue(CQueue *Q, dataType x)
{
if (isFull(Q) == 1) return 0;
else
{
Q->data[Q->rear] = x;
Q->rear = (Q->rear + 1) % MAXSIZE;
}
}
/* 出队 */
int outQueue(CQueue *Q, dataType *elem)
{
if (length(Q) == 0) return -1;
else
{
*elem = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;
return 1;
}
}
int main()
{
CQueue *cq;
int i;
dataType x;
cq = initCQueue();
printf("队列长度:%d\n", length(cq));
for (i = 0; i < 15; i++)
{
inQueue(cq, i);
}
printf("队列长度:%d\n", length(cq));
printf("队中元素依次为:");
for (i = 0; i < 15; i++)
{
outQueue(cq, &x);
printf("%d, ", x);
}
printf("队列长度:%d\n", length(cq));
return 0;
}
3.链式队列
#include <stdio.h>
#include <stdlib.h>
typedef int dataType;
typedef struct node
{
dataType data;
struct node *next;
}QNode;
typedef struct
{
QNode *front;
QNode *rear;
}LQueue;
/* 建立空队列 */
LQueue *initLQueue();
/* 返回队列长度 */
int length(LQueue *Q);
/* 队列为空:返回1;否则返回0 */
int isEmpty(LQueue *Q);
/* 入队成功:返回1;否则返回0 */
int inQueue(LQueue *Q, dataType x);
/* 出队成功:返回1;否则返回0 */
int outQueue(LQueue *Q);
/* 遍历链队列并输出 */
void traverseLQueue(LQueue *Q);
/* 销毁队列 */
void destroyLQueue(LQueue *Q);
int main()
{
LQueue *lq;
int i;
lq = initLQueue();
printf("队列长度:%d\n", length(lq));
for (i = 0; i < 15; i++)
inQueue(lq, i);
printf("队列长度:%d\n", length(lq));
traverseLQueue(lq);
for (i = 0; i < 15; i++)
outQueue(lq);
traverseLQueue(lq);
printf("队列长度:%d\n", length(lq));
destroyLQueue(lq);
return 0;
}
/* 建立空队列 */
LQueue *initLQueue()
{
LQueue *Q;
QNode *p;
Q = (LQueue *)malloc(sizeof(LQueue));
p = (QNode *)malloc(sizeof(QNode));
p->next = NULL;
Q->front = p;
Q->rear = p;
return Q;
}
int length(LQueue *Q)
{
int c = 0;
QNode *p = Q->front;
while (p->next) //while(p != Q->rear)
{
c++;
p = p->next;
}
return c;
}
int isEmpty(LQueue *Q)
{
return Q->front == Q->rear ? 1 : 0;
}
/* 入队 */
int inQueue(LQueue *Q, dataType x)
{
QNode *p;
p = (QNode *)malloc(sizeof(QNode));
p->data = x;
p->next = NULL;
Q->rear->next = p;
Q->rear = p;
return 1;
}
/* 出队 */
int outQueue(LQueue *Q)
{
if (isEmpty(Q) == 1) return 0;
else
{
QNode *p;
p = Q->front->next;
Q->front->next = p->next;
free(p);
if (Q->front->next == NULL)
Q->rear = Q->front;
return 1;
}
}
/* 遍历链队列 */
void traverseLQueue(LQueue *Q)
{
if (isEmpty(Q) == 1) printf("队列为空!\n");
else
{
QNode *p; int c;
printf("队中元素依次为:");
p = Q->front;
while (p->next)
{
p = p->next;
printf("%d, ", p->data);
}
printf("\n");
}
}
/* 销毁队列 */
void destroyLQueue(LQueue *Q)
{
QNode *p = Q->front->next;
while(p)
{
Q->front->next = p->next;
free(p);
p = Q->front->next;
}
free(Q->front);
free(Q);
Q=NULL;
printf("销毁成功!\n");
}