循环队列一种实现
2017-09-22 19:18
295 查看
Queue实现方式有两种:数组和链表,本文属于数组实现,为了防止队列出现“明明还有存储空间,但是却提示队列已满”的情况,故使用循环队列
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
typedef struct Qnode {
int size;
int front;
int rear;
char data[MAX_SIZE];
}Squeue,*Squeueptr;
void init_squeue(Squeueptr *sq)
{
*sq = malloc(sizeof(Squeue));
if(*sq == NULL)
printf("Error: alloc memory failed!!\n");
else {
(*sq)->front = 0;
(*sq)->rear = 0;
(*sq)->size = 0;
(*sq)->data[0] = '\0';
}
}
int ensqueue(Squeueptr sq, char e)
{
if(sq == NULL) {
printf("Error:this squeue don't exsit!!\n");
return -1;
}
if(sq->front == (sq->rear + 1)%MAX_SIZE) {
printf("Error:this squeue already full!!\n");
return -1;
}
sq->data[sq->rear] = e;
sq->rear++;
if(sq->rear == MAX_SIZE)
sq->rear = 0;
sq->data[sq->rear] = '\0';
return 0;
}
int desqueue(Squeueptr sq, char *e)
{
if(sq == NULL) {
printf("Error:this squeue don't exsit!!\n");
return -1;
}
if(sq->front == sq->rear) {
printf("Error:this squeue already empty!!\n");
return -1;
}
*e = sq->data[sq->front];
sq->data[sq->front] = '\0';
sq->front++;
if(sq->front == MAX_SIZE)
sq->front = 0;
return 0;
}
void print_squeue(Squeueptr sq)
{
int i;
if(sq == NULL) {
printf("Error:this squeue don't exsit!!\n");
return;
}
for(i=0;i<MAX_SIZE;i++)
printf("yxw test0911: data[%d] = %c\n",i,sq->data[i]);
printf("yxw test0911: front = %d\n",sq->front);
printf("yxw test0911: rear = %d\n",sq->rear);
}
int main(void)
{
Squeueptr sq;
char c;
int n;
init_squeue(&sq);
while(1) {
printf("---------menu display-----------------\n");
printf("---------0: enqueue a data-----------------\n");
printf("---------1: dequeue a data-----------------\n");
printf("---------3: display a queue-----------------\n");
scanf("%d",&n);
getchar();
switch(n){
case 0:
printf("please input a data to add: ");
scanf("%c",&c);
ensqueue(sq,c);
break;
case 1:
if(!desqueue(sq,&c))
printf("this is c = %c\n",c);
break;
case 2:
print_squeue(sq);
break;
default:
print_squeue(sq);
break;
}
}
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
typedef struct Qnode {
int size;
int front;
int rear;
char data[MAX_SIZE];
}Squeue,*Squeueptr;
void init_squeue(Squeueptr *sq)
{
*sq = malloc(sizeof(Squeue));
if(*sq == NULL)
printf("Error: alloc memory failed!!\n");
else {
(*sq)->front = 0;
(*sq)->rear = 0;
(*sq)->size = 0;
(*sq)->data[0] = '\0';
}
}
int ensqueue(Squeueptr sq, char e)
{
if(sq == NULL) {
printf("Error:this squeue don't exsit!!\n");
return -1;
}
if(sq->front == (sq->rear + 1)%MAX_SIZE) {
printf("Error:this squeue already full!!\n");
return -1;
}
sq->data[sq->rear] = e;
sq->rear++;
if(sq->rear == MAX_SIZE)
sq->rear = 0;
sq->data[sq->rear] = '\0';
return 0;
}
int desqueue(Squeueptr sq, char *e)
{
if(sq == NULL) {
printf("Error:this squeue don't exsit!!\n");
return -1;
}
if(sq->front == sq->rear) {
printf("Error:this squeue already empty!!\n");
return -1;
}
*e = sq->data[sq->front];
sq->data[sq->front] = '\0';
sq->front++;
if(sq->front == MAX_SIZE)
sq->front = 0;
return 0;
}
void print_squeue(Squeueptr sq)
{
int i;
if(sq == NULL) {
printf("Error:this squeue don't exsit!!\n");
return;
}
for(i=0;i<MAX_SIZE;i++)
printf("yxw test0911: data[%d] = %c\n",i,sq->data[i]);
printf("yxw test0911: front = %d\n",sq->front);
printf("yxw test0911: rear = %d\n",sq->rear);
}
int main(void)
{
Squeueptr sq;
char c;
int n;
init_squeue(&sq);
while(1) {
printf("---------menu display-----------------\n");
printf("---------0: enqueue a data-----------------\n");
printf("---------1: dequeue a data-----------------\n");
printf("---------3: display a queue-----------------\n");
scanf("%d",&n);
getchar();
switch(n){
case 0:
printf("please input a data to add: ");
scanf("%c",&c);
ensqueue(sq,c);
break;
case 1:
if(!desqueue(sq,&c))
printf("this is c = %c\n",c);
break;
case 2:
print_squeue(sq);
break;
default:
print_squeue(sq);
break;
}
}
}
相关文章推荐
- 循环队列的一种实现模型
- 8.用定长数组 实现 循环队列
- 数组实现循环队列
- C++用数组实现栈和循环队列
- 简单循环队列(C++模版技术实现)
- 数组实现循环队列
- 两种方法实现队满和队空的判断操作(循环队列)
- 用C语言实现简单循环队列结构
- 数据结构:循环队列(C语言实现)
- C语言实现,顺序队列,循环队列,和栈!
- Java数组实现循环队列的两种方法
- 循环队列的表示和实现(数据结构)
- 数据结构学习----顺序循环队列(Java实现)
- 循环队列--数组实现
- 循环队列Queue--使用顺序存储结构(数组)实现
- 循环队列操作之一:循环队列的表示和实现(C语言版本)
- 循环队列拾遗-使用数组实现循环队列
- 数据结构 c语言实现链式循环队列(输入数字入队 输入字母出队)
- 数据结构基础——循环队列的C++实现
- C语言使用非循环双向链表实现队列