DS--队列(循环队列和链队列)

/*****************

Project: Queue(Sequence Circular queue and LinkQueue)
Date: 2021/11/12
Author: Alex_Yrdm

******************/

循环链表

/***************
Part1: Sequence Circular queue
***************/

/*
Sequence Circular queue functions:
    int InitQueue(Queue& q);            参数:循环队列q,功能:初始化队列,时间复杂度:O(1)
    int size(Queue q);                  参数:循环队列q,功能:返回队列中元素数目.时间复杂度:O(1)
    bool IsEmpty(Queue q);              参数:循环队列q,功能:判断循环队列是否为空,时间复杂度O(1)
    int EnQueue(Queue& q, Elemtype e);  参数:循环队列q,入队列元素e.功能:将e入队尾.时间复杂度O(1)
    int DeQueue(Queue& q, Elemtype& e); 参数:循环队列q,出队元素e.功能:删除队头元素并用e返回.时间复杂度O(1)
    bool GetHead(Queue q, Elemtype& e); 参数:循环队列q,队头元素e.功能:获得队头元素.时间复杂度:O(1)
*/
#define MAXSIZE 100
#include<stdio.h>
#include<stdlib.h>
typedef int Elemtype;
typedef struct {
    Elemtype* base;
    int front;
    int rear;
}Queue;
int InitQueue(Queue& q);
int size(Queue q);
bool IsEmpty(Queue q);
int EnQueue(Queue& q, Elemtype e);
int DeQueue(Queue& q, Elemtype& e);
bool GetHead(Queue q, Elemtype& e);
int main() {
    /*






    */
    return 0;
}
int InitQueue(Queue& q) {
    q.base = (Elemtype*)malloc(MAXSIZE*sizeof(Elemtype));
    if (!q.base) exit(-1);
    q.front = 0;
    q.rear = 0;
    return 1;
}
int size(Queue q) {
    return (q.rear - q.front + MAXSIZE) % MAXSIZE;
}
bool IsEmpty(Queue q)
{
    if (q.rear==q.front) return true;
    return false;
}
int EnQueue(Queue& q, Elemtype e) {
    if ((q.rear + 1) % MAXSIZE == q.front) return -1;
    q.base[q.rear] = e;
    q.rear = (q.rear + 1) & MAXSIZE;
    return 1;
}
int DeQueue(Queue& q, Elemtype& e) {
    if (q.rear == q.front) return -1;
    e = q.base[q.front];
    q.front = (q.front + 1) % MAXSIZE;
    return 1;
}
bool GetHead(Queue q, Elemtype& e) {
    if (q.rear == q.front) return false;
    e = q.base[q.front];
    return true;
}

链队列

/***********
Part2: LinkQueue
***********/

/*
* 带头结点!!!方便些
    LinkQueue functions:                        
    int InitQueue(LinkQueue& q);                参数:链队列q,功能:初始化队列,时间复杂度:O(1)
    int size(LinkQueue q);                      参数:链队列q,功能:返回队列中元素数目,时间复杂度:O(n)
    int EnQueue(LinkQueue& q, Elemtype e);      参数:链队列q,入队元素e,功能:将e加入队尾,时间复杂度:O(1)
    int DeQueue(LinkQueue& q, Elemtype& e);     参数:链队列q,出队元素e.功能:删除队头元素并用e返回,时间复杂度:O(1)
    bool GetHead(LinkQueue q, Elemtype& e);     参数:链队列q,队头元素e,功能:返回队头元素e,时间复杂度:O(1)
    bool IsEmpty(LinkQueue q);                  参数:链队列q,功能:判断队列是否为空,时间复杂度:O(1)

*/
#include<stdio.h>
#include<stdlib.h>

typedef int Elemtype;

typedef struct QNode{
    Elemtype data;
    struct QNode* next;
}QNode,*Queueptr;
typedef struct {
    Queueptr front;
    Queueptr rear;
}LinkQueue;

int InitQueue(LinkQueue& q);
int size(LinkQueue q);
int EnQueue(LinkQueue& q, Elemtype e);
int DeQueue(LinkQueue& q, Elemtype& e);
bool GetHead(LinkQueue q, Elemtype& e);
bool IsEmpty(LinkQueue q);
int main() {
    /*







    */
    return 0;
}
int InitQueue(LinkQueue& q) {
    q.front = q.rear = (Queueptr)malloc(sizeof(QNode));
    if (!q.front) exit(-1);
    q.front->next = NULL;
    return 1;
}
int size(LinkQueue q) {
    int m = 0;
    if (q.front->next) {
        m++;
        q.front = q.front->next;
    }
    return m;
}
int EnQueue(LinkQueue& q, Elemtype e) {
    if (!q.front) return -1;
    Queueptr p;
    p = (Queueptr)malloc(sizeof(QNode));
    if (!p) return -1;
    p->data = e;
    p->next = NULL;
    q.rear->next = p;
    q.rear = p;
    return 1;
}
int DeQueue(LinkQueue& q, Elemtype& e) {
    if (!q.front || q.front == q.rear)  return -1;
    Queueptr p;
    p = q.front->next;
    e = p->data;
    q.front->next = p->next;
    if (q.rear == p) q.rear = q.front;//2021/11/14日,使用二叉树层序遍历时出现bug,原因竟然是q.rear=p 啊啊啊啊
    free(p);
    return 1;
}
bool GetHead(LinkQueue q, Elemtype& e) {
    if (!q.front||q.front==q.rear) return false;
    e = q.front->next->data;
    return true;
}
bool IsEmpty(LinkQueue q) {
    if (q.front == q.rear) return true;
    return false;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怡人蝶梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值