顺序队列初始化/入队/出队列/判断队空

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#define MAXSIZE 100
using namespace std;
//顺序队列的类型
struct sequeue{
    int _queue[MAXSIZE] ;
    int _front , _rear ;
};
//顺序队列的常用形式就是循环队列,循环队列的基本运算
void initqueue(struct sequeue *q){
    q->_front = 0 ;
    q->_rear = 0 ;
}
//入队列算法,若队列未满插入队尾并返回入队成功标志1,否则返回队列已满入队不成功标志0
int addqueue(struct sequeue * q , int x){
    if(( q->_rear + 1 ) % MAXSIZE == q->_front)
        return 0 ;
    q->_rear = (q->_rear + 1) % MAXSIZE ;
    q->_queue[q->_rear] = x ;
    return 1 ;
}

//出队列算法,若队列不空,删除队头元素并返回其值,否则返回NULL
int outqueue(struct sequeue * q ){
    if(q ->_front == q->_rear)
        return NULL ;
    q->_front =( q->_front + 1) % MAXSIZE ;
    return q->_queue[q->_front] ;
}
//读队头元素,与出队列的差别仅在于没有修改队头指针
int getqueue(struct sequeue *q){
    if(q->_front== q->_rear )
        return NULL ;
    return q->_queue[(q->_front+1) % MAXSIZE] ;
//注意,因为通常约定队尾指针指向当前队尾元素的实际位置,而队头指针指向当前队头元素实际位置的前一个位置

}
//判断队列为空,若队列为空则返回1,否则返回0
int isempty(struct sequeue * q){
    if(q->_front == q->_rear)
        return 1 ;
    return 0 ;
}
int main(){
    struct sequeue * r ;
    r = (struct sequeue *)malloc(sizeof(struct sequeue)) ;
    initqueue(r) ;
    for(int i = 1 ; i<= 8 ; i++)
        addqueue(r , i) ;
    while(!isempty(r)){
        int temp = outqueue(r) ;
        cout<<"outqueue = "<< temp << endl ;
    }
   return 0 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值