队列和栈简单实现

数据结构队列

队列是一种访问受限的线性表。队列的元素只能从表的一端插入,另一端删除(即队头插入,队尾删除)。
队列的实现中,主要涉及到初始化,入队,出队,判断队空,判断队满,遍历队列等操作。下面是对队列的简单实现。

/***环形队列实现*****/
// 头文件
class MyQueue
{
public:
    MyQueue(int queueCapacity);         // 初始化队列,创建对垒
    virtual ~MyQueue();                 // 销毁队列
    void ClearQueue();                  // 清空队列
    bool QueueEmpty() const;            // 判空队列
    bool QueueFull() const;             // 判满队列
    int QueueLength() const;            // 队列长度
    bool EnQueue(int element);          // 入队操作
    bool DeQueue(int &element);         // 出队操作
    void QueueTravese();                // 遍历队列
private:
    int *m_pQueue;                      // 队列数组指针
    int m_iQueueLen;                    // 队列元素个数
    int m_iQueueCapacity;               // 队列数组容量
    int m_iHead;                        // 队列头指针                    
    int m_iTail;                        // 队列尾指针 

// 源文件
#include "MyQueue.h"
#include <iostream>
using namespace std;

MyQueue::MyQueue(int queueCapacity)
{
    m_iQueueCapacity = queueCapacity;
    m_iHead = 0;
    m_iTail = 0;
    m_iQueueLen = 0;
    m_pQueue = new int[queueCapacity];
}

MyQueue::~MyQueue()
{
    delete [] m_pQueue;
    m_pQueue = NULL;
}

void MyQueue::ClearQueue()
{
    m_iHead = 0;
    m_iTail = 0;
    m_iQueueLen = 0;
}

bool MyQueue::QueueEmpty() const
{
    return m_iQueueLen == 0 ? true : false;
}

bool MyQueue::QueueFull() const
{
    return m_iQueueCapacity == m_iQueueLen ? true : false;
}

int MyQueue::QueueLength() const
{
    return m_iQueueLen;
}

bool MyQueue::EnQueue(int element)
{
    if (QueueFull())
        return false;
    m_pQueue[m_iTail] = element;
    m_iTail++;
    m_iTail = m_iTail % m_iQueueCapacity;
    m_iQueueLen++;
    return true;
}

bool MyQueue::DeQueue(int &element)
{
    if (QueueEmpty())
        return false;
    element = m_pQueue[m_iHead];
    m_iHead++;
    m_iHead = m_iHead % m_iQueueCapacity;
    m_iQueueLen--;
    return true;
}

void MyQueue::QueueTravese()
{
    for (int i = m_iHead; i < m_iHead + m_iQueueLen; i++)
    {
        cout << m_pQueue[i % m_iQueueCapacity] << " ";
    }
    cout << endl;
}

// 测试文件
#include "MyQueue.h"
#include <iostream>
using namespace std;

int main()
{
    MyQueue *p = new MyQueue(4);
    p->EnQueue(10);
    p->EnQueue(12);
    p->EnQueue(8);
    p->EnQueue(3);
    p->EnQueue(5);
    p->QueueTravese();

    int e = 0;
    p->DeQueue(e);
    cout << e << endl;

    p->DeQueue(e);
    cout << e << endl;

    p->QueueTravese();
    p->ClearQueue();
    p->QueueTravese();

    p->EnQueue(20);
    p->EnQueue(5);
    int len = p->QueueLength();
    cout << len << endl;
    p->QueueTravese();

    delete p;
    p = NULL;
    return 0;
}

栈也是操作受限的线性表。它只允许在一端进行插入和删除操作。栈的主要操作包括:判断栈是否为空,栈是否满,栈中元素个数,入栈操作,出栈操作等。

栈的应用:递归调用,表达式求值,进制转换,括号匹配判断,深度遍历等。

下面是栈的基本实现:

// 头文件
#ifndef STACK_H
#define STACK_H

class MyStack
{
public:
    MyStack(int size);           // 分配栈空间
    virtual ~MyStack();          // 栈析构函数
    bool stackEmpty();           // 判断栈空
    bool stackFull();            // 判断栈满
    void clearStack();           // 清空栈
    int stackLength();           // 栈中元素长度
    bool push(char elem);        // 入栈操作
    bool pop(char &elem);        // 出栈操作
    void stackTraverse();        // 栈遍历操作
private:
    char *m_pBuffer;             // 栈空间指针
    int m_iSize;                 // 栈容量
    int m_iTop;                  // 栈顶指针
};
#endif

// 源文件
#include"MyStack.h"
#include <iostream>
using namespace std;

MyStack::MyStack(int size)
{
    m_iSize = size;
    m_pBuffer = new char[size];
    m_iTop = 0;
}

MyStack::~MyStack()
{
    delete []m_pBuffer;
    m_pBuffer = NULL;
}

bool MyStack::stackEmpty()
{
    return m_iTop == 0 ? true : false;
}

bool MyStack::stackFull()
{
    return m_iSize == m_iTop ? true : false;
}

void MyStack::clearStack()
{
    m_iTop = 0;
}

int MyStack::stackLength()
{
    return m_iTop;
}

bool MyStack::push(char elem)
{
    if (stackFull())
        return false;
    m_pBuffer[m_iTop++] = elem;
    return true;
}

bool MyStack::pop(char &elem)
{
    if (stackEmpty())
        return false;
    elem = m_pBuffer[--m_iTop];
    return true;
}

void MyStack::stackTraverse()
{
    for (int i = 0; i < m_iTop; i++)
    {
        cout << m_pBuffer[i] << " ";
    }
    cout << endl;
}

// 测试文件
#include "MyQueue.h"
#include "MyStack.h"
#include <iostream>
using namespace std;
int main()
{
    MyStack *pStack = new MyStack(5);
    int len = pStack->stackLength();
    cout << len << endl;
    pStack->push('h');
    pStack->push('m');
    pStack->push('e');
    pStack->push('l');
    pStack->push('y');
    pStack->push('r');
    if (pStack->stackEmpty())
    {
        cout << "栈为空" << endl;
    }
    if (pStack->stackFull())
    {
        cout << "栈为满" << endl;
    }
    pStack->stackTraverse();
    char t;
    pStack->pop(t);
    pStack->stackTraverse();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值