赞
踩
学习笔记
- //队列的顺序存储结构
- #define MaxSize 100
- typedef struct{
- int data[MaxSize];
- int front;
- int rear;
- int size;
- }SqQueue;
-
- //初始化
- void InitQueue(SqQueue &Q){
- Q.front = 0;
- Q.rear = 0;
- Q.size = 0;
- }
- //判断队列是否为空
- bool QueueEmpty(SqQueue Q){
- if(Q.rear == Q.front) return true;
- else return false;
- }
- //入队
- bool EnQueue(SqQueue &Q,int x){
- //判断队列已满
- if((Q.rear+1)%MaxSize == Q.front) return false;
- Q.data[Q.rear] = x;
- Q.rear = (Q.rear+1)%MaxSize;
- Q.size++;
- return true;
- }
- //出队
- bool DeQueue(SqQueue &Q){
- //判断队空
- if(Q.rear == Q.front) return false;
- int x;
- x = Q.data[Q.front];
- Q.front = (Q.front+1)%MaxSize;
- Q.size--;
- return true;
- }
- //获得队头元素
- int GetHead(SqQueue Q){
- if(Q.rear == Q.front) return false;
- int x;
- x = Q.data[Q.front];
- return x;
- }
- //队列的长度
- int QueueLength(SqQueue Q){
- return Q.size;
- }
- #include<stdio.h>
- #include<stdlib.h>
-
- //队列的顺序存储结构
- #define MaxSize 100
- typedef struct{
- int data[MaxSize];
- int front;
- int rear;
- int size;
- }SqQueue;
-
- //初始化
- void InitQueue(SqQueue &Q){
- Q.front = 0;
- Q.rear = 0;
- Q.size = 0;
- }
-
- //判断队列是否为空
- bool QueueEmpty(SqQueue Q){
- if(Q.rear == Q.front) return true;
- else return false;
- }
-
- //入队
- bool EnQueue(SqQueue &Q,int x){
- //判断队列已满
- if((Q.rear+1)%MaxSize == Q.front) return false;
- Q.data[Q.rear] = x;
- Q.rear = (Q.rear+1)%MaxSize;
- Q.size++;
- return true;
- }
-
- //出队
- bool DeQueue(SqQueue &Q){
- //判断队空
- if(Q.rear == Q.front) return false;
- int x;
- x = Q.data[Q.front];
- printf("此时出队的为: %d\n",x);
- Q.front = (Q.front+1)%MaxSize;
- Q.size--;
- return true;
- }
-
- //获得队头元素
- int GetHead(SqQueue Q){
- if(Q.rear == Q.front) return false;
- int x;
- x = Q.data[Q.front];
- return x;
- }
-
- //队列的长度
- int QueueLength(SqQueue Q){
- return Q.size;
- }
-
- int main(){
- SqQueue Q;
- InitQueue(Q);
- int x;
- scanf("%d",&x);
- while(x != 9999){
- printf("此时入队的是:%d\n",x);
- EnQueue(Q,x);
- scanf("%d",&x);
- }
- int len;
- len = QueueLength(Q);
- printf("此时队列的长度为:%d\n",len);
- int va_head = GetHead(Q);
- printf("此时的队头元素为:%d\n",va_head);
- while (Q.front != Q.rear) DeQueue(Q);
- len = QueueLength(Q);
- printf("此时队列的长度为:%d\n",len);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。