第三章栈和队列-作业题
1.栈和列队的基本概念
2-1 若元素a,b,c,d,e,f依次进栈,允许进栈、退栈操作交替进行,但不允许连续三次进行退栈操作,则不可能得到的出栈序列是(D )。
(2分)
A.d,c,e,b,f,a
B.c,b,d,a,e,f
C.b,c,a,e,f,d
D.a,f,e,d,c,b
2-2 若某堆栈的输入序列为1,2,3,…,n-1,n,输出序列的第1个元素为n,则第i个输出元素为(A )
A.n-i+1
B.n-1
C.i
D.哪个元素都有可能
2-3 以数组Q[0…m-1]存放循环队列中的元素,变量rear和qulen分别指示循环队列中队尾元素的实际位置和当前队列中元素的个数,队列第一个元素的实际位置是(D )。
A.rear-qulen
B.rear-qulen+m
C.m-quelen
D.(1+(rear-quelen+m)) mod m
2-4设输入元素为1,2,3,A,B,输入次序为123AB,元素经过栈后到达输出序列,当所有元素均到达输出序列后,序列(C )是不可能的。
(2分)
A.BA321
B.A3B21
C.B32A1
D.AB321
2-5已知操作符包括+,-,,/,(,)。将中缀表达式 a+b-a((c+d)/e-f)+g 转换为等价的后缀表达式 ab+acd+e/f-*-g+ 时,用栈来存放暂时还不确定运算次序的操作符。若栈初始时为空,则转换过程中同时保存在栈中的操作符的最大个数是(A )。
A.5
B.7
C.8
D.11
2-6元素A,B,C,D依次入栈,出栈无限制,则以下( B)是可能的出栈序列。
A.C, A, B, D
B.B, A, D, C
C.B, D, A, C
D.A, D, B, C
2-7用S表示入栈操作,X表示出栈操作,若元素入栈的顺序为1234,为了得到1342出栈顺序,相应的S和X的操作串为( )。
A.SXSSSXXX
B.SXSXSXSX
C.SSSSXXXX
D.SXSSXSXX
2-8若用大小为6的数组来实现循环队列,且当前front和rear的值分别为0和4。当从队列中删除两个元素,再加入两个元素后,front和rear的值分别为多少?(A)
A.2和0
B.2和2
C.2和4
D.2和6
2-9如果循环队列用大小为m的数组表示,队头位置为front、队列元素个数为size,那么队尾元素位置rear为:D
A.front+size
B.front+size-1
C.(front+size)%m
D.(front+size-1)%m
2-10循环队列的队满条件为 (C )。
A.(sq.rear+1) % maxsize ==(sq.front+1) % maxsize
B.(sq.front+1) % maxsize ==sq.rear
C.(sq.rear+1) % maxsize ==sq.front
D.sq.rear ==sq.front
2-11栈和队列的共同点是( C)。
A.都是先进先出
B.都是先进后出
C.只允许在端点处插入和删除元素
D.没有共同点
6-1 使用栈完成回文判断 (12 分)
回文是指正读反读均相同的字符序列,如“abba”和“abdba”均是回文,但“good”不是回文。试写一个算法判定给定的字符串是否为回文。
函数接口定义:
int IsPalindrome(char s[]); //判断字符串s是否回文。
其中 s 是用户传入的参数,其值为待判断字符串。如果s 是回文,则返回值为1,否则返回值为0。
特别说明:本题要求使用栈的基本操作完成回文的判断。 栈的定义如下:
#define Stack_Size 50
typedef char ElemType;
typedef struct
{ ElemType data[Stack_Size];
int top;
}SeqStack;
//栈的基本操作函数定义
SeqStack* InitStack(); //栈初始化
int IsEmpty(SeqStack *S); //栈判空
int IsFull(SeqStack *S); //栈判满
int Push(SeqStack * S, ElemType x); // 入栈
int Pop(SeqStack * S, ElemType *x); // 出栈
int GetTop(SeqStack *S, ElemType *x); // 取栈顶元素
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define Stack_Size 50
typedef char ElemType;
typedef struct
{ ElemType data[Stack_Size];
int top;
}SeqStack;
//栈的基本操作函数定义
SeqStack* InitStack(); //栈初始化
int IsEmpty(SeqStack *S); //栈判空
int IsFull(SeqStack *S); //栈判满
int Push(SeqStack * S, ElemType x); // 入栈
int Pop(SeqStack * S, ElemType *x); // 出栈
int GetTop(SeqStack *S, ElemType *x); // 取栈顶元素
int IsPalindrome(char s[]); //判断字符串s是否回文。
main()
{
char s[20];
scanf("%s",s);
if(IsPalindrome(s))
printf("It's Palidrome!\n");
else
printf("It's not Palidrome!\n");
}
SeqStack* InitStack()
{
SeqStack *s;
s=(SeqStack *)malloc(sizeof(SeqStack));
s->top=-1;
return s;
}
int IsEmpty(SeqStack *S)
{
return(S->top==-1?TRUE:FALSE);
}
int IsFull(SeqStack *S)
{
return(S->top== Stack_Size-1?TRUE:FALSE);
}
int Push(SeqStack * S, ElemType x)
{
if(S->top== Stack_Size-1)
return(FALSE);
S->top++;
S->data[S->top]=x;
return(TRUE);
}
int Pop(SeqStack * S, ElemType *x)
{ if(S->top==-1)
return(FALSE);
*x= S->data[S->top];
S->top--;
return(TRUE);
}
int GetTop(SeqStack *S, ElemType *x)
{
if(S->top==-1)
return(FALSE);
*x = S->data[S->top];
return(TRUE);
}
/* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
abba
结尾无空行
输出样例:
在这里给出相应的输出。例如:
It’s Palidrome!
int IsPalindrome(char s[]) {
SeqStack* stack = InitStack();
stack->top = 0;
int Len = strlen(s);
for ( ; stack->top < Len / 2 && !IsFull(stack); ++stack->top) {
stack->data[stack->top] = s[stack->top];
}
stack->top--;
for (int p = (Len - 1) / 2 + 1; p < Len; ++p) {
if (stack->data[stack->top] == s[p]) {
stack->top--;
} else {
break;
}
}
if (stack->top == -1) {
return 1;
} else {
return 0;
}
}
6-2 判断表达式中括号是否匹配 (12 分)
设算术表达式中有圆括号、方括号、花括号,设计一个算法,判断表达式中的各种括号是否配对。
说明:
(1)本题仅判断括号是否配对,对于表达式其它问题不做检测。
(2)括号只考虑配对问题,不考虑括号间的嵌套准则。例如:(a+(b-[c+d])*{e/f}) 中的括号是匹配的;(a+{b-c)} 中的括号不匹配。
函数接口定义:
int IsBracketMatch(char *str);//判断str中括号是否匹配。
其中 str是用户传入的参数,其值为带判断表达式。 若括号匹配则返回1,否则返回0。
栈的定义如下:
#define Stack_Size 50
typedef char ElemType;
typedef struct
{ ElemType data[Stack_Size];
int top;
}SeqStack;
//栈的基本操作函数定义
SeqStack* InitStack(); //栈初始化
int IsEmpty(SeqStack *S); //栈判空
int IsFull(SeqStack *S); //栈判满
int Push(SeqStack * S, ElemType x); // 入栈
int Pop(SeqStack * S, ElemType *x); // 出栈
int GetTop(SeqStack *S, ElemType *x); // 取栈顶元素
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define Stack_Size 50
typedef char ElemType;
typedef struct
{ ElemType data[Stack_Size];
int top;
}SeqStack;
//栈的基本操作函数定义
SeqStack* InitStack(); //栈初始化
int IsEmpty(SeqStack *S); //栈判空
int IsFull(SeqStack *S); //栈判满
int Push(SeqStack * S, ElemType x); // 入栈
int Pop(SeqStack * S, ElemType *x); // 出栈
int GetTop(SeqStack *S, ElemType *x); // 取栈顶元素
int IsBracketMatch(char *str);//判断str中括号是否匹配。
main()
{
char s[20];
scanf("%s",s);
if( IsBracketMatch(s))
printf("Match!\n");
else
printf("Not Match!\n");
}
SeqStack* InitStack()
{
SeqStack *s;
s=(SeqStack *)malloc(sizeof(SeqStack));
s->top=-1;
return s;
}
int IsEmpty(SeqStack *S)
{
return(S->top==-1?TRUE:FALSE);
}
int IsFull(SeqStack *S)
{
return(S->top== Stack_Size-1?TRUE:FALSE);
}
int Push(SeqStack * S, ElemType x)
{
if(S->top== Stack_Size-1)
return(FALSE);
S->top++;
S->data[S->top]=x;
return(TRUE);
}
int Pop(SeqStack * S, ElemType *x)
{ if(S->top==-1)
return(FALSE);
*x= S->data[S->top];
S->top--;
return(TRUE);
}
int GetTop(SeqStack *S, ElemType *x)
{
if(S->top==-1)
return(FALSE);
*x = S->data[S->top];
return(TRUE);
}
/* 请在这里填写答案 */
输入样例1:
在这里给出一组输入。例如:
(a+(b-[c*d]-{e/f}))
结尾无空行
输出样例1:
在这里给出相应的输出。例如:
Match!
输入样例2:
在这里给出一组输入。例如:
(a+[b-(c*d]-{e/f}))
结尾无空行
输出样例2:
在这里给出相应的输出。例如:
Not Match!
int IsBracketMatch(char *str) {
SeqStack *stack = InitStack();
ElemType x = 'c';
for (int i = 0; i < strlen(str); ++i) {
if ((str[i] == '(' || str[i] == '[' || str[i] == '{') && !IsFull(stack)) {
Push(stack, str[i]);
} else if (str[i] == ')' || str[i] == ']' || str[i] == '}') {
switch (str[i]) {
case ')':
if (GetTop(stack, &x) && x == '(') {
Pop(stack, &x);
} else {
return 0;
}
break;
case ']':
if (GetTop(stack, &x) && x == '[') {
Pop(stack, &x);
} else {
return 0;
}
break;
case '}':
if (GetTop(stack, &x) && x == '{') {
Pop(stack, &x);
} else {
return 0;
}
break;
default :
break;
}
}
}
if (IsEmpty(stack)) {
return 1;
}
return 0;
}
6-3 队列的操作集合(带尾指针的循环链表) (18 分)
假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点,试编写相应的初始化队列、判空队、入队和出队算法。(队中元素均为整数)
程序功能为:初始化一个空队列,然后接收命令并完成相应操作,命令如下:
ENQUEUE x 将整数x入队。若操作成功则无输出,若操作失败则输出FULL!。
DELQUEUE 出队一个元素。若操作成功则输出该元素,若失败则输EMPTY QUEUE!。
ISEMPTY 判断队列是否为空。若为空则输出 EMPTY,若非空则输出NOT EMPTY。
END 依次输出队列中所有元素,释放结点空间,并结束程序。
函数接口定义:
CirLinkQueue InitQueue(); //初始化队列,返回值为队列的尾指针。
int IsEmptyQueue(CirLinkQueue Q); //队列判空,若为空,则返回1;非空,返回0。
int EnQueue(CirLinkQueue *Q, DataType x); // 元素x入队,若操作成功,则返回1;操作失败,则返回0。
int DelQueue(CirLinkQueue *Q, DataType *x); // 出队一个元素,若操作成功,则返回1;操作失败,则返回0。
说明:队列使用仅带尾指针的循环链表表示,数据类型定义如下:
typedef int DataType;
typedef struct node
{ DataType data;
struct node *next;
}LNode,*CirLinkQueue;
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int DataType;
typedef struct node
{ DataType data;
struct node *next;
}LNode,*CirLinkQueue;
//队列的基本操作函数定义
CirLinkQueue InitQueue(); //初始化队列,返回值为队列的尾指针。
int IsEmptyQueue(CirLinkQueue Q); //队列判空,若为空,则返回1;非空,返回0。
int EnQueue(CirLinkQueue *Q, DataType x); // 元素x入队,若操作成功,则返回1;操作失败,则返回0。
int DelQueue(CirLinkQueue *Q, DataType *x); // 出队一个元素,若操作成功,则返回1;操作失败,则返回0。
void DestroyQueue(CirLinkQueue Q);
int main(void)
{
char cmd[20];
CirLinkQueue pQueue = InitQueue();
DataType x;
scanf("%s", cmd);
while (strcmp(cmd, "END") != 0)
{
if (strcmp(cmd, "ENQUEUE") == 0)
{
scanf("%d", &x);
if (EnQueue(&pQueue, x) == 0)
printf("FULL QUEUE!\n");
}
else if (strcmp(cmd, "DELQUEUE") == 0)
{
if (DelQueue(&pQueue,&x) == 0)
printf("EMPTY QUEUE!\n");
else
printf("%d\n",x);
}
else if (strcmp(cmd, "ISEMPTY") == 0)
{
if (IsEmptyQueue(pQueue) == 0)
printf("NOT EMPTY\n");
else
printf("EMPTY\n");
}
scanf("%s", cmd);
}
DestroyQueue(pQueue);
return 0;
}
void DestroyQueue(CirLinkQueue Q)
{
LNode *p,*t;
p=Q;Q=Q->next;
p->next=NULL;
p=Q->next;
while(p)
{
printf("%d ",p->data);
t=p->next;
free(p);
p=t;
}
free(Q);
}
/* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
ISEMPTY
ENQUEUE 1
ENQUEUE 2
DELQUEUE
DELQUEUE
DELQUEUE
ENQUEUE 3
ISEMPTY
ENQUEUE 4
ENQUEUE 5
DELQUEUE
END
结尾无空行
输出样例:
在这里给出相应的输出。例如:
EMPTY
1
2
EMPTY QUEUE!
NOT EMPTY
3
4 5
结尾无空行
CirLinkQueue InitQueue(){
CirLinkQueue Q;
Q=(CirLinkQueue)malloc(sizeof(LNode));
Q->next=Q;
return Q;
}
int IsEmptyQueue(CirLinkQueue Q){
if(Q->next==Q)return 1;
else return 0;
}
int EnQueue(CirLinkQueue *Q, DataType x){
LNode *p;
p=(LNode *)malloc(sizeof(LNode));
if(p==NULL)return 0;
p->data=x;
p->next=(*Q)->next;
(*Q)->next=p;
(*Q)=p;
return 1;
}
int DelQueue(CirLinkQueue *Q, DataType *x){
if(IsEmptyQueue(*Q))return 0;
CirLinkQueue r=*Q,f;
f=r->next->next;
if(f==r){
*Q=r->next;
*x=f->data;
r->next->next=f->next;
}else{
*x=f->data;
r->next->next=f->next;
}
return 1;
}
6-4 循环队列的操作集合(只设 rear和quelen) (18 分)
假设循环队列中只设rear和quelen来分别指示队尾元素的位置和队中元素的个数,试编写相应的判满、判空、入队和出队算法。(队中元素均为整数,为了便于测试,数组做大容量设定为4。)
程序功能为:初始化一个空队列,然后接收命令并完成相应操作,命令如下: ENQUEUE x 将整数x入队。若操作成功则无输出,若操作失败则输出FULL QUEUE!。
DELQUEUE 出队一个元素。若操作成功则输出该元素,若失败则输EMPTY QUEUE!。
ISEMPTY 判断队列是否为空。若为空则输出 EMPTY,若非空则输出NOT EMPTY。
ISFULL 判断队列是否已满。若满则输出 FULL,否则输出NOT FULL。
END 依次输出队列中所有元素,释放结点空间,并结束程序。
函数接口定义:
int IsEmptyQueue(SeQueue *Q); //队列判空,若为空,则返回1;非空,返回0。
int IsFullQueue(SeQueue *Q); //队列判满,若为满,则返回1;非满,返回0。
int EnQueue(SeQueue *Q, ElemType x); // 元素x入队,若操作成功,则返回1;操作失败,则返回0。
int DelQueue(SeQueue *Q, ElemType *x); // 出队一个元素,若操作成功,则返回1;操作失败,则返回0。
说明:循环队列类型定义如下:
#define MAXSIZE 4 //队列的最大容量
typedef int ElemType;
typedef struct
{ ElemType elem[MAXSIZE];
int rear; //队尾元素的位置
int quelen; //队中元素的个数
}SeQueue;
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 4 //队列的最大容量
typedef int ElemType;
typedef struct
{ ElemType elem[MAXSIZE];
int rear; //队尾元素的位置
int quelen; //队中元素的个数
}SeQueue;
//队列的基本操作函数定义
SeQueue * InitQueue(); //初始化队列,返回值为指向队列的指针。
void DestroyQueue(SeQueue *Q); //依次输出队列中元素值,并释放空间。
int IsEmptyQueue(SeQueue *Q); //队列判空,若为空,则返回1;非空,返回0。
int IsFullQueue(SeQueue *Q); //队列判满,若为满,则返回1;非满,返回0。
int EnQueue(SeQueue *Q, ElemType x); // 元素x入队,若操作成功,则返回1;操作失败,则返回0。
int DelQueue(SeQueue *Q, ElemType *x); // 出队一个元素,若操作成功,则返回1;操作失败,则返回0。
int main(void)
{
char cmd[20];
SeQueue *pQueue=InitQueue();
ElemType x;
scanf("%s", cmd);
while (strcmp(cmd, "END") != 0)
{
if (strcmp(cmd, "ENQUEUE") == 0)
{
scanf("%d", &x);
if (EnQueue(pQueue, x) == 0)
printf("FULL QUEUE!\n");
}
else if (strcmp(cmd, "DELQUEUE") == 0)
{
if (DelQueue(pQueue,&x) == 0)
printf("EMPTY QUEUE!\n");
else
printf("%d\n",x);
}
else if (strcmp(cmd, "ISEMPTY") == 0)
{
if (IsEmptyQueue(pQueue) == 0)
printf("NOT EMPTY\n");
else
printf("EMPTY\n");
}
else if (strcmp(cmd, "ISFULL") == 0)
{
if (IsFullQueue(pQueue) == 0)
printf("NOT FULL\n");
else
printf("FULL\n");
}
scanf("%s", cmd);
}
DestroyQueue(pQueue);
return 0;
}
SeQueue *InitQueue() //初始化队列,返回值为指向队列的指针。
{
SeQueue *q;
q=(SeQueue*)malloc(sizeof(SeQueue));
q->quelen=0;
q->rear=-1;
return q;
}
void DestroyQueue(SeQueue *Q)
{
ElemType x;
while(!IsEmptyQueue(Q))
{
DelQueue(Q,&x);
printf("%d ",x);
}
printf("\n");
free(Q);
}
/* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
ENQUEUE 1
ISEMPTY
ISFULL
DELQUEUE
ISEMPTY
ISFULL
DELQUEUE
ENQUEUE 2
ENQUEUE 3
DELQUEUE
ENQUEUE 4
ENQUEUE 5
ENQUEUE 6
ENQUEUE 7
ISFULL
ISEMPTY
DELQUEUE
ENQUEUE 8
END
结尾无空行
输出样例:
在这里给出相应的输出。例如:
NOT EMPTY
NOT FULL
1
EMPTY
NOT FULL
EMPTY QUEUE!
2
FULL QUEUE!
FULL
NOT EMPTY
3
4 5 6 8
结尾无空行
int IsEmptyQueue(SeQueue *Q){
if(Q->quelen == 0)
return 1;
else
return 0;
}
int IsFullQueue(SeQueue *Q){
if(Q->quelen == MAXSIZE)
return 1;
else
return 0;
}
int EnQueue(SeQueue *Q, ElemType x){
if(IsFullQueue(Q))
return 0;
Q->rear=(Q->rear+1)%MAXSIZE;
Q->elem[Q->rear]=x;
Q->quelen++;
return 1;
}
int DelQueue(SeQueue *Q, ElemType *x){
if (IsEmptyQueue(Q))
return 0;
Q->quelen--;
*x = Q->elem[(Q->rear - Q->quelen+MAXSIZE)%MAXSIZE];
return 1;
}
6-5 循环队列的操作集合(增设tag) (18 分)
在循环队列中,可以设置一个标志域tag,以区分当尾指针和头指针相等时,队列的状态是“空”还是“满”。编写此结构相应的队列初始化、入队、出队算法以及判空、判满算法。(队中元素均为整数,为了便于测试,数组做大容量设定为4。)
程序功能为:初始化一个空队列,然后接收命令并完成相应操作,命令如下: ENQUEUE x 将整数x入队。若操作成功则无输出,若操作失败则输出FULL QUEUE!。
DELQUEUE 出队一个元素。若操作成功则输出该元素,若失败则输EMPTY QUEUE!。
ISEMPTY 判断队列是否为空。若为空则输出 EMPTY,若非空则输出NOT EMPTY。
ISFULL 判断队列是否已满。若满则输出 FULL,否则输出NOT FULL。
END 依次输出队列中所有元素,释放结点空间,并结束程序。
函数接口定义:
SeqQueue * InitQueue(); //初始化队列,返回值为指向队列的指针。
int IsEmptyQueue(SeqQueue *Q); //队列判空,若为空,则返回1;非空,返回0。
int IsFullQueue(SeqQueue *Q); //队列判满,若为满,则返回1;非满,返回0。
int EnQueue(SeqQueue *Q, ElemType x); // 元素x入队,若操作成功,则返回1;操作失败,则返回0。
int DelQueue(SeqQueue *Q, ElemType *x); // 出队一个元素,若操作成功,则返回1;操作失败,则返回0。
说明:循环队列类型定义如下:
#define MAXSIZE 4 //队列的最大容量
typedef int ElemType;
typedef struct
{
ElemType elem[MAXSIZE];
int rear; //队尾元素的位置
int front; //对头元素的前一个位置
int tag;//标志最近一次队列操作是入队还是出队。入队设为1,出队设为0。
}SeqQueue;
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 4 //队列的最大容量
typedef int ElemType;
typedef struct
{
ElemType elem[MAXSIZE];
int rear; //队尾元素的位置
int front; //对头元素的前一个位置
int tag;//标志最近一次队列操作是入队还是出队。入队设为1,出队设为0。
}SeqQueue;
//队列的基本操作函数定义
SeqQueue * InitQueue(); //初始化队列,返回值为指向队列的指针。
void DestroyQueue(SeqQueue *Q); //依次输出队列中元素值,并释放空间。
int IsEmptyQueue(SeqQueue *Q); //队列判空,若为空,则返回1;非空,返回0。
int IsFullQueue(SeqQueue *Q); //队列判满,若为满,则返回1;非满,返回0。
int EnQueue(SeqQueue *Q, ElemType x); // 元素x入队,若操作成功,则返回1;操作失败,则返回0。
int DelQueue(SeqQueue *Q, ElemType *x); // 出队一个元素,若操作成功,则返回1;操作失败,则返回0。
int main(void)
{
char cmd[20];
SeqQueue *pQueue=InitQueue();
ElemType x;
scanf("%s", cmd);
while (strcmp(cmd, "END") != 0)
{
if (strcmp(cmd, "ENQUEUE") == 0)
{
scanf("%d", &x);
if (EnQueue(pQueue, x) == 0)
printf("FULL QUEUE!\n");
}
else if (strcmp(cmd, "DELQUEUE") == 0)
{
if (DelQueue(pQueue,&x) == 0)
printf("EMPTY QUEUE!\n");
else
printf("%d\n",x);
}
else if (strcmp(cmd, "ISEMPTY") == 0)
{
if (IsEmptyQueue(pQueue) == 0)
printf("NOT EMPTY\n");
else
printf("EMPTY\n");
}
else if (strcmp(cmd, "ISFULL") == 0)
{
if (IsFullQueue(pQueue) == 0)
printf("NOT FULL\n");
else
printf("FULL\n");
}
scanf("%s", cmd);
}
DestroyQueue(pQueue);
return 0;
}
void DestroyQueue(SeqQueue *Q)
{
ElemType x;
while(!IsEmptyQueue(Q))
{
DelQueue(Q,&x);
printf("%d ",x);
}
printf("\n");
free(Q);
}
/* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
ISEMPTY
ENQUEUE 1
ENQUEUE 2
DELQUEUE
DELQUEUE
DELQUEUE
ENQUEUE 3
ISEMPTY
ENQUEUE 4
ENQUEUE 5
DELQUEUE
END
结尾无空行
输出样例:
在这里给出相应的输出。例如:
EMPTY
1
2
EMPTY QUEUE!
NOT EMPTY
3
4 5
结尾无空行
SeqQueue * InitQueue(){
SeqQueue *Q;
Q=(SeqQueue *)malloc(sizeof(SeqQueue));
Q->front=Q->rear=-1;
return Q;
}
int IsEmptyQueue(SeqQueue *Q){
if(Q->tag==0&&Q->rear==Q->front)return 1;
return 0;
}
int IsFullQueue(SeqQueue *Q){
if(Q->tag==1&&Q->rear==Q->front)return 1;
return 0;
}
int EnQueue(SeqQueue *Q, ElemType x){
if(IsFullQueue(Q))return 0;
Q->rear=(Q->rear+1)%MAXSIZE;
Q->elem[Q->rear]=x;
Q->tag=1;
return 1;
}
int DelQueue(SeqQueue *Q, ElemType *x){
if(IsEmptyQueue(Q))return 0;
Q->front=(Q->front+1)%MAXSIZE;
*x=Q->elem[Q->front];
Q->tag=0;
return 1;
}