赞
踩
定义和结构
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<math.h>
- #include<iostream>
- #include<algorithm>//sort函数要用
-
- #define MAXSIZE 100
- #define ElemType int
- #define Status int
- enum TF{ERROR,OK};
-
- using namespace std;//与上面某些函数库搭配使用
-
-
- typedef struct
- {
- ElemType *base;
- int front;
- int rear;
- }SqQueue;
- Status InitQueue(SqQueue &Q)
- {
- Q.base=new ElemType[MAXSIZE];
- if(!Q.base)
- { printf("分配失败!"); exit(0);}
- Q.front=Q.rear=0;
- return OK;
- }
- Status DestroyQueue(SqQueue &Q)
- {
- if(Q.front==Q.rear)
- {
- printf("队空!\n");
- return ERROR;
- }
- Q.front=Q.rear=0;
- printf("队列已销毁!\n");
- return OK;
- }
-
- Status ClearQueue(SqQueue &Q)
- {
- if(Q.front==Q.rear)
- {
- printf("队空!\n");
- return ERROR;
- }
- Q.front=Q.rear=0;
- printf("队列已清空!\n");
- return OK;
- }
- bool QueueEmpty(SqQueue Q)
- {
- if(Q.rear==Q.front) return true;
- return false;
- }
(对于非循环队列,尾指针和头指针的差值就是队列长度,而对于循环队列来说,差值可能为负数,所以需要将差值加上MAXSIZE然后与MAXSIZE求余)
- int QueueLength(SqQueue Q)
- {
- return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
- }
- Status GetTop(SqQueue Q,ElemType &e)
- {
- if(Q.front==Q.rear)
- {
- printf("队空!\n");
- exit(0);
- }
- else
- e=Q.base[Q.rear-1];
- return OK;
- }
- Status EnQueue(SqQueue &Q,ElemType e)
- {
- if((Q.rear+1)%MAXSIZE==Q.front)
- {
- printf("队列已满!");
- return ERROR;
- }
- else
- {
- Q.base[Q.rear]=e;
- Q.rear=(Q.rear+1)%MAXSIZE;
- return OK;
- }
- }
- Status DeQueue(SqQueue &Q,ElemType &e)
- {
- if(Q.front==Q.rear)
- {
- printf("队空!\n");
- return ERROR;
- }
- e=Q.base[Q.rear-1];//注意
- Q.rear--;
- return OK;
- }
- Status QueueTraverse(SqQueue Q)
- {
- if(Q.front==Q.rear)
- {
- printf("队空!\n");
- return ERROR;
- }
- printf("队列元素:");
- for(int i=Q.front;i<Q.rear;i++)
- {
- printf("%d ",Q.base[i]);
- }
- printf("\n");
- return OK;
- }
- void Print(SqQueue Q)
- {
- for(int i=Q.front;i<Q.rear;i++)
- {
- printf("%d ",Q.base[i]);
- }
- printf("\n");
- }
- void menu()
- {
- printf("********1.入队 2.出队*********\n");
- printf("********3.取队顶 4.清空队*******\n");
- printf("********5.遍历 6.销毁*********\n");
- printf("********7.队长度 8.退出*********\n");
- }
- //以下是功能函数
-
- void enqueue(SqQueue &Q)
- {
- int n;
- ElemType e;
- int flag;
- printf("请输入入队元素个数(>=1):");
- scanf("%d",&n);
- for(int i=0;i<n;i++)
- {
- printf("请输入第%d个元素的值:",i+1);
- scanf("%d",&e);
- flag=EnQueue(Q,e);
- if(flag)printf("%d已入队\n",e);
- }
- printf("当前队列元素为:");
- Print(Q);
-
-
- }
-
- void dequeue(SqQueue &Q)
- {
- ElemType e;
- if(DeQueue(Q,e))
- {
- printf("出队元素为:%d\n",e);
- printf("剩余元素为:");
- Print(Q);
- }
- }
-
- void gettop(SqQueue &Q)
- {
- ElemType e;
- if(GetTop(Q,e))
- {
- printf("队顶元素为:%d\n",e);
-
- }
- }
-
- void queuelength(SqQueue &Q)
- {
- printf("队的长度为:%d\n",QueueLength(Q));
- printf("此时队的元素:");
- Print(Q);
- }
- int main()
- {
- SqQueue Q;
- InitQueue(Q);
- int choice;
- while(1)
- {
- menu();
- printf("请输入菜单号:");
- scanf("%d",&choice);
- if(choice==8) break;
- switch(choice)
- {
- case 1:enqueue(Q);break;
- case 2:dequeue(Q);break;
- case 3:gettop(Q);break;
- case 4:ClearQueue(Q);break;
- case 5:QueueTraverse(Q);break;
- case 6:DestroyQueue(Q);break;
- case 7:queuelength(Q);break;
- default:printf("输入错误!");
- }
- }
- return 0;
- }
代码整合
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<math.h>
- #include<iostream>
- #include<algorithm>//sort函数要用
-
- #define MAXSIZE 100
- #define ElemType int
- #define Status int
- enum TF{ERROR,OK};
-
- using namespace std;//与上面某些函数库搭配使用
-
-
- typedef struct
- {
- ElemType *base;
- int front;
- int rear;
- }SqQueue;
-
- Status InitQueue(SqQueue &Q)
- {
- Q.base=new ElemType[MAXSIZE];
- if(!Q.base)
- { printf("分配失败!"); exit(0);}
- Q.front=Q.rear=0;
- return OK;
- }
-
- Status DestroyQueue(SqQueue &Q)
- {
- if(Q.front==Q.rear)
- {
- printf("队空!\n");
- return ERROR;
- }
- Q.front=Q.rear=0;
- printf("队列已销毁!\n");
- return OK;
- }
-
- Status ClearQueue(SqQueue &Q)
- {
- if(Q.front==Q.rear)
- {
- printf("队空!\n");
- return ERROR;
- }
- Q.front=Q.rear=0;
- printf("队列已清空!\n");
- return OK;
- }
-
- bool QueueEmpty(SqQueue Q)
- {
- if(Q.rear==Q.front) return true;
- return false;
- }
-
- int QueueLength(SqQueue Q)
- {
- return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
- }
-
- Status GetTop(SqQueue Q,ElemType &e)
- {
- if(Q.front==Q.rear)
- {
- printf("队空!\n");
- exit(0);
- }
- else
- e=Q.base[Q.rear-1];
- return OK;
- }
-
- Status EnQueue(SqQueue &Q,ElemType e)
- {
- if((Q.rear+1)%MAXSIZE==Q.front)
- {
- printf("队列已满!");
- return ERROR;
- }
- else
- {
- Q.base[Q.rear]=e;
- Q.rear=(Q.rear+1)%MAXSIZE;
- return OK;
- }
- }
-
- Status DeQueue(SqQueue &Q,ElemType &e)
- {
- if(Q.front==Q.rear)
- {
- printf("队空!\n");
- return ERROR;
- }
- e=Q.base[Q.rear-1];//注意
- Q.rear--;
- return OK;
- }
-
- Status QueueTraverse(SqQueue Q)
- {
- if(Q.front==Q.rear)
- {
- printf("队空!\n");
- return ERROR;
- }
- printf("队列元素:");
- for(int i=Q.front;i<Q.rear;i++)
- {
- printf("%d ",Q.base[i]);
- }
- printf("\n");
- return OK;
- }
-
- void Print(SqQueue Q)
- {
- for(int i=Q.front;i<Q.rear;i++)
- {
- printf("%d ",Q.base[i]);
- }
- printf("\n");
- }
-
- void menu()
- {
- printf("********1.入队 2.出队*********\n");
- printf("********3.取队顶 4.清空队*******\n");
- printf("********5.遍历 6.销毁*********\n");
- printf("********7.队长度 8.退出*********\n");
- }
-
- //以下是功能函数
-
- void enqueue(SqQueue &Q)
- {
- int n;
- ElemType e;
- int flag;
- printf("请输入入队元素个数(>=1):");
- scanf("%d",&n);
- for(int i=0;i<n;i++)
- {
- printf("请输入第%d个元素的值:",i+1);
- scanf("%d",&e);
- flag=EnQueue(Q,e);
- if(flag)printf("%d已入队\n",e);
- }
- printf("当前队列元素为:");
- Print(Q);
-
-
- }
-
- void dequeue(SqQueue &Q)
- {
- ElemType e;
- if(DeQueue(Q,e))
- {
- printf("出队元素为:%d\n",e);
- printf("剩余元素为:");
- Print(Q);
- }
- }
-
- void gettop(SqQueue &Q)
- {
- ElemType e;
- if(GetTop(Q,e))
- {
- printf("队顶元素为:%d\n",e);
-
- }
- }
-
- void queuelength(SqQueue &Q)
- {
- printf("队的长度为:%d\n",QueueLength(Q));
- printf("此时队的元素:");
- Print(Q);
- }
-
- int main()
- {
- SqQueue Q;
- InitQueue(Q);
- int choice;
- while(1)
- {
- menu();
- printf("请输入菜单号:");
- scanf("%d",&choice);
- if(choice==8) break;
- switch(choice)
- {
- case 1:enqueue(Q);break;
- case 2:dequeue(Q);break;
- case 3:gettop(Q);break;
- case 4:ClearQueue(Q);break;
- case 5:QueueTraverse(Q);break;
- case 6:DestroyQueue(Q);break;
- case 7:queuelength(Q);break;
- default:printf("输入错误!");
- }
- }
- return 0;
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。