C++ 队列(quque)实现

队列可以基于数组或链表(即顺序存储和链式存储)

1.顺序存储:

#include<stdio.h>
#include<stdlib.h>
#define maxSize 20
typedef int E;
typedef struct{
    int front;
    int raer;
    E A[maxSize];
}queue,*pQueue;
pQueue create(){
    pQueue p = (pQueue)malloc(sizeof(queue));
    p->front=-1;
    p->raer=-1;
    return p;
}
int isEmpty(pQueue q){//判断队列是否为空
    return q->raer==q->front;
}

int isFull(pQueue q){//判断队列是否为满
    return q->raer >= maxSize-1;
}

E getFront(pQueue q){//获得队头元素
    if (isEmpty(q)) {
        printf("the queue is empty\n");
        return -1;
    }
    else{
        return q->A[q->front+1];
    }

}

void add(pQueue &q , E val){//入队
    if(isFull(q)){
        printf("the queue is full\n");
    }else{
        q->A[++(q->raer)]=val;
    }
}

E pop( pQueue &q){//出队
    if(isEmpty(q)){
        printf("the queue is empty\n");
        return -1;
    }else{
        E old = q->A[++(q->front)];
        return old;
    }
}

void print(pQueue q){//输出队中元素
    int i;
    for(i=q->front+1;i<=q->raer;i++){
        printf("%d ",q->A[i]);
    }
    printf("\n");
}

2.链式存储:

#include<stdio.h>
#include<stdlib.h>
typedef int E;
typedef struct  node
{
   E val;
   node * next;
}qNode,*pQNode;
typedef struct
{
   pQNode rear;
   pQNode front;
}listQueue,*pListQueue;
pListQueue create(){
    pListQueue p=(pListQueue)malloc(sizeof(listQueue));
    p->rear=NULL;
    p->front=NULL;
    return p;
}

int isEmpty(pListQueue q){//判断队列是否为空
    return q->front==NULL;
}

E getFront(pListQueue q){//获得队头元素
    if(isEmpty(q)){
        printf("the queue is empty\n");
        return -1;
    }else{
        return q->front->val;
    }
}

void add(pListQueue &q, int val){//入队
    pQNode node = (pQNode)malloc(sizeof(qNode));
    node->next=NULL;
    node->val=val;
    if(isEmpty(q)){
        q->front=node;
    }else{
        q->rear->next=node;
    }
    q->rear=node;
 
}

E pop(pListQueue &q){//出队
    pQNode node =q->front;
    E old = node->val;
    q->front=q->front->next;
    free(node);
    return old;
    
}
void print(pListQueue q){//输出队中元素
    pQNode node =q->front;
    while(node != NULL){
        printf("%d ",node->val);
        node=node->next;
    }
    printf("\n");
}

3.循环队列:

#include<stdio.h>
#include<stdlib.h>
#define maxSize 20//队中最多含有的元素个数
typedef int E;
typedef struct{
    int front;
    int raer;
    E A[maxSize];
}queue,*pQueue;
pQueue create(){
    pQueue p = (pQueue)malloc(sizeof(queue));
    p->front=-1;
    p->raer=-1;
    return p;
}

int isEmpty(pQueue q){//判断队列是否为空
    return q->raer==q->front;
}

int isFull(pQueue q){//判断队列是否为满
    return (q->raer+1)%maxSize==q->front;
}

E getFront(pQueue q){//获得队头元素
    if (isEmpty(q)) {
        printf("the queue is empty\n");
        return -1;
    }
    else{
        return q->A[q->front+1];
    }

}

void add(pQueue &q , E val){//入队
    if(isFull(q)){
        printf("the queue is full\n");
    }else{
        
        q->A[++(q->raer)%maxSize]=val;
    }
}
E pop( pQueue &q){//出队
    if(isEmpty(q)){
        printf("the queue is empty\n");
        return -1;
    }else{
        q->front=(q->front+1)%maxSize;
        E old = q->A[q->front];
        return old;
    }
}
void print(pQueue q){//输出队中元素
    int i;
    for(i=0;i<=q->raer;i++){
        printf("%d ",q->A[i]);
    }
    printf("\n");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值