数据算法与结构-队列(循环队列,链式队列)

415 阅读4分钟

循环队列:为充分利用向量空间,克服"假溢出"现象的方法是:将向量空间想象为一个首尾相接的圆环,并称这种向量为循环向量。存储在其中的队列称为循环队列(Circular Queue)。循环队列是把顺序队列首尾相连,把存储队列元素的表从逻辑上看成一个环,成为循环队列。

顺序队列、循环队列 图解如下图所示:

循环队列详细图解

首先我们定义下队列的数据结构

1.循环队列的初始化条件如何判断?

  1. front == rear ? front == 0 ?
  2. 循环队列判断队列为空 只需要判断 front == rear是否相等,因为队列是前进状态的,如果判断是否为0 则需要加入更多复杂的判断

2.循环队列的初始化条件如何判断?

一般情况下我们会保留一个空间判断 (rear+1)%maxSize == front 来定义队列是否已满,保留一个存储空间

#define MAXSIZE 20 /* 存储空间初始分配量 */
typedef int Status;
typedef int QElemType; /* QElemType类型根据实际情况而定,这里假设为int */

/* 循环队列的顺序存储结构 */
typedef struct
{
    QElemType data[MAXSIZE];
    int front;        /* 头指针 */
    int rear;        /* 尾指针,若队列不空,指向队列尾元素的下一个位置 */
}SQueue;

// 初始化一个空队列Q
Status InitQueue(SqQueue *Q){
    Q->front = 0;
    Q->rear = 0;
    return OK;
}

// 判断队列是否为空队列,是则返回TRUR,否则返回FALSE;
Status QueueEmpty(SqQueue Q){
    //队空标记
    if (Q.front == Q.rear)
    return TRUE;
    else
    return FALSE;
}

// 将队列清空 
Status ClearQueue(SqQueue *Q){
//清空队列我们值需要重新定义front rear 的指向,不需要清空data数据
    Q->front = Q->rear = 0;
    return OK;
}


//队列的元素个数,也就是队列的当前长度
int QueueLength(SqQueue Q){
    return (Q.rear - Q.front + MAXSIZE)%MAXSIZE;
}

// 获取队列的头 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR;
Status GetHead(SqQueue Q,QElemType *e){
    //队列已空
    if (Q.front == Q.rear)
        return ERROR;
    
    *e = Q.data[Q.front];
    return OK;
    
}

// 若队列未满,则插入元素e为新队尾元素
Status EnQueue(SqQueue *Q,QElemType e){
    
    //队列已满
    if((Q->rear+1)%MAXSIZE == Q->front)
        return ERROR;
    
    //将元素e赋值给队尾
    Q->data[Q->rear] = e;
    
    //rear指针向后移动一位,若到最后则转到数组头部;
    Q->rear = (Q->rear+1)%MAXSIZE;
    
    return OK;
}

链式队列:物理结构为链式存储结构的队列,对内存空间的利用率更高。不需要如上定义maxSize了,只要需要我们可以一直在rear添加下一个节点,首先我们创建下数据结构

#define ERROR 0
#define TRUE 1
#define FALSE 0
#define OK 1
typedef int Status;/* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int ElemType;/* ElemType类型根据实际情况而定,这里假设为int */

//节点所需的单链数据结构
typedef struct QueueNode {
    ElemType data;
    struct QueueNode *next;
} QueueNode, *QueueNodePtr;

//链式队列的数据结构 ,我们只需要知道第一个与最后一个节点就好了
typedef struct {
    QueueNodePtr front;
    QueueNodePtr rear;
} LinkQueue;

/// 初始化
Status InitQueue(LinkQueue *Q)
{
    // 初始队列为空,只有头节点,不是有效数据的节点
    //为什么我们要创建一个空的头节点?只是方便操作,如果没有头节点对首元节点对删除操作会比较复杂
    *Q->front = *Q->rear = (QueueNodePtr)malloc(sizeof(QueueNode));
    if (*Q->front == NULL) return ERROR;
    
    // 头节点的后面为空
    *Q->front->next = NULL;
    
    return OK;
}

/* 销毁队列Q*/
Status DestoryQueue(LinkQueue *Q){
    
    //遍历整个队列,销毁队列的每个结点
    while (Q->front) {
        Q->rear = Q->front->next;
        free(Q->front);
        Q->front = Q->rear;
    }
    return OK;
    
}

/* 将队列Q置空*/
Status ClearQueue(LinkQueue *Q){
    
    QueuePtr p,q;
    Q->rear = Q->front;
    p = Q->front->next;
    Q->front->next = NULL;
    
    while (p) {
        
        q = p;
        p = p->next;
        free(q);
        
    }
    
    return OK;
}

/* 判断队列Q是否为空*/
Status QueueEmpty(LinkQueue Q){
    if (Q.front == Q.rear)
        return TRUE;
    else
        return FALSE;
}

/* 获取队列长度*/
int QueueLength(LinkQueue Q){
    int i= 0;
    QueuePtr p;
    p = Q.front;
    while (Q.rear != p) {
        i++;
        p = p->next;
    }
    return i;
}

/* 插入元素e为队列Q的新元素*/
Status EnQueue(LinkQueue *Q,QElemType e){
    
    //为入队元素分配结点空间,用指针s指向;
    QueuePtr s = (QueuePtr)malloc(sizeof(QNode));
    
    //判断是否分配成功
    if (!s) {
         return ERROR;
    }
    
    //将新结点s指定数据域.
    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
    p = Q->front->next;
    
    //将要删除的队头结点的值赋值给e
    *e = p->data;
    
    //将原队列头结点的后继p->next 赋值给头结点后继
    Q->front->next = p ->next;
    
    //若队头就是队尾,则删除后将rear指向头结点.
    if(Q->rear == p) Q->rear = Q->front;
    
    free(p);
    
    return OK;
}

/* 获取队头元素*/
Status GetHead(LinkQueue Q,QElemType *e){
   
    //队列非空
    if (Q.front != Q.rear) {
        //返回队头元素的值,队头指针不变
        *e =  Q.front->next->data;
        return TRUE;
    }
    
    return  FALSE;
    
}

/* 遍历队列*/
Status QueueTraverse(LinkQueue Q){
    
    QueuePtr p;
    p = Q.front->next;
    while (p) {
        printf("%d ",p->data);
        p = p->next;
    }
    printf("\n");
    return OK;
}