PTA-队列练习题

选择题

2-1在少用一个元素空间的循环队列(m为最大队列长度)是满队列的条件( )。
A.rear== front
B.(rear+1)%m==front
C.(rear+1) == front
D.front ==(front+1)%m

2-2循环队列存储在数组A[0…n-1]中,其头尾指针分别为f和r,头指针f总是指向队头元素,尾指针r总是指向队尾元素的下一个位置,假设队列不空,元素出队时头尾指针的操作为( )。

A.f=(f+1)%n
B.f=f+1
C.r=(r+1)%n
D.f=(f+1)%(n-1)

2-3设循环队列中数组的下标范围是0—n-1,其头尾指针分别为front和rear,头指针front总是指向队头元素,尾指针rear总是指向队尾元素的下一个位置,则其元素的个数为( )

A.rear-front
B.rear-front+1
C.(rear-front)%n+1
D.(rear-front+n) %n

2-4设循环队列中数组的下标范围是0—n,其头尾指针分别为front和rear,头指针front总是指向队头元素,尾指针rear总是指向队尾元素的下一个位置,则其元素的个数为( )。

A.rear-front
B.rear-front+1
C.(rear-front+n+1)%(n+1)
D.(rear-front+n) %n

2-5设循环队列中数组的下标范围是0—n-1,其头尾指针分别为f和r,头指针f总是指向队头元素,尾指针r总是指向队尾元素的下一个位置,则队满的条件为( )。

A.(r+1)%n == f
B. r == f
C.r+1 == f
D.(r-l)%n == f

2-6在一个链表表示的队列中, f和r分别指向队列的头和尾。下列哪个操作能正确地将s结点插入到队列中:
A.f->next=s; f=s;
B.r->next=s; r=s;
C.r+1 == f
D.(r-l)%n == f
2-7用链接方式存储的队列,在进行删除运算时( )。

A.仅修改头指针
B.仅修改尾指针
C.头、尾指针都要修改
D.头、尾指针可能都要修改
2-8
最大容量为n的循环队列,队尾指针是rear,队头是front,则队空的条件是( )。

(2分)

A.
(rear+1)%n==front

B.
rear==front

C.
rear+1==front

D.
(rear-l)%n==front

2-9
队列的“先进先出”特性是指( )。

Ⅰ.最后插入队列中的元素总是最后被删除
Ⅱ.当同时进行插入、删除操作时,总是插入操作优先
Ⅲ.每当有删除操作时,总要先做一次插入操作
Ⅳ.每次从队列中删除的总是最早插入的元素

(2分)

A.

B.
Ⅰ、Ⅳ

C.
Ⅱ、Ⅲ

D.

2-10
已知循环队列的存储空间为数组A[21],front指向队头元素的前一个位置,rear指向队尾元素,假设当前front和rear的值分别为8和3,则该队列的长度为( )。

(2分)

A.
5

B.
6

C.
16

D.
17
2-11
若用数组A[0…5]来实现循环队列,且当前rear和front的值分别为1和5,当从队列中删除一个元素,再加入两个元素后,rear和front的值分别为( )。

(2分)

A.
3和4

B.
3和0

C.
5和0

D.
5和1

2-12
循环队列放在一维数组A[0…M-1]中,end1指向队头元素,end2指向队尾元素的后一个位置。假设队列两端均可进行入队和出队操作,队列中最多能容纳M-1个元素。初始时为空。下列判断队空和队满的条件中,正确的是( )。

(2分)

A.
队空:end1 == end2;队满: end1==(end2+1)mod M

B.
队空:end1 == end2;队满:end2==(end1+1)mod (M-1)

C.
队空:end2==(end1+1) mod M;队满:end1==(end2+1) mod M

D.
队空:end1==(end2+1) mod M;队满:end2==(end1+1)mod (M-1)

答案:
DCDCA
BDBBC
BA

程序填空题

5-1
已知循环队列的结构定义如下:

typedef struct
{
int size, front, rear;
int *element;
} AQUEUE;
说明:element 为存储队列数据元素的动态数组,size 为动态数组的尺寸,front 为队首元素的下标,rear 为队尾元素下一位置的下标。

假设有以下定义:

AQUEUE *queue;
判断 queue 所指队列为空的条件是:

queue->front==queue->rear

判断 queue 所指队列为满的条件是:

(queue->rear + 1) % queue->size == queue->front

queue 所指队列的长度是:

(queue->rear - queue->front + queue->size) % queue->size

5-3
循环队列入队出队操作。

#include
using namespace std;

#define MAXQSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef char QElemType;
typedef char SElemType;
typedef int Status;

typedef struct {
QElemType *base;
int front;
int rear;
} SqQueue;

Status InitQueue(SqQueue &Q) {

Q.base=new QElemType

;
if (!Q.base)
exit(OVERFLOW);

Q.front=Q.rear=0

;
return OK;
}

Status EnQueue(SqQueue &Q, QElemType e) {
if (

(Q.rear+1) % MAXQSIZE == Q.front

)
return ERROR;
Q.base[Q.rear] = e;

Q.rear=(Q.rear+1) % MAXQSIZE

;
return OK;
}

Status DeQueue(SqQueue &Q, QElemType &e) {
if (

Q.front == Q.rear

)
return ERROR;
e = Q.base[Q.front];

Q.front = (Q.front+1) % MAXQSIZE

;
return OK;
}

int main() {
SqQueue Q;
int n, i;
char c;
InitQueue(Q);
cin >> n;
for(i=0;i<n;i++){
cin >> c;
EnQueue(Q,c);
}

for(i=0;i<n;i++){
    DeQueue(Q,c);
    cout << c << " ";
}
return 0;

}

5-4
链队基本操作。

#include
using namespace std;

#define OK 1
#define ERROR 0
typedef int Status;
typedef char QElemType;
typedef struct QNode {
QElemType data;
struct QNode *next;
} QNode, *QueuePtr;

typedef struct {
QueuePtr front;
QueuePtr rear;
} LinkQueue;

Status InitQueue(LinkQueue &Q) {
Q.front = Q.rear = new QNode;
Q.front->next = NULL;
return OK;
}

Status EnQueue(LinkQueue &Q, QElemType e) {
QueuePtr p;
p = new QNode;
p->data = e;
p->next = NULL;

Q.rear->next=p

;

Q.rear=p

;
return OK;
}

Status DeQueue(LinkQueue &Q, QElemType &e) {
QueuePtr p;
if (Q.front == Q.rear)
return ERROR;
p = Q.front->next;
e = p->data;

Q.front->next=p->next

;
if (Q.rear == p)

Q.rear=Q.front

;
delete p;
return OK;
}

int main() {
LinkQueue Q;
int n,m,i;
char c;
InitQueue(Q);
cin >> n;
for(i=0;i<n;i++){
cin >> c;
EnQueue(Q,c);
}
for(i=0;i<n;i++){
DeQueue(Q,c);
}
cin >> m;
for(i=0;i<m;i++){
cin >> c;
EnQueue(Q,c);
}
for(i=0;i<m;i++){
DeQueue(Q,c);
cout << c << " ";
}
return 0;
}

输入格式:
输入第一行为1个整数n,第二行输入n个字符,将n个字符依次入队,再执行n次出队操作(不输出)。 输入第三行为1个整数m,第四行输入m个字符,将m个字符依次入队,再执行m次出队操作并输出。

4
ABCD
5
12345
输出格式:
1 2 3 4 5

函数题

6-1 舞伴问题

假设男士和女士的记录存放在一个数组中,设计算法实现舞伴配对,要求输出配对的舞伴,并输出没有配对的队头元素的姓名。
函数接口定义:

void DancePartner(DataType dancer[], int num) ;

其中 dancer[]是存放男士和女士信息的数组,num是数组大小。
裁判测试程序样例:

#include<stdio.h>
#include<stdlib.h>

typedef struct {
    char name[20]; 
    char sex; 
} DataType;

struct Node {
    DataType      data;
    struct Node*  next;
};
typedef struct Node  *PNode;
struct Queue
{
    PNode        f;
    PNode        r;
};
typedef struct Queue *LinkQueue;
LinkQueue  SetNullQueue_Link()
{
    LinkQueue lqueue;
    lqueue = (LinkQueue)malloc(sizeof(struct Queue));
    if (lqueue != NULL)
    {
        lqueue->f = NULL;
        lqueue->r = NULL;
    }
    else
        printf("Alloc failure! \n");
    return  lqueue;
}

int IsNullQueue_link(LinkQueue lqueue)
{
    return (lqueue->f == NULL);
}

void EnQueue_link(LinkQueue lqueue, DataType x)
{
    PNode  p;
    p = (PNode)malloc(sizeof(struct Node));
    if (p == NULL)
        printf("Alloc failure!");
    else {
        p->data = x;
        p->next = NULL;
        if (lqueue->f == NULL)
        {
            lqueue->f = p;
            lqueue->r = p;
        }
        else
        {
            lqueue->r->next = p;
            lqueue->r = p;
        }
    }
}
void DeQueue_link(LinkQueue lqueue)
{
    struct Node  * p;
    if (lqueue->f == NULL)
        printf("It is empty queue!\n ");
    else
    {
        p = lqueue->f;
        lqueue->f = lqueue->f->next;
        free(p);
    }
}
DataType  FrontQueue_link(LinkQueue lqueue)
{
    if (lqueue->f == NULL)
    {
        printf("It is empty queue!\n");
    }
    else
        return (lqueue->f->data);
}

void DancePartner(DataType dancer[], int num) 
{
            /* 请在这里填写答案 */
}

int main()
{
    DataType dancer[9];
    for (int i = 0; i < 9; i++)
    scanf("%s %c", dancer[i].name, &dancer[i].sex);
    DancePartner(dancer, 9);
    return 0;
}

输入样例:
在这里给出一组输入。例如:

李敏浩 M
李钟硕 M
高欣雅 F
吴彦祖 M
王思聪 M
张甜源 F
张智霖 M
许丹丹 F
马小云 F

输出样例:

高欣雅 李敏浩
张甜源 李钟硕
许丹丹 吴彦祖
马小云 王思聪

张智霖
void DancePartner(DataType dancer[], int num)
{
    LinkQueue Queue_head = SetNullQueue_Link();

    for (int i = 0; i < num; i++)
    {

        if (!IsNullQueue_link(Queue_head) &&
            (FrontQueue_link(Queue_head).sex) != dancer[i].sex)
        {
            if (dancer[i].sex == 'F')
                printf("%s %s\n", dancer[i].name, Queue_head->f->data.name);
            else
            {
                printf("%s %s\n", Queue_head->f->data.name, dancer[i].name);
            }
            DeQueue_link(Queue_head);
        }
        else
        {
            EnQueue_link(Queue_head, dancer[i]);
        }
    }
    printf("\n");

    if (!IsNullQueue_link(Queue_head))
    {
        printf("%s", Queue_head->f->data.name);
    }
}


编程题

7-1 队列的实现及基本操作
给定一个初始为空的队列和一系列入队、出队操作,请编写程序输出每次出队的元素。队列的元素值均为整数。

输入格式:
输入第1行为1个正整数n,表示操作个数;接下来n行,每行表示一个操作,格式为1 d或0。1 d表示将整数d入队,0表示出队。n不超过20000。

输出格式:
按顺序输出每次出队的元素,每个元素一行。若某出队操作不合法(如在队列空时出队),则对该操作输出invalid。

输入样例:

7
1 1
1 2
0
0
0
1 3
0

输出样例:

1
2
invalid
3

#include <stdio.h>
int queue[20000];
int front;
int rear;
void initqueue();//初始化队列
void enterqueue(int x);//入队
int deletequeue();//出队 

int main()
{
	int n,t;
	int i,k=0;
	int a[20000][2];
	char ch;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{	
		k=0;
		do
		{
			scanf("%d",&a[i][k]);
			k++;
		}while((ch=getchar())!='\n');					
	}
	for(i=0;i<n;i++)
	{
		if(a[i][0]==1)//入队 
		{
			enterqueue(a[i][1]);
		}			
		if(a[i][0]==0)
		{	
			t=deletequeue();				
			if(t==0)
				printf("invalid\n");
			else
				printf("%d\n",queue[front-1]);			
		}
	}	
	return 0;
}

void initqueue()//初始化队列
{
	front=0;
	rear=0;
}

void enterqueue(int x)//入队
{	
	queue[rear]=x;
	rear++;	
}

int deletequeue()//出队 
{
	if(front==rear)
		return 0;//队列为空 
	else
	{
		front++;
		return 1;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值