队列的实现(循环顺序存储结构+链式存储结构+STL类模板)

队列的实现(循环顺序存储结构+链式存储结构+STL类模板)

队列

1.循环顺序存储结构

// 队列(循环队列顺序存储结构)C++.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include <iostream>
#include<stdio.h>
using namespace std;

typedef int ElemType;
#define OK 1
#define ERROR 0
//Status是状态码,标识着函数执行结果的状态,OK或者ERROR
typedef int Status;

class SqQueue {
private:
    int MaxSize;        //数组最大长度
    int front;          //头指针
    int rear;           //尾指针,指向队尾元素的下一个位置
    ElemType* list;     //指向数组的指针
public:
    SqQueue();
    ~SqQueue();
    int QueueLength();              //返回队列的元素个数,也就是队列的长度
    Status EnQueue(ElemType e);     //若队列未满,则插入元素e为新的队尾元素
    Status Dequeue();               //若队列不空,则删除队头元素
    void ClearQueue();              //清空队列
    void display();
};
SqQueue::SqQueue() {    //初始化一个空队列
    front = 0;
    rear = 0;
    MaxSize = 99;       //默认最大长度为99
    list = new int[MaxSize];
}
SqQueue::~SqQueue() {
    delete[]list;
}
int SqQueue::QueueLength() {
    return (rear - front + MaxSize) % MaxSize;
}
Status SqQueue::EnQueue(ElemType e) {
    if ((rear + 1) % MaxSize == front)    //队列满没满的判断
        return ERROR;
    list[rear] = e;
    rear = (rear + 1) % MaxSize;
    return OK;
}
Status SqQueue::Dequeue() {
    if (front == rear)    //队列为空的判断
        return ERROR;
    front = (front + 1) % MaxSize;
    return OK;
}
void SqQueue::ClearQueue() {
    front = 0;
    rear = 0;
}
void SqQueue::display() {
    if (front == rear)
        cout << "当前为空队列!!!" << endl;
    else
    {
        cout << "当前队列内元素有(头---->尾):";
        for (int i =front,j=0 ; j < QueueLength(); i++,j++)
        {
            cout << list[i%MaxSize] << " ";
        }
        cout << endl;
    }
}
int main()
{
    SqQueue test;
    test.display();
    test.EnQueue(2);
    test.display();
    test.EnQueue(3);
    test.display();
    test.EnQueue(4);
    test.display();
    test.Dequeue();
    test.display();
    test.ClearQueue();
    test.display();
    return 0;
}

2.链式存储结构

// 队列(链式存储结构)C++.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include <iostream>
#include<stdio.h>
using namespace std;

typedef int ElemType;
#define OK 1
#define ERROR 0
//Status是状态码,标识着函数执行结果的状态,OK或者ERROR
typedef int Status;

//单链表结点类
class Node {
public:
    ElemType data;  //节点数据
    Node* next;
    Node() {		
        next = NULL;
        data = 0;
    }
};
class LinkQueue {
private:
    Node* front;		     //队头节点指针     //front一直为空,这里还是头结点表示整个链表,第一个元素节点在链表的第二个节点
    Node* rear;              //队尾节点指针
    int len;		         //链表长度
public:
    LinkQueue();
    ~LinkQueue();
    Status EnQueue(ElemType e); //插入元素e为新的队尾元素
    Status DeQueue();           //若队列不空,则删除对头元素
    Status ClearQueue();        //清空队列内元素
    void display();             //打印所有队列内元素,(头----->尾)
};
LinkQueue::LinkQueue() {
    front = new Node;
    rear = front;         //重要!!!!  一开始初始化rear和front为同一个节点,表示空对列
    len = 0;
}
LinkQueue::~LinkQueue() {
    Node* p, * q;
    p = front;		 //p指向第一个节点
    for (int i = 0; i < len; i++)
    {
        q = p->next;
        delete p;
        p = q;
    }
    front->next = NULL;  //!!!
    rear = front;
    len = 0;
}
Status LinkQueue::EnQueue(ElemType e) {
    Node* temp = new Node;
    temp->data = e;
    temp->next = NULL;
    rear->next = temp;
    rear = temp;
    len++;
    return OK;
}
Status LinkQueue::DeQueue() {
    Node *p = new Node;
    if (front == rear)          //判断队列是否为空
        return ERROR;
    p = front->next;            //要删除的队头节点
    front->next = p->next;      //删除队头节点
    if (rear == p)    //如果队列只有一个元素,删除后,rear指向头结点front,表示空队列
        rear = front;
    delete p;
    len--;
    return OK;
}
Status LinkQueue::ClearQueue() {
    Node* p, * q;
    p = front->next;		 //p指向第二个节点
    for (int i = 0; i < len-1; i++)
    {
        q = p->next;
        delete p;
        p = q;
    }
    front->next=NULL;  //!!!
    rear = front;
    len = 0;
    return OK;
}
void LinkQueue::display() {
    Node* temp;
    if (rear == front)
        cout << "当前为空队列!!!" << endl;
    else
    {
        temp = front;
        cout << "当前对列内元素有:";
        for (int i = 0; i < len; i++)
        {
            cout << temp->next->data << " ";
            temp = temp->next;
        }
        cout << endl;
    }
}

int main() {
    LinkQueue test;
    test.EnQueue(1);
    test.EnQueue(2);
    test.EnQueue(3);
    test.display();

    test.DeQueue();
    test.display();

    test.ClearQueue();
    test.EnQueue(3);
    test.EnQueue(1);
    test.display();

    test.ClearQueue();
    test.display();

    //test.ClearQueue();
    //test.display();
    return 0;
}

3.STL类模板实现

队列queue的用法如下:

  • 1.包含头文件:#include <queue>
  • 2.定义一个整数队列对象:queue<int> myQe;
  • 3.定义一个整数队列对象数组:queue myQA[10];
  • 4.入队操作:myQe.push(itemp); //把整数itemp进入队列
  • 5.出队操作:myQe.pop(); //把队头元素弹出队列,注意本操作不获取队头元素
  • 6.获取队头元素: itemp = myQe.front(); // 把队头元素放入itemp中,注意本操作不弹出元素
  • 7.判断队列是否为空:myQe.empty();//队列空则返回true,不空则返回false
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值