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

47 _ 循环队列程序演示.swf

时间:2017-06-05 23:53:16      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:队列的实现   删除元素   index   printf   std   循环队列   循环   logs   span   

通过上面的分析我们已经对循环队列很了解了,现在我们来学习下循环队列的实现形式

1、代码使用数组现实循环队列

#include<stdio.h>
#include<malloc.h>
#include <stdio.h>  
#include <stdlib.h> 
typedef struct Queue{
    int * data;//存储的数据,指向一个数组 
    int font ;//队列数据删除
    int rear;//队列数据的添加
     
}QUEUE ,*PQUEUE;


/*初始化队列 6表示数组的长度*/
void initQueue( PQUEUE pQueue){
    /*
    队列有三个元素构成:
    1、一个数组
    2、font标志
    3、rear标志 
    */
    //首先让队列 执行一个地址
     pQueue->data = (int*)malloc(sizeof(int)*6);//该数组存储6个int类型的数据
     pQueue->font = 0;
     pQueue->rear = 0; 
    
}

/*
判断队列是否已经存储满了 
*/
bool full_quequ( PQUEUE pQueue){
    if((pQueue->rear+1)%6 == pQueue->font){
        return true;
    }else{
        return false;
    }
}

/*对队列进行遍历*/
void   traverse_queue(PQUEUE pQueue){
    int index = pQueue->font;
    while(index != pQueue->rear){
        printf("%d\n",pQueue->data[index]);
        index = (index+1)%6;
    }
} 

/*向队列中存储数据*/
bool en_queue(PQUEUE pQueue ,int val){
 if(full_quequ(pQueue)){
     return false;
 }else{
     //将输出放入数组对应的当前rear的位置
     pQueue->data[pQueue->rear] = val;
    //rear向上移动
    pQueue->rear = (pQueue->rear+1)%6;//数组的长度是6
     return true; 
      
 }
    
}

/*判断队列是否为空*/
bool isEmptyQueue(PQUEUE pQueue){
    
    if(pQueue->font == pQueue->rear ){
        return true;
    }else{
        return false;
    }
    
}

/*删除队列中的元素,将删除的元素保存到pVal的值中*/
bool delete_quequ(PQUEUE pQueue ,int *pVal){
    if(isEmptyQueue(pQueue)){
        return false;
    }else{
        //删除元素
        *pVal =  pQueue->data[pQueue->font];
        //font指针上移动
        pQueue->font = (pQueue->font +1) % 6;
        return true; 
    }
} 
int main(){
    QUEUE s ;
    initQueue(&s);
    //数组的长度是6,一个空间存储rear,有效的空间只有5个,只能存储5个有效的数据 
    en_queue(&s,1);
    en_queue(&s,2);
    en_queue(&s,3);
    en_queue(&s,1);
    en_queue(&s,2);

    traverse_queue(&s);
    return 0;
}

 

47 _ 循环队列程序演示.swf

标签:队列的实现   删除元素   index   printf   std   循环队列   循环   logs   span   

原文地址:http://www.cnblogs.com/kebibuluan/p/6947218.html

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