## choice question

2-1 the condition that the circular queue with less one element space (m is the maximum queue length) is full ().

A.rear== front

B.(rear+1)%m==front

C.(rear+1) == front

D.front ==(front+1)%m

2-2 the cyclic queue is stored in array A[0... n-1], and its head and tail pointers are f and R respectively. The head pointer f always points to the head element of the queue, and the tail pointer r always points to the next position of the tail element. Assuming that the queue is not empty, the operation of the head and tail pointer when the element leaves the queue is ().

A.f=(f+1)%n

B.f=f+1

C.r=(r+1)%n

D.f=(f+1)%(n-1)

2-3 let the subscript range of the array in the cyclic queue be 0-n-1, and its head and tail pointers are front and rear respectively. The head pointer front always points to the team head element, and the tail pointer rear always points to the next position of the team tail element, then the number of elements is ()

A.rear-front

B.rear-front+1

C.(rear-front)%n+1

D.(rear-front+n) %n

2-4 let the subscript range of the array in the cyclic queue be 0-n, and its head and tail pointers are front and rear respectively. The head pointer front always points to the team head element, and the tail pointer rear always points to the next position of the team tail element, then the number of elements is ().

A.rear-front

B.rear-front+1

C.(rear-front+n+1)%(n+1)

D.(rear-front+n) %n

2-5 let the subscript range of the array in the cyclic queue be 0-n-1, and its head and tail pointers are f and R respectively. The head pointer f always points to the head element of the queue, and the tail pointer r always points to the next position of the tail element, then the condition of full queue is ().

A.(r+1)%n == f

B. r == f

C.r+1 == f

D.(r-l)%n == f

2-6 in a queue represented by a linked list, f and r point to the head and tail of the queue respectively. Which of the following operations can correctly insert the s node into the queue:

A.f->next=s; f=s;

B.r->next=s; r=s;

C.r+1 == f

D.(r-l)%n == f

2-7 queues stored in link mode are deleted ().

A. Modify header pointer only

B. Modify tail pointer only

C. The head and tail pointers should be modified

D. The head and tail pointers may have to be modified

2-8

For a circular queue with a maximum capacity of n, if the tail pointer is rear and the head of the queue is front, the condition for the empty queue is ().

(2 points)

A.

(rear+1)%n==front

B.

rear==front

C.

rear+1==front

D.

(rear-l)%n==front

2-9

The first in, first out feature of the queue refers to ().

I. the last element inserted into the queue is always deleted last

II. When inserting and deleting operations are performed at the same time, the insertion operation always takes precedence

III. whenever there is a delete operation, always insert it first

Ⅳ. The earliest inserted element is always deleted from the queue every time

(2 points)

A.

Ⅰ

B.

Ⅰ,Ⅳ

C.

Ⅱ,Ⅲ

D.

Ⅳ

2-10

It is known that the storage space of the circular queue is array A[21], front points to the previous position of the queue head element, and rear points to the tail element. Assuming that the current values of front and rear are 8 and 3 respectively, the length of the queue is ().

(2 points)

A.

5

B.

6

C.

16

D.

17

2-11

If the circular queue is implemented with array A[0... 5], and the current values of rear and front are 1 and 5 respectively, when an element is deleted from the queue and two elements are added, the values of rear and front are () respectively.

(2 points)

A.

3 and 4

B.

3 and 0

C.

5 and 0

D.

5 and 1

2-12

The circular queue is placed in the one-dimensional array A[0... M-1], end1 points to the queue head element, and end2 points to the last position of the queue tail element. Assuming that both ends of the queue can be queued in and out, the queue can hold up to M-1 elements. Initially empty. Of the following conditions for judging empty and full teams, the correct one is ().

(2 points)

A.

Team air: end1end2; Team full: end1(end2+1)mod M

B.

Team air: end1end2; Team full: end2(end1+1)mod (M-1)

C.

Team air: end2==(end1+1) mod M; Team full: end1==(end2+1) mod M

D.

Team air: end1==(end2+1) mod M; Team full: end2==(end1+1)mod (M-1)

answer:

DCDCA

BDBBC

BA

## Procedural blank filling

5-1

The structure of the known circular queue is defined as follows:

typedef struct

{

int size, front, rear;

int *element;

} AQUEUE;

Note: element is the dynamic array storing queue data elements, size is the size of the dynamic array, front is the subscript of the first element of the queue, and rear is the subscript of the next position of the last element of the queue.

The following definitions are assumed:

AQUEUE *queue;

The conditions for judging that the queue referred to by the queue is empty are:

queue->front==queue->rear

；

The conditions for judging that the queue referred to by the queue is full are:

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

；

Queue refers to the length of the queue:

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

5-3

Loop queue in and out operations.

#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

Basic operation of chain team.

#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;

}

Input format:

Input an integer n in the first line, input n characters in the second line, queue n characters in turn, and then perform n out of queue operations (no output). Input an integer m in the third line, input M characters in the fourth line, queue M characters in turn, and then perform m out of queue operations and output.

4

ABCD

5

12345

Output format:

1 2 3 4 5

## Function problem

## 6-1 partner problem

Assuming that the records of men and women are stored in an array, an algorithm is designed to realize partner pairing. It is required to output the paired partner and the name of the team head element without pairing.

Function interface definition:

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

Where dancer [] is the array for storing men's and women's information, and num is the size of the array.

Example of referee test procedure:

#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) { /* Please fill in the answer here */ } 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; }

Input example:

A set of inputs is given here. For example:

Li minhao M Li zhongshuo M Gao Xinya F Wu Yanzu M Sephirex Wang M Zhang Tianyuan F Zhang Zhilin M Xu Dandan F Ma Xiaoyun F

Output example:

Gao Xinya, Li minhao Zhang Tianyuan, Li zhongshuo Xu Dandan, Wu Yanzu Ma Xiaoyun, Wang Sicong Zhang Zhilin

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); } }

## Programming problem

7-1 implementation and basic operation of queue

Given an initially empty queue and a series of queue in and queue out operations, please write a program to output the elements of each queue out. The element values of the queue are all integers.

Input format:

Enter a positive integer n in the first row, indicating the number of operations; The next N lines, each representing an operation, are in the format of 1 d or 0. 1 d means to queue the integer d, and 0 means to queue. N not more than 20000.

Output format:

Output the elements out of the queue each time in order, one line for each element. If an out of queue operation is illegal (e.g. out of the queue when the queue is empty), invalid is output for the operation.

## Input example:

7 1 1 1 2 0 0 0 1 3 0

## Output example:

1 2 invalid 3

## answer

#include <stdio.h> int queue[20000]; int front; int rear; void initqueue();//Initialize queue void enterqueue(int x);//Join the team int deletequeue();//Out of the team 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)//Join the team { 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()//Initialize queue { front=0; rear=0; } void enterqueue(int x)//Join the team { queue[rear]=x; rear++; } int deletequeue()//Out of the team { if(front==rear) return 0;//Queue is empty else { front++; return 1; } }