我与数据结构有个约会,带你领略不一样的数据结构!
#include <stdio.h>
#include <stdlib.h>
#define Maxsize 100
typedef int QElemType;
typedef struct{
QElemType data[Maxsize];
int front; //头指针
int rear; //尾指针
}SqQueue;
//初始化
void InitQueue(SqQueue &L){
L.front =L.rear = 0;
}
//队列长度
int QueueLength(SqQueue L){
return (L.rear - L.front + Maxsize) % Maxsize;
}
//入队
bool EnQueue (SqQueue &L,int x){
if((L.rear+1)%Maxsize == L.front){ //判断队满条件
return false;
}
L.data[L.rear] = x;
//指针向后移动
L.rear = (L.rear+1)%Maxsize;
return true;
}
//出队列
bool DeQueue(SqQueue &L,int &e){
if(L.front == L.rear){
return false;
}
e = L.data[L.front];
//移动
L.front = (L.front+1)%Maxsize;
return true;
}
//输出
void Print(SqQueue L){
int len = QueueLength(L);
for(int k = L.front;k<=len;k++){
printf("%d",L.data[k]);
}
}
int main(){
int x,e=0;
SqQueue l;
InitQueue(l);
for(int i=0;i<5;i++){
scanf("%d",&x);
EnQueue(l,x);
}
printf("\n");
DeQueue(l,e);
Print(l);
system("pause");
}
网友评论