队列的三种实现方式

来源:互联网 发布:找淘宝兼职工作靠谱吗 编辑:程序博客网 时间:2024/05/16 04:13

普通顺序存储结构

#include <cstdio>#include <iostream>#include <cstring>#include <cstdlib>const int maxSize = 100 ;using namespace std ;typedef struct{    int data[maxSize] ;    int front ;  //定义头尾指针    int rear ;}SqQueue ;void initQueue(SqQueue *&q)  //初始化队列{    q = (SqQueue *)malloc(sizeof(SqQueue)) ;    q->front = q->rear = -1 ;}void destroyQueue(SqQueue *&q)    //销毁队列{    free(q) ;}bool QueueEmpty(SqQueue *q)       //判断队列是否为空{    return (q->rear == q->front) ;}bool enQueue(SqQueue *&q,int e)  //将元素e入队列{    if(q->rear == maxSize-1)   //队列已满 入队失败        return false ;    q->rear++ ;    q->data[q->rear] = e ;    return true ;}bool deQueue(SqQueue *&q,int &e)   //出队列,并用返回出队元素{    if(q->rear == q->front)        return false ;      //队列为空 无法入队        q->front++ ;    e = q->data[q->front] ;    return true ;}int main(){    SqQueue *queue ;    int e ;    initQueue(queue) ;    enQueue(queue,12) ;    deQueue(queue,e) ;    printf("出队的元素是%d\n",e) ;    if(QueueEmpty(queue))    {        printf("队列为空\n") ;    }    else    {        printf("队列不为空\n");    }    //enQueue(queue,1) ;}

环形队列

#include <cstdio>#include <iostream>#include <cstring>#include <cstdlib>const int maxSize = 100 ;using namespace std ;typedef struct{    int data[maxSize] ;    int front ;    int rear ;}SqQueue;void initQueue(SqQueue *&q)    //初始化队列{    q = (SqQueue*)malloc(sizeof(SqQueue)) ;    q->front = q->rear = 0 ;}void destroyQueue(SqQueue *&q)   //销毁队列{    free(q) ;}bool enQueue(SqQueue *&q,int e)  //将元素e入队q{    if(q->rear == maxSize-1)  //队列已满,入队失败        return false ;    q->rear = (q->rear+1)%maxSize ;   //通过对maxSize循环取余解决假溢出问题    q->data[q->rear] = e ;    return true ;}bool deQueue(SqQueue *&q,int &e)  //队头元素出队,用e记录出队元素{    if(q->front == q->rear)   //队列中没有元素,无法出队        return false ;    q->front = (q->front+1)%maxSize ;    e = q->data[q->front] ;    return true ;}int main(){    SqQueue *queue ;    initQueue(queue) ;    int e ;    enQueue(queue,3) ;    deQueue(queue,e) ;    printf("出队元素是%d\n",e) ;    return  0  ;}

队列的链式表示
#include <cstdio>#include <iostream>#include <cstdlib>#include <cstring>using namespace std ;typedef struct QNode{    int data ;    struct QNode *next ;}QNode;typedef struct{    QNode *front ;    QNode *rear ;}LiQueue ;void initQueue(LiQueue *&q)   //初始化队列{    q = (LiQueue *)malloc(sizeof(LiQueue)) ;    q->front = q->rear = NULL ;    printf("队列创建成功\n") ;}void destroyQueue(LiQueue *&q)   //销毁队列{    QNode *p = q->front ,*r;    if(p != NULL)    {        r = p->next ;        while(r != NULL)        {            free(p) ;            p = r ;            r = p->next ;        }    }    free(p) ;    free(q) ;    printf("队列已经成功销毁\n") ;}bool QueueEmpty(LiQueue *q){    return (q->front == q->rear) ;}void enQueue(LiQueue *&q,int e){    QNode *p ;    p = (QNode *)malloc(sizeof(QNode)) ;    p->data = e ;    if(q->front == q->rear)  //如果当前队列为空,则新节点既是头指针又是尾指针    {        q->front = q->rear = p ;    }    else    {        q->rear->next = p ;        q->rear = p ;    }    printf("%d已经成功入队\n",e) ;}bool delQueue(LiQueue *&q,int &e)  //出队,并且用保存出队元素{    if(q->rear == NULL)        return false ;    QNode *t = q->front ;    e = t->data ;    if(q->front == q->rear)        q->front = q->rear = NULL ;    else q->front->next = t->next ;    free(t) ;    printf("%d已成功出队\n",e) ;    return true ;}int main(){    LiQueue *queue ;    initQueue(queue) ;    printf("%d\n",QueueEmpty(queue)) ;    enQueue(queue,3) ;    int e ;    delQueue(queue,e) ;}



0 0
原创粉丝点击