赞
踩
- #define MAX 10
- #include<stdio.h>
- #include<stdlib.h>
- typedef int elemtype;
-
- //计数法
- typedef struct SqQueue
- {
- int queue[MAX];
- int front;
- int rear;
- int count;
- }SqQueue;
-
- //初始化
- void InitQueue(SqQueue* Q)
- {
- Q->front = 0;
- Q->rear = 0;
- Q->count = 0;
- }
-
- //判断队列是否为空
- int SqQueueempty(SqQueue Q)
- {
- if (Q.count != 0)
- return 1;
- else
- return 0;
- }
-
- //判断队列是否为满
- int SqQueuefull(SqQueue Q)
- {
- if (Q.count == MAX)
- return 1;
- else
- return 0;
- }
-
- //入队
- int EnSqQueue(SqQueue* Q, elemtype e)
- {
- if (SqQueuefull(*Q))
- return 0;
- Q->queue[Q->rear] = e;
- Q->rear++;
- Q->count++;
- return 1;
- }
-
- //出队
- int DeSqQueue(SqQueue* Q, elemtype* e)
- {
- //if(SqQueueempty(*Q))
- //return 0;
- *e = Q->queue[Q->front];
- Q->front = (Q->front + 1) % MAX;
- Q->count--;
- return 1;
- }
-
- int main()
- {
- SqQueue Q;
- int i;
- elemtype e=1;
- InitQueue(&Q);
- for (i = 1; i <= 5; i++)
- EnSqQueue(&Q, i);
- while (SqQueueempty(Q))
- {
- DeSqQueue(&Q, &e);
- printf("%8d", e);
- }
- printf("\n");
- }
- #define MAX 10
- #include<stdio.h>
- #include<stdlib.h>
- typedef int elemtype;
-
- //flag法
- typedef struct SqQueue
- {
- int queue[MAX];
- int front;
- int rear;
- int flag;
- }SqQueue;
-
- //初始化
- void InitQueue(SqQueue* Q)
- {
- Q->front = 0;
- Q->rear = 0;
- Q->flag = 0;
- }
-
- //判断队列是否为空
- int SqQueueempty(SqQueue Q)
- {
- if (Q.front==Q.rear&&Q.flag==0)
- return 0;
- else
- return 1;
- }
-
- //判断队列是否为满
- int SqQueuefull(SqQueue Q)
- {
- if (Q.front == Q.rear && Q.flag ==1)
- return 1;
- else
- return 0;
- }
-
- //入队
- int EnSqQueue(SqQueue* Q, elemtype e)
- {
- if (SqQueuefull(*Q))
- return 0;
- Q->queue[Q->rear] = e;
- Q->rear++;
- Q->flag = 1;
- return 1;
- }
-
- //出队
- int DeSqQueue(SqQueue* Q, elemtype* e)
- {
- //if(SqQueueempty(*Q))
- //return 0;
- *e = Q->queue[Q->front];
- Q->front = (Q->front + 1) % MAX;
- Q->flag = 0;
- return 1;
- }
-
- int main()
- {
- SqQueue Q;
- int i;
- elemtype e=0;
- InitQueue(&Q);
- for (i = 1; i <= 5; i++)
- EnSqQueue(&Q, i);
- while (SqQueueempty(Q))
- {
- DeSqQueue(&Q, &e);
- printf("%8d", e);
- }
- printf("\n");
- }
(Q.rear+1)&MAX==Q.front;
- #define MAX 10
- #include<stdio.h>
- #include<stdlib.h>
- typedef int elemtype;
-
- //少用一个元素法
- typedef struct SqQueue
- {
- int queue[MAX];
- int front;
- int rear;
-
- }SqQueue;
-
- //初始化
- void InitQueue(SqQueue* Q)
- {
- Q->front = 0;
- Q->rear = 0;
-
- }
-
- //判断队列是否为空
- int SqQueueempty(SqQueue Q)
- {
- if (Q.front==Q.rear) //此时少用一个元素,判空的条件变得唯一
- return 0;
- else
- return 1;
- }
-
- //判断队列是否为满
- int SqQueuefull(SqQueue Q)
- {
- if (Q.front==(Q.rear+1)%MAX) //判满条件为rear+1后再求余与front比较
- return 1;
- else
- return 0;
- }
-
- //入队
- int EnSqQueue(SqQueue* Q, elemtype e)
- {
- if (SqQueuefull(*Q))
- return 0;
- Q->queue[Q->rear] = e;
- Q->rear++;
-
- return 1;
- }
-
- //出队
- int DeSqQueue(SqQueue* Q, elemtype* e)
- {
- //if(SqQueueempty(*Q))
- //return 0;
- *e = Q->queue[Q->front];
- Q->front = (Q->front + 1) % MAX;
-
- return 1;
- }
-
- int main()
- {
- SqQueue Q;
- int i;
- elemtype e=0;
- InitQueue(&Q);
- for (i = 1; i <= 5; i++)
- EnSqQueue(&Q, i);
- while (SqQueueempty(Q))
- {
- DeSqQueue(&Q, &e);
- printf("%8d", e);
- }
- printf("\n");
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。