Q1:
如果用一个循环数组
qu[0
,
m0-1]
表示队列时,该队列只有一个头指针
front
,不设队尾指针
rear
,而改置计数器
count
用以记录队列中结点的个数。
(
1
)编写实现队列的五个基本运算;
(2)队列中能容纳元素的最多个数还是 m0-1 吗?
队列为空:count==0,front==0;
队列为满:count==m0;
My View;有三个关键的地方:1,模运算的作用:数字在一定范围循环,实现首尾相连;
2,数组的序号变化:控制在0-(m0-1)范围内;
3,count:控制在1-m0范围内.
CODE:
#include
<
stdio.h
>
#include < stdlib.h >
#define m0 5 // 最大队列长度
typedef int QElemType; // 队列元素类型
typedef struct ... {
QElemType *base;//初始化的动态分配存储空间
int front;
int count;
} SqQueue;
// 构造空队列
SqQueue * makeQueue() ... {
SqQueue *Q = (SqQueue *) malloc(sizeof(SqQueue));
if(!Q)printf("OVERFLOW! ");
Q -> base = (QElemType *) malloc (m0 * sizeof (QElemType));/**//*比较关键的一步*/
if(!Q -> base)printf("OVERFLOW! ");
Q -> front = 0;
Q -> count = 0;
return Q;
}
// 判断队列是否为空
int isEmpty(SqQueue * Q) ... {
if(Q -> count == 0)
return 1;
else
return 0;
}
// 判断队列是否已满
int isFull(SqQueue * Q) ... {
if(Q -> count == m0)
return 1;
else
return 0;
}
// 取队头元素
QElemType pop(SqQueue * Q) ... {
QElemType h = 0;
if(isEmpty(Q))
printf("Sorry,the queue is empty! ");//队列下溢出
h = Q -> base[Q -> front];
return h;
}
// 将元素e入列
void enQueue(SqQueue * Q,QElemType e) ... {
if(isFull(Q))...{
printf("Queue overFlow! ");//队列上溢出
}
int place;
place = (Q -> front + Q -> count) % m0;
Q -> base[place] = e;
Q -> count = (Q -> count + 1) % (m0 + 1);
if(Q -> count == 0)
Q -> count = 1;//确保count在1-5循环
}
// 删除头元素
void deQueue(SqQueue * Q) ... {
if(isEmpty(Q))
printf("Queue underFlow! ");//队列下溢出
else...{
Q -> front =(Q -> front + 1) % m0;
Q -> count --;
}
}
// 打印当前队列
void print(SqQueue * Q) ... {
if(isEmpty(Q))
printf("Sorry,the queue is empty! ");//队列下溢出
else...{
int p = Q -> count;
int i = 0;
printf("Now,there are %d elements of queue :",p);
while(p!=0)...{
int place = (Q -> front + i) % m0;
printf("%d,",Q -> base[place]);
i++;
p--;
}
printf(" ");
}
}
#include < stdlib.h >
#define m0 5 // 最大队列长度
typedef int QElemType; // 队列元素类型
typedef struct ... {
QElemType *base;//初始化的动态分配存储空间
int front;
int count;
} SqQueue;
// 构造空队列
SqQueue * makeQueue() ... {
SqQueue *Q = (SqQueue *) malloc(sizeof(SqQueue));
if(!Q)printf("OVERFLOW! ");
Q -> base = (QElemType *) malloc (m0 * sizeof (QElemType));/**//*比较关键的一步*/
if(!Q -> base)printf("OVERFLOW! ");
Q -> front = 0;
Q -> count = 0;
return Q;
}
// 判断队列是否为空
int isEmpty(SqQueue * Q) ... {
if(Q -> count == 0)
return 1;
else
return 0;
}
// 判断队列是否已满
int isFull(SqQueue * Q) ... {
if(Q -> count == m0)
return 1;
else
return 0;
}
// 取队头元素
QElemType pop(SqQueue * Q) ... {
QElemType h = 0;
if(isEmpty(Q))
printf("Sorry,the queue is empty! ");//队列下溢出
h = Q -> base[Q -> front];
return h;
}
// 将元素e入列
void enQueue(SqQueue * Q,QElemType e) ... {
if(isFull(Q))...{
printf("Queue overFlow! ");//队列上溢出
}
int place;
place = (Q -> front + Q -> count) % m0;
Q -> base[place] = e;
Q -> count = (Q -> count + 1) % (m0 + 1);
if(Q -> count == 0)
Q -> count = 1;//确保count在1-5循环
}
// 删除头元素
void deQueue(SqQueue * Q) ... {
if(isEmpty(Q))
printf("Queue underFlow! ");//队列下溢出
else...{
Q -> front =(Q -> front + 1) % m0;
Q -> count --;
}
}
// 打印当前队列
void print(SqQueue * Q) ... {
if(isEmpty(Q))
printf("Sorry,the queue is empty! ");//队列下溢出
else...{
int p = Q -> count;
int i = 0;
printf("Now,there are %d elements of queue :",p);
while(p!=0)...{
int place = (Q -> front + i) % m0;
printf("%d,",Q -> base[place]);
i++;
p--;
}
printf(" ");
}
}
测试代码:
#include
<
stdio.h
>
#include " sqqueue.h "
typedef int ElemType;
void main() ... {
ElemType h;
SqQueue *queue = makeQueue();
print(queue);
deQueue(queue);
enQueue(queue,1);
enQueue(queue,2);
enQueue(queue,3);
enQueue(queue,4);
print(queue);
deQueue(queue);
deQueue(queue);
deQueue(queue);
enQueue(queue,5);
print(queue);
enQueue(queue,6);
print(queue);
deQueue(queue);
enQueue(queue,7);
print(queue);
}
#include " sqqueue.h "
typedef int ElemType;
void main() ... {
ElemType h;
SqQueue *queue = makeQueue();
print(queue);
deQueue(queue);
enQueue(queue,1);
enQueue(queue,2);
enQueue(queue,3);
enQueue(queue,4);
print(queue);
deQueue(queue);
deQueue(queue);
deQueue(queue);
enQueue(queue,5);
print(queue);
enQueue(queue,6);
print(queue);
deQueue(queue);
enQueue(queue,7);
print(queue);
}
RUN:
Q2:假定用一个循环单链表表示队列(称为循环队列),该队列只设一个队尾指针 rear,不设队首指针,编写如下函数:
(1)向循环链队中插入一个元素为 x 的结点;
(2)从循环链队中删除一个结点。
My View:在构造队列时,有一个关键的地方:以节点为中心.
CODE:
#include
<
stdio.h
>
#include < stdlib.h >
typedef struct QNode ... {
int data;
struct QNode *next;
} QNode, * QueuePtr; // 节点定义符
typedef struct ... {
QueuePtr rear;//队尾指针
} LinkQueue; // 队列定义符号
/**/ /*构造队列*/
LinkQueue * makeQueue() ... {
LinkQueue *Q = (LinkQueue *) malloc(sizeof(LinkQueue));
if(!Q)printf("Q:OVERFLOW ");//存储分配失败
else...{
Q->rear=NULL;
//Q->rear->next = Q->rear;
}
return Q;
}
// 构造节点
QueuePtr makeNode( int i) ... {
QueuePtr N = (QueuePtr)malloc(sizeof(QNode));
if(!N)printf("Node:OVERFLOW ");//存储分配失败
else...{
N->data=i;
N->next=NULL;
}
return N;
}
// 判断队列是否为空
int isEmpty(LinkQueue * Q) ... {
if(Q->rear == NULL)
return 1;
else
return 0;
}
// 将队列置空
void makeNull(LinkQueue * Q) ... {
if(!isEmpty(Q))
printf("错误:队列为空!");
else...{
Q->rear->next = NULL;
}
}
// 删除队列第一个元素
void deQueue(LinkQueue * Q) ... {
if(isEmpty(Q))
printf("错误:队列为空! ");
else...{
QueuePtr p;
p = Q->rear->next;
Q->rear->next = p->next;
free(p);
}
}
/**/ /*返回队列的第一个元素*/
int front(LinkQueue * Q)
... {
int x;
if(!isEmpty(Q))
x=(Q->rear->next->data);
return x;
}
/**/ /*把元素x插入到队列右端*/
void enqueue(LinkQueue * Q, int e)
... {
QueuePtr p = makeNode(e);
if(isEmpty(Q))...{//如果队列为空
Q->rear = p;
Q->rear->next = p;//这个很关键,要以节点为中心
/**//*Q->rear = Q->rear->next;这是错误的表达*/
}
else...{//如果队列不为空
p->next = Q->rear->next;
Q->rear->next = p;
Q->rear = p;
}
}
/**/ /*打印链表*/
void print(LinkQueue * Q) ... {
if(!isEmpty(Q))...{//判断队列是否为空
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if(!p)printf("Node:OVERFLOW ");//存储分配失败
else...{
printf("队列为:");
p=Q->rear->next;
do
...{
printf("%d",p->data);
printf(",");
if(p == Q->rear)
break;
p=p->next;
}while(1);
printf(" ");
}
}
else
printf("错误:检测到队列为空,无法打印! ");
}
#include < stdlib.h >
typedef struct QNode ... {
int data;
struct QNode *next;
} QNode, * QueuePtr; // 节点定义符
typedef struct ... {
QueuePtr rear;//队尾指针
} LinkQueue; // 队列定义符号
/**/ /*构造队列*/
LinkQueue * makeQueue() ... {
LinkQueue *Q = (LinkQueue *) malloc(sizeof(LinkQueue));
if(!Q)printf("Q:OVERFLOW ");//存储分配失败
else...{
Q->rear=NULL;
//Q->rear->next = Q->rear;
}
return Q;
}
// 构造节点
QueuePtr makeNode( int i) ... {
QueuePtr N = (QueuePtr)malloc(sizeof(QNode));
if(!N)printf("Node:OVERFLOW ");//存储分配失败
else...{
N->data=i;
N->next=NULL;
}
return N;
}
// 判断队列是否为空
int isEmpty(LinkQueue * Q) ... {
if(Q->rear == NULL)
return 1;
else
return 0;
}
// 将队列置空
void makeNull(LinkQueue * Q) ... {
if(!isEmpty(Q))
printf("错误:队列为空!");
else...{
Q->rear->next = NULL;
}
}
// 删除队列第一个元素
void deQueue(LinkQueue * Q) ... {
if(isEmpty(Q))
printf("错误:队列为空! ");
else...{
QueuePtr p;
p = Q->rear->next;
Q->rear->next = p->next;
free(p);
}
}
/**/ /*返回队列的第一个元素*/
int front(LinkQueue * Q)
... {
int x;
if(!isEmpty(Q))
x=(Q->rear->next->data);
return x;
}
/**/ /*把元素x插入到队列右端*/
void enqueue(LinkQueue * Q, int e)
... {
QueuePtr p = makeNode(e);
if(isEmpty(Q))...{//如果队列为空
Q->rear = p;
Q->rear->next = p;//这个很关键,要以节点为中心
/**//*Q->rear = Q->rear->next;这是错误的表达*/
}
else...{//如果队列不为空
p->next = Q->rear->next;
Q->rear->next = p;
Q->rear = p;
}
}
/**/ /*打印链表*/
void print(LinkQueue * Q) ... {
if(!isEmpty(Q))...{//判断队列是否为空
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if(!p)printf("Node:OVERFLOW ");//存储分配失败
else...{
printf("队列为:");
p=Q->rear->next;
do
...{
printf("%d",p->data);
printf(",");
if(p == Q->rear)
break;
p=p->next;
}while(1);
printf(" ");
}
}
else
printf("错误:检测到队列为空,无法打印! ");
}
测试代码:
#include
<
stdio.h
>
#include " nofront.h "
void main()
... {
LinkQueue *Queue = makeQueue();
print(Queue);
enqueue(Queue,3);
enqueue(Queue,4);
enqueue(Queue,5);
enqueue(Queue,6);
print(Queue);
int h = front(Queue);
printf("the first element is %d ",h);
deQueue(Queue);
print(Queue);
enqueue(Queue,7);
print(Queue);
}
#include " nofront.h "
void main()
... {
LinkQueue *Queue = makeQueue();
print(Queue);
enqueue(Queue,3);
enqueue(Queue,4);
enqueue(Queue,5);
enqueue(Queue,6);
print(Queue);
int h = front(Queue);
printf("the first element is %d ",h);
deQueue(Queue);
print(Queue);
enqueue(Queue,7);
print(Queue);
}
RUN:
闪……