ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

数据结构-队列

2021-01-22 10:35:19  阅读:132  来源: 互联网

标签:return 队列 MAXSIZE front 数据结构 rear define


//队列 是只允许在一端进行插入操作,另一端删除操作 先进先出
//队头删除,队尾插入 
//循环队列 队列的头尾相接的顺序存储结构 
 //队列的顺序存储 
 /*
 队列满:(rear+1)%QueueSize==front 
 队列长度: (rear-front+QueueSize)%QueueSize 
 */ 
#include<stdio.h>
#define MAXSIZE 1000
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int Status;
typedef int QElemType;
typedef struct {
    QElemType data[MAXSIZE];
    int front;//头指针 
    int rear;//尾指针,若队列不为空,指向对位元素的下一个位置 
}SqQueue;
/*初始化一个空队列*/
Status InitQueue(SqQueue *Q){
    Q->front=0;
    Q->rear=0;
    return OK;
} 
//返回Q的元素个数
int QueueLength(SqQueue Q){
    return (Q.rear-Q.front+MAXSIZE)%MAXSIZE; 
} 
/*若队列未满,插入元素e为Q的新的队尾元素*/
Status EnQueue(SqQueue *Q,QElemType e){
    if((Q->rear+1)%MAXSIZE==Q->front){
        return ERROR; 
    }//判断队满 
    Q->data[Q->rear]=e;
    Q->rear=(Q->rear+1)%MAXSIZE; 
    return OK;
} 
//删除队头元素
Status DelQueue(SqQueue *Q,QElemType *e){
    if(Q->rear==Q->front){
        return ERROR;
    } //判断队列是否为空 
    *e=Q->data[Q->front];
    Q->front=(Q->front+1)%MAXSIZE;
    return OK; 
} 

---------------------------队列的链式存储

//队列的链式存储结构及实现 
#include<stdio.h>
#define MAXSIZE 1000
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OVERFLOW 0
typedef int Status;
typedef int QElemType;
typedef struct QNode {
    QElemType data;
    struct QNode *next;
}QNode,*QueuePtr;
typedef struct {
    QueuePtr front,rear;
}LinkQueue;
//入队操作
Status EnQueue (LinkQueue *Q,QElemType e){
    QueuePtr s=(QueuePtr)malloc (sizeof(QNode));
    if(!s){
        exit(OVERFLOW);
    }
    s->data=e;
    s->next=NULL;
    Q->rear->next=s;
    Q->rear=s;
    return OK;
} 
//出队操作
Status DeQueue (LinkQueue *Q,QElemType *e){
    QueuePtr p;
    if(Q->front==Q->rear){
        return ERROR;
    }
    p=Q->front->next;
    *e=p->data;
    Q->front->next=p->next;
    if(Q->rear==p){//如果删除的是队尾,将删除后的rear指向头结点 
        Q->rear=Q->front; 
    }
    free(p);
    return OK;
     
}

 

标签:return,队列,MAXSIZE,front,数据结构,rear,define
来源: https://www.cnblogs.com/wyqgzq/p/14311908.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有