广大考研数据结构复习之队列

王道书籍上队列的基本操作和课后部分习题

一:循环队列(数组实现)

/**
 * @author Dawn
 * @date 2019年11月12日20:39:27
 * @version 1.0
 * 循环队列:关于循环队列的最大的特点就是,在每次入队和出队以及判断队列是否满了,的时候都用的是模。而不是普通的加加减减
 */
#include<stdio.h>
#include<stdlib.h>

#define MaxSize 6

typedef int ElemType;
typedef struct {
    ElemType data[MaxSize];//存放队列的元素
    int front, rear;//队列头和队列尾
}SeQueue;

//1.初始化
void InitQueue(SeQueue& Q) {
    Q.rear = Q.front = 0;//标志,不是指针哦!!
}
//2.判断队列是否为空
bool IsEmpty(SeQueue Q) {
    if (Q.front == Q.rear)
        return true;
    else
        return false;
}
//3.入队
bool EnQueue(SeQueue& Q,ElemType x) {
    //判断是否为满了的
    if ((Q.rear + 1) % MaxSize == Q.front) {
        printf("队列到此撑了\n");
        return false;
    }
        

    Q.data[Q.rear] = x;
    Q.rear = (Q.rear + 1) % MaxSize;
    return true;
}
//4.出队
bool DeQueue(SeQueue& Q, ElemType& x) {
    //如果为空
    if (Q.rear == Q.front)
        return false;

    x = Q.data[Q.front];
    Q.front = (Q.front + 1) % MaxSize;
    return true;
}
int main() {
    SeQueue Q;
    InitQueue(Q);

    printf("队列是否为空:%s\n", IsEmpty(Q) ? "" : "不为空");
    EnQueue(Q, 1);
    EnQueue(Q, 2);
    EnQueue(Q, 3);
    EnQueue(Q, 4);
    EnQueue(Q, 5);
    EnQueue(Q, 5);//会执行失败
    printf("队列是否为空:%s\n", IsEmpty(Q) ? "" : "不为空");

    //开始出队列
    int x;
    DeQueue(Q, x);
    printf("出队元素为:%d\n", x);//出队元素为1,先进先出
    return 0;
}

 

二:队列的链式存储

/**
 * @author Dawn
 * @date 2019年11月11日22:25:13
 * @version 1.0
 * 队列的复习:链式存储
 */
#include<stdio.h>
#include<stdlib.h>

typedef int ElemType;

typedef struct LinkNode {//在王道书籍上这里有个小错误。没有写LinkNode,不写会报错的
    ElemType data;
    struct LinkNode* next;
}LinkNode;

typedef struct {
    LinkNode* front, * rear;//队列的队头和队尾指针
}LinkQueue;

//1.初始化
void InitQueue(LinkQueue& Q) {
    //带头节点哈
    Q.front = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
    Q.front->next = NULL;//初始为空
}
//2.判断队列是否为空
bool IsEmpty(LinkQueue Q) {
    if (Q.front == Q.rear)
        return true;
    else
        return false;
}
//3.入队
void EnQueue(LinkQueue& Q,ElemType x){
    
    LinkNode* p = (LinkNode*)malloc(sizeof(LinkNode));
    p->data = x;
    //插入到队列中
    Q.rear->next = p;
    Q.rear = p;
}
//4.出队
bool DeQueue(LinkQueue& Q, ElemType &x) {
    //如果队列为空
    if (Q.front == Q.rear)
        return false;
    LinkNode* p = Q.front->next;//待出队的元素
    x = p->data;

    Q.front->next = p->next;
    if (Q.rear == p)
        Q.rear = Q.front;//如果只有一个元素,删除后变空
    free(p);

    return true;
}

//王道书籍后的用2个栈(s1,s2)来实习队列
//思路?入栈:将元素放到s1中,如果s1中满了,就必须等s2栈为“空”。再将s1中的元素“全部”放入s2中
//        出栈:将s2中的元素取出,如果s2空了,则将s1中的“所有”元素放进s2中


int main() {
    LinkNode L;
    LinkQueue Q;
    InitQueue(Q);
    printf("是否队列为空:%s\n", IsEmpty(Q) ? "" : "不空");
    EnQueue(Q, 1);
    EnQueue(Q, 2);
    EnQueue(Q, 3);
    EnQueue(Q, 4);
    EnQueue(Q, 5);
    printf("是否队列为空:%s\n", IsEmpty(Q) ? "" : "不空");

    ElemType x;
    DeQueue(Q, x);
    printf("出来的第一个元素为:%d\n", x);
    DeQueue(Q, x);
    printf("出来的第二个元素为:%d\n", x);
    return 0;
}

 

posted @ 2019-11-12 23:50  大魔王阿黎  阅读(202)  评论(0编辑  收藏  举报