队列 定义,也是一种操作受限的线性表,只允许在表的一端进行插入,而在另一端进行删除。 基本操作 InitQueue(&Q);初始化 QueueEmpty(Q);队列是否为空 EnQueue(&Q, x);若队列未满,入队 DeQueue(&Q, &x);若队列非空,出队 GetHead(Q, &x);读取队头元素,若队列非空,将队头元素赋值给x ClearQueue(&Q);清空队列,并回收内存
队列的顺序存储结构
#include<cstdio>
#define MaxSize 50
typedef struct{
int data[MaxSize];
int front,rear;
} SqQueue;
void InitQueue(SqQueue &Q)
{
Q.front = Q.rear = 0;
}
bool QueueEmpty(SqQueue Q)
{
if(Q.front == Q.rear)
return true;
return false;
}
bool EnQueue(SqQueue &Q, int x)
{
Q.data[Q.rear++] = x;
return true;
}
bool DeQueue(SqQueue &Q, int &x)
{
if(Q.front == Q.rear)
return false;
x = Q.data[Q.front++];
return true;
}
bool GetHead(SqQueue Q, int &x)
{
if(Q.front == Q.rear)
return false;
x = Q.data[Q.front];
return true;
}
void ClearQueue(SqQueue &Q)
{
Q.front = Q.rear = 0;
}
int main()
{
SqQueue Q;
InitQueue(Q);
for(int i = 0; i < 10; i++){
if(EnQueue(Q, i)){
int x;
GetHead(Q, x);
}
}
while(!QueueEmpty(Q))
{
int x;
GetHead(Q, x);
printf("当前队头元素是%d\n", x);
DeQueue(Q, x);
printf("出队的元素是%d\n", x);
}
ClearQueue(Q);
return 0;
}
??超级重点 循环队列的l顺序存储结构 为了解决当队尾追上队头时判满和判空时的矛盾有三种方法,分别是
1.牺牲一个存储空间
#include<cstdio>
#define MaxSize 10
typedef struct{
int data[MaxSize];
int front,rear;
} SqQueue;
void InitQueue(SqQueue &Q)
{
Q.front = Q.rear = 0;
}
bool QueueEmpty(SqQueue Q)
{
if(Q.front == Q.rear)
return true;
return false;
}
bool EnQueue(SqQueue &Q, int x)
{
if((Q.rear + 1)%MaxSize == Q.front) return false;
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1) % MaxSize;
return true;
}
bool DeQueue(SqQueue &Q, int &x)
{
if(Q.front == Q.rear)
return false;
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
return true;
}
bool GetHead(SqQueue Q, int &x)
{
if(Q.front == Q.rear)
return false;
x = Q.data[Q.front];
return true;
}
int Length(SqQueue Q){
return (Q.rear - Q.front + MaxSize) % MaxSize;
}
void ClearQueue(SqQueue &Q)
{
Q.front = Q.rear = 0;
}
int main()
{
SqQueue Q;
InitQueue(Q);
for(int i = 0; i < 15; i++){
if(EnQueue(Q, i)){
int x;
GetHead(Q, x);
}
else
printf("%d入队失败\n", i);
}
while(!QueueEmpty(Q))
{
int x;
GetHead(Q, x);
printf("当前队头元素是%d\n", x);
DeQueue(Q, x);
printf("出队的元素是%d\n", x);
}
ClearQueue(Q);
return 0;
}
2.初始为Q.front = Q.rear = Q.size = 0,队满为Q.size == MaxSize
#include<cstdio>
#include<cstring>
#define MaxSize 10
typedef struct{
int data[MaxSize];
int front,rear;
int size;
} SqQueue;
void InitQueue(SqQueue &Q)
{
memset(Q.data, 0, sizeof(Q.data));
Q.front = Q.rear = Q.size = 0;
}
bool QueueEmpty(SqQueue Q)
{
if(Q.size == 0)
return true;
return false;
}
bool EnQueue(SqQueue &Q, int x)
{
if(Q.size == MaxSize) return false;
Q.data[Q.rear] = x;
Q.size++;
Q.rear = (Q.rear + 1) % MaxSize;
return true;
}
bool DeQueue(SqQueue &Q, int &x)
{
if(Q.size == 0)
return false;
x = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize;
Q.size--;
return true;
}
bool GetHead(SqQueue Q, int &x)
{
if(Q.size == 0)
return false;
x = Q.data[Q.front];
return true;
}
int Length(SqQueue Q){
return Q.size;
}
void ClearQueue(SqQueue &Q)
{
Q.front = Q.rear = 0;
}
int main()
{
SqQueue Q;
InitQueue(Q);
for(int i = 0; i < 15; i++){
if(EnQueue(Q, i)){
int x;
GetHead(Q, x);
}
else
printf("%d入队失败\n", i);
}
while(!QueueEmpty(Q))
{
int x;
GetHead(Q, x);
printf("当前队头元素是%d\n", x);
DeQueue(Q, x);
printf("出队的元素是%d\n", x);
}
ClearQueue(Q);
return 0;
}
3. 初始为Q.front = Q.rear = 0,tag = false 当Q.front == Q.rear时,tag == false 为队列插入导致队满 tag == true 为队列删除导致队空
#include<cstdio>
#include<cstring>
#define MaxSize 10
typedef struct{
int data[MaxSize];
int front,rear;
bool tag;
} SqQueue;
void InitQueue(SqQueue &Q)
{
memset(Q.data, 0, sizeof(Q.data));
Q.front = Q.rear = 0;
Q.tag = false;
}
bool QueueEmpty(SqQueue Q)
{
if(Q.front == Q.rear && Q.tag == false)
return true;
return false;
}
bool EnQueue(SqQueue &Q, int x)
{
if(Q.front == Q.rear && Q.tag == true) return false;
Q.data[Q.rear] = x;
Q.rear = (Q.rear + 1)%MaxSize;
Q.tag = true;
return true;
}
bool DeQueue(SqQueue &Q, int &x)
{
if(Q.front == Q.rear && Q.tag == false)
return false;
x = Q.data[Q.front];
Q.front = (Q.front + 1)%MaxSize;
Q.tag = false;
return true;
}
bool GetHead(SqQueue Q, int &x)
{
if(Q.front == Q.rear && Q.tag == false)
return false;
x = Q.data[Q.front];
return true;
}
int Length(SqQueue Q){
return (Q.rear - Q.front + MaxSize) % MaxSize;
}
void ClearQueue(SqQueue &Q)
{
memset(Q.data, 0, sizeof(Q.data));
Q.front = Q.rear = 0;
Q.tag = false;
}
int main()
{
SqQueue Q;
InitQueue(Q);
for(int i = 0; i < 15; i++){
if(EnQueue(Q, i)){
int x;
GetHead(Q, x);
}
else
printf("%d入队失败\n", i);
}
while(!QueueEmpty(Q))
{
int x;
GetHead(Q, x);
printf("当前队头元素是%d\n", x);
DeQueue(Q, x);
printf("出队的元素是%d\n", x);
}
ClearQueue(Q);
return 0;
}
队列的链式存储结构
#include <cstdio>
#include <cstdlib>
typedef struct LinkNode{
int data;
struct LinkNode *next;
}LinkNode;
typedef struct{
LinkNode *front, *rear;
}LinkQueue;
void InitQueue(LinkQueue &Q)
{
Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
Q.front->next = NULL;
}
bool QueueEmpty(LinkQueue Q)
{
if(Q.front == Q.rear)
return true;
return false;
}
bool EnQueue(LinkQueue &Q, int x)
{
LinkNode *p = (LinkNode*)malloc(sizeof(LinkNode));
p->data = x;
p->next=NULL;
Q.rear->next = p;
Q.rear = p;
return true;
}
bool DeQueue(LinkQueue &Q, int &x)
{
if(Q.front == Q.rear)
return false;
LinkNode *p = Q.front->next;
x = p->data;
Q.front->next = p->next;
if(p == Q.rear)
Q.rear = Q.front;
free(p);
return true;
}
bool GetHead(LinkQueue Q, int &x)
{
if(Q.front == Q.rear)
return false;
x = Q.front->next->data;
return true;
}
int Length(LinkQueue Q){
LinkNode *p = Q.front->next;
int cnt = 0;
while(p != NULL){
cnt++;
p = p->next;
}
return cnt;
}
void ClearQueue(LinkQueue &Q)
{
LinkNode *p = Q.front->next;
while(p != NULL){
LinkNode *tmp = p;
p = p->next;
free(tmp);
}
Q.front->next = Q.rear->next = NULL;
}
int main()
{
LinkQueue Q;
InitQueue(Q);
for(int i = 0; i < 10; i++){
if(EnQueue(Q, i)){
printf("%d入队成功\n", Q.rear->data);
}
else
printf("%d入队失败\n", i);
}
printf("入队后队列中元素的个数是%d\n", Length(Q));
while(!QueueEmpty(Q)){
int x;
GetHead(Q, x);
printf("当前队首元素%d\n", x);
DeQueue(Q, x);
printf("出队元素是%d\n", x);
}
ClearQueue(Q);
printf("清空后队列中元素的个数是%d\n", Length(Q));
return 0;
}
|