?? 循環隊列 .cpp
字號:
//* * * * * * * * * * * * * * * * * * * * * * * *
//*CHAPTER :3 (3_5) *
//*PROGRAM :循環隊列 *
//*CONTENT :初始化,入隊列,出隊列 *
//* * * * * * * * * * * * * * * * * * * * * * * *
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXQSIZE 5
enum BOOL{False,True};
typedef struct //定義隊列結構
{char elem[MAXQSIZE]; //隊列體
int front; //隊頭指針
int rear; //隊尾指針
}SqQueue;
void initial(SqQueue &); //初始化一個隊列
BOOL En_SqQueue(SqQueue &,char); //將一個元素入隊列
BOOL De_SqQueue(SqQueue &,char &); //將一個元素出隊列
void Print_SqQueue(SqQueue);//顯示隊列中所有元素
void main()
{SqQueue S;
char ch,j;
int flag=1;
BOOL temp;
textbackground(3); //設定屏幕顏色
textcolor(15);
//---------------------程序解說-----------------------
printf("本程序實現循環隊列的操作。\n");
printf("可以進行入隊列,出隊列等操作。\n");
//----------------------------------------------------
clrscr();
initial(S); //初始化隊列
while(flag)
{ printf("請選擇\n");
printf("1.顯示隊列所有元素\n");
printf("2.入隊列\n");
printf("3.出隊列\n");
printf("4.退出程序\n");
scanf(" %c",&j);
switch(j)
{case '1':Print_SqQueue(S); //顯示隊列中所有元素
break;
case '2':printf("請輸入隊的元素(一個字符):");
scanf(" %c",&ch); //輸入要入隊列的字符
temp=En_SqQueue(S,ch);//入隊列
if(temp==False) printf("隊列已滿!\n");
Print_SqQueue(S);
break;
case '3':temp=De_SqQueue(S,ch); //出隊列
if(temp!=False) {printf("刪除了一個元素:%c\n",ch);//若隊列不空,顯示出隊列的元素
Print_SqQueue(S);
}
else printf("隊列為空!\n");//否則隊列為空
break;
default:flag=0;printf("程序運行結束,按任意鍵結束!\n");
}
}
getch();
}
void initial(SqQueue &Q)
{//隊列初始化
Q.front=Q.rear=0; //隊頭指針及隊尾指針同置為0
}
BOOL En_SqQueue(SqQueue &Q,char ch)
{//入隊列,成功返回True,失敗返回False
if((Q.rear+1)%MAXQSIZE==Q.front) return False; //若隊列已滿,返回False
Q.elem[Q.rear]=ch;
Q.rear=(Q.rear+1)%MAXQSIZE; //修改隊尾指針
return True;
}
BOOL De_SqQueue(SqQueue &Q,char &ch)
{//出隊列,成功返回True,并用ch返回該元素值,失敗返回False
if(Q.front==Q.rear) return False; //若隊列已空,返回True
ch=Q.elem[Q.front];
Q.front=(Q.front+1)%MAXQSIZE; //修改隊頭指針
return True; //成功出隊列,返回True
}
void Print_SqQueue(SqQueue Q)
{//顯示隊列中所有元素
int i;
if(Q.front==Q.rear) printf("隊列為空!\n");
else {i=Q.front;
while(i!=Q.rear)
{printf("%c ",Q.elem[i]);
i++;
if(i>=MAXQSIZE) i=i%MAXQSIZE;
}
printf("\n");
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -