码迷,mamicode.com
首页 > 其他好文 > 详细

循环队列的基本接口实现

时间:2017-11-26 22:59:00      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:eth   实现   als   span   queue   head   循环队列   amp   max   

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE   1
#define FALSE  0
#define OK     1
#define ERROR  0
#define IBFEASIBLE  -1
#define OVERFLOW    -2 

typedef int Status;
typedef int ElemType; /* 元素类型为int类型*/

//循环队列类型 
typedef struct{
    ElemType *elem;      //存储空间的基址
    int front;            //对头位标
    int rear;             //队尾位标
    int maxSize;         //存储容量 
}SqQueue; 


//初始化空队列 
Status InitQueue_Sq(SqQueue &Q, int size);
//销毁队列
Status DestroyQueue_Sq(SqQueue &Q);
//将Q置为空队列
void  ClearQueue_Sq(SqQueue &Q);
//判断是否为空队列
Status QueueEmpty_Sq(SqQueue &Q);
//返回队列元素个数
int QueueLength_Sq(SqQueue &Q);
//若队列不空,则用e返回Q的队列头
Status GetHead_Sq(SqQueue Q,ElemType &e);
//在队尾插入元素
Status EnQueue_Sq(SqQueue &Q,ElemType e);
//删除队头元素
Status DeQueue_Sq(SqQueue &Q,ElemType &e); 
//遍历打印队列
void TraverseQueue_Sq(SqQueue &Q); 

//循环队列基本接口实现

//1.初始化空队列 
Status InitQueue_Sq(SqQueue &Q, int size){
    Q.elem = (ElemType*)malloc(size*sizeof(ElemType));
    if(NULL==Q.elem) return OVERFLOW;
    Q.maxSize=size;
    Q.front=Q.rear=0;
    return OK;
} 

//2.销毁队列
Status DestroyQueue_Sq(SqQueue &Q){
    free(Q.elem); 
       Q.elem=NULL; 
      Q.front=Q.rear=0;
      return OK; 
} 

//3. 置为空队列
void  ClearQueue_Sq(SqQueue &Q){
    Q.front=Q.rear=0; 
}

//4. 判断是否为空对列
Status QueueEmpty_Sq(SqQueue &Q){
     if(Q.front==Q.rear){
         return TRUE;
     }else{
         return FALSE;
     }
}

//5.求队列元素个数
int QueueLength_Sq(SqQueue &Q){
    if(Q.rear-Q.front<0) {
        return Q.maxSize-Q.front+Q.rear;
    }else{
        return Q.rear-Q.front;
    } 
} 

//6.返回队头元素,并返回OK
Status GetHead_Sq(SqQueue Q,ElemType &e){
    if(Q.maxSize<=0) return ERROR; 
    e=Q.elem[Q.front];
    return OK;
} 

//7.在队尾位置插入元素
Status EnQueue_Sq(SqQueue &Q,ElemType e){
    if((Q.rear+1)%Q.maxSize==Q.front) return ERROR;
    Q.elem[Q.rear]=e;
    Q.rear=(Q.rear+1)%Q.maxSize;
    return OK; 
} 

//8.出队
Status DeQueue_Sq(SqQueue &Q,ElemType &e){
    if(Q.front==Q.rear) return ERROR;
    e=Q.elem[Q.front];
    Q.front=(Q.front+1)%Q.maxSize;
    return OK; 
}
//9.遍历并打印元素
void TraverseQueue_Sq(SqQueue &Q)  
{  
    int i=Q.front;  
    printf("队中的元素是:");  
    while(i%Q.maxSize!=Q.rear)  
    {  
        printf("%4d ",Q.elem[i]);  
        i++;  
    }  
    printf("\n");  
} 
//测试函数 
int main(){
    int i,e,n=10,a,j; 
    SqQueue Q;
    do{
        printf("1.初始化循环队列\n");
        printf("2.销毁循环队列\n");
        printf("3.将循环队列置为空队列\n");
        printf("4.判断循环队列是否为空\n");
        printf("5.循环队列的长度\n");
        printf("6.返回循环队列的队头元素\n");
        printf("7.在队尾插入元素\n");
        printf("8.删除队头元素\n");
        printf("9.遍历并打印队列元素\n"); 
        printf("请输入你要进行的操作:\n"); 
        scanf("%d",&i);
        switch(i){
            case 1:
                   if(InitQueue_Sq(Q,n)){
                       printf("初始化成功\n");
                       printf("请输入5个元素:\n");
                       for(j=0;j<5;j++){
                           scanf("%d",&a);
                           EnQueue_Sq(Q,a);
                       }
                   };
                   break;
            case 2:
                   DestroyQueue_Sq(Q);
                   printf("销毁队列成功\n");
                   break; 
            case 3:
                   ClearQueue_Sq(Q);
                   printf("置空成功\n"); 
                   break;
            case 4:
                   if(QueueEmpty_Sq(Q)){
                       printf("循环队列为空!\n"); 
                   }else{
                       printf("循环队列不为空!\n"); 
                   } 
                   break;
            case 5:
                   printf("循环队列的长度为:%d\n",QueueLength_Sq(Q));
                   break;
            case 6:
                   GetHead_Sq(Q,e);
                   printf("循环队列队头元素为:%d\n",e);
                   break;
            case 7:
                   printf("请输入你要插入的元素:");
                   scanf("%d",&e);
                   if(EnQueue_Sq(Q,e)){
                        printf("插入成功\n"); 
                   }
                   break;
            case 8: 
                   DeQueue_Sq(Q,e);
                   printf("删除成功\n"); 
                   break;
            case 9:
                   TraverseQueue_Sq(Q);
                   break;
        } 
    }while(i>0&&i<10);
    return 0;
} 

 

循环队列的基本接口实现

标签:eth   实现   als   span   queue   head   循环队列   amp   max   

原文地址:http://www.cnblogs.com/linwx/p/7900690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!