数据结构——队列与栈

本文贴出队列、栈 的模板类代码,可以直接调用,如有需要可进行相应的修改。
文中代码均已在VS2015上测试,空指针均为nullptr(C++11)。参考来源:慕课网

队列

FIFO(First In First Out)先进先出
队头,队尾

普通队列

环形队列:充分利用内存空间

示例(环形队列):

#ifndef MYQUEUE_H
#define MYQUEUE_H
/****************************/
/*环形队列C++类[MyQueue.h]  */
/****************************/
class MyQueue
{
public:
    MyQueue(int queueCapacity);//InitQueue(&Q)创建队列
    virtual ~MyQueue();//DestroyQueue(&Q)销毁队列
    void ClearQueue();//ClearQueue(&Q)清空队列
    bool QueueEmpty()const;//QueueEmpty(Q)判空队列
    bool QueueFull() const;//判满队列
    int QueueLength()const;//QueueLength(Q)队列长度
    bool EnQueue(int element);//EnQueue(&Q,element)新元素入队
    bool DeQueue(/*int &element*/);//DeQueue(&Q,&element)首元素出队
    void QueueTraverse();//QueueTraverse(Q,visit())遍历队列
private:
    int *m_pQueue;//队列数组指针
    int m_iQueueLen;//队列元素个数
    int m_iQueueCapacity;//队列数组容量
    int m_iHead;
    int m_iTail;
};
#endif


/******************************/
/*环形队列C++实现[MyQueue.cpp]*/
/******************************/
#include "MyQueue.h"
#include 
using namespace std;
MyQueue::MyQueue(int queueCapacity)
{
    m_iQueueCapacity = queueCapacity;
    //ClearQueue();
    m_iHead = 0;
    m_iTail = 0;
    m_iQueueLen = 0;
    m_pQueue = new int[m_iQueueCapacity];
}

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

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

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

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

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

bool MyQueue::EnQueue(int element)
{
    if (QueueFull())
    {
        return false;
    }
    else 
    {
        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;
    }
    else
    {
        /*element = m_pQueue[m_iHead];*/
        m_iHead++;
        m_iHead = m_iHead % m_iQueueCapacity;
        m_iQueueLen--;
        return true;
    }
    
}

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


/******************************/
/*环形队列使用实例[Main.cpp]  */
/******************************/
#include"MyQueue.h"
#include 
using namespace std;

int main()
{
    MyQueue *p = new MyQueue(4);
    p->EnQueue(10);
    p->EnQueue(12);
    p->EnQueue(14);
    p->EnQueue(18);
    p->EnQueue(20);
    cout << p->QueueEmpty() << endl;
    cout << p->QueueFull() << endl;
    cout << p->QueueLength() << endl;
    p->QueueTraverse();
    p->DeQueue();
    p->QueueTraverse();
    p->EnQueue(200);
    p->QueueTraverse();
    p->ClearQueue();
    p->QueueTraverse();
    p->EnQueue(500);
    p->QueueTraverse();
    delete p;
    p = nullptr;
    return 0;
}

LIFO(Last In First Out)后进先出

栈顶

示例:

#ifndef MYSTACK_H
#define MYSTACK_H
/****************************/
/*栈C++类[MyStack.h]        */
/****************************/
class MyStack
{
public:
    MyStack(int size);
    ~MyStack();
    bool stackEmpty();
    bool stackFull();
    void clearStack();
    int stackLength();
    bool push(char elem);
    bool pop(char &elem);
    void stackTraverse(bool isFromBottom);

private:
    char *m_pBuffer;
    int m_iTop;
    int m_iSize;
};
#endif


/******************************/
/*栈C++实现[MyStack.cpp]      */
/******************************/
#include "MyStack.h"
#include 
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 = nullptr;
}

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

bool MyStack::stackFull()
{
    if (m_iTop == m_iSize)
    {
        return true;
    }
    return 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;
    m_iTop++;
    return true;
}

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

void MyStack::stackTraverse(bool isFromBottom)
{
    if (isFromBottom)
    {
        for (int i = 0; i < stackLength(); i++)
        {
            cout << m_pBuffer[i] << " ";
        }
        cout << endl;
    }
    else
    {
        for (int i = stackLength()-1; i >=0; i--)
        {
            cout << m_pBuffer[i] << " ";
        }
        cout << endl;
    }
}


/******************************/
/*栈使用实例[Main.cpp]        */
/******************************/
#include"MyStack.h"
#include 
using namespace std;

int main()
{
    MyStack *p = new MyStack(3);
    cout << p->stackEmpty() << endl;
    cout << p->stackFull() << endl;
    cout << p->stackLength() << endl;
    p->push('c');p->push('+');p->push('+');
    p->stackTraverse(1);
    char elem = NULL;
    p->pop(elem);
    p->stackTraverse(0);
    p->clearStack();
    p->stackTraverse(0);
    return 0;
}

进阶

环形队列模板类(注意,模板类 不支持头文件与cpp文件分离

#ifndef MYQUEUE_H
#define MYQUEUE_H
#include 
using namespace std;
/********************/
/*环形队列C++实现     */
/********************/
template
class MyQueue
{
public:
    MyQueue(int queueCapacity);//InitQueue(&Q)创建队列
    virtual ~MyQueue();//DestroyQueue(&Q)销毁队列
    void ClearQueue();//ClearQueue(&Q)清空队列
    bool QueueEmpty()const;//QueueEmpty(Q)判空队列
    bool QueueFull() const;//判满队列
    int QueueLength()const;//QueueLength(Q)队列长度
    bool EnQueue(T element);//EnQueue(&Q,element)新元素入队
    bool DeQueue(/*int &element*/);//DeQueue(&Q,&element)首元素出队
    void QueueTraverse();//QueueTraverse(Q,visit())遍历队列

private:
    T *m_pQueue;//队列数组指针
    int m_iQueueLen;//队列元素个数
    int m_iQueueCapacity;//队列数组容量
    int m_iHead;
    int m_iTail;
};

template
MyQueue::MyQueue(int queueCapacity)
{
    m_iQueueCapacity = queueCapacity;
    //ClearQueue();
    m_iHead = 0;
    m_iTail = 0;
    m_iQueueLen = 0;
    m_pQueue = new T[m_iQueueCapacity];
}
template
MyQueue::~MyQueue()
{
    delete[]m_pQueue;
    m_pQueue = nullptr;
}
template
void MyQueue::ClearQueue()
{
    m_iHead = 0;
    m_iTail = 0;
    m_iQueueLen = 0;
}
template
bool MyQueue::QueueEmpty() const
{
    //return m_iQueueLen == 0 ? true : false;
    if (m_iQueueLen == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}
template
bool MyQueue::QueueFull() const
{
    if (m_iQueueLen == m_iQueueCapacity)
    {
        return true;
    }
    return false;
}
template
int MyQueue::QueueLength() const
{
    return m_iQueueLen;
}
template
bool MyQueue::EnQueue(T element)
{
    if (QueueFull())
    {
        return false;
    }
    else
    {
        m_pQueue[m_iTail] = element;
        m_iTail++;
        m_iTail = m_iTail % m_iQueueCapacity;
        m_iQueueLen++;
        return true;
    }
}
template
bool MyQueue::DeQueue(/*T & element*/)
{
    if (QueueEmpty())
    {
        return false;
    }
    else
    {
        /*element = m_pQueue[m_iHead];*/
        m_iHead++;
        m_iHead = m_iHead % m_iQueueCapacity;
        m_iQueueLen--;
        return true;
    }

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

#endif// !MYQUEUE_H

栈模板类(注意,模板类 不支持头文件与cpp文件分离

#ifndef MYSTACK_H
#define MYSTACK_H
#include 
using namespace std;

template
class MyStack
{
public:
    MyStack(int size);
    ~MyStack();
    bool stackEmpty();
    bool stackFull();
    void clearStack();
    int stackLength();
    bool push(T elem);
    bool pop(T &elem);
    void stackTraverse(bool isFromBottom);

private:
    T *m_pBuffer;
    int m_iTop;
    int m_iSize;
};

template
MyStack::MyStack(int size)
{
    m_iSize = size;
    m_pBuffer = new T[size];
    m_iTop = 0;
}
template
MyStack::~MyStack()
{
    delete[]m_pBuffer;
    m_pBuffer = nullptr;
}
template
bool MyStack::stackEmpty()
{
    if (0 == m_iTop)
    {
        return true;
    }
    return false;
}
template
bool MyStack::stackFull()
{
    if (m_iTop == m_iSize)
    {
        return true;
    }
    return false;
}
template
void MyStack::clearStack()
{
    m_iTop = 0;
}
template
int MyStack::stackLength()
{
    return m_iTop;
}
template
bool MyStack::push(T elem)
{
    if (stackFull())
    {
        return false;
    }
    m_pBuffer[m_iTop] = elem;
    m_iTop++;
    return true;
}
template
bool MyStack::pop(T &elem)
{
    if (stackEmpty())
    {
        return false;
    }
    m_iTop--;
    elem = m_pBuffer[m_iTop];
    return true;
}
template
void MyStack::stackTraverse(bool isFromBottom)
{
    if (isFromBottom)
    {
        for (int i = 0; i < stackLength(); i++)
        {
            cout << m_pBuffer[i] << " ";
        }
        cout << endl;
    }
    else
    {
        for (int i = stackLength() - 1; i >= 0; i--)
        {
            cout << m_pBuffer[i] << " ";
        }
        cout << endl;
    }
}

#endif // !MYSTACK_H

栈应用之进制转换

十进制转换二进制,八进制,十六进制。
原理:短除法,将余数push进栈,然后pop出栈。
方案一:main.cpp

#include "MyStack.h"
#define BINARY 2
#define OCTONARY 8
#define HEXADECIMAL 16

int main()
{
    char num[] = "0123456789ABCDEF";
    MyStack *p = new MyStack(30);
    int N = 2016;
    int mod = 0;
    while (N != 0)
    {
        mod = N % HEXADECIMAL;
        p->push(num[mod]);
        N = N / HEXADECIMAL;
    }
    p->stackTraverse(false);
    return 0;
}

方案二:main.cpp

#include "MyStack.h"
#define BINARY 2
#define OCTONARY 8
#define HEXADECIMAL 16

int main()
{
    char num[] = "0123456789ABCDEF";
    MyStack *p = new MyStack(30);
    int N = 2016;
    int mod = 0;
    while (N != 0)
    {
        mod = N % HEXADECIMAL;
        p->push(mod);
        N = N / HEXADECIMAL;
    }
    int elem = 0;
    while(!p->stackEmpty())
    {
        p->pop(elem);
        cout << num[elem];
    }
    return 0;
}

栈应用之括号匹配

任意输入一组括号,判断括号是否匹配。[()]、[()()]、[[()]]

main.cpp

/**只限输入[]()字符进行检测*/
#include "MyStack.h"
#define BINARY 2
#define OCTONARY 8
#define HEXADECIMAL 16

int main(void)
{
    MyStack *pOrig = new MyStack(30);
    MyStack *pNeed = new MyStack(30);
    char str[] = "[()]]";
    char currentNeed = 0;
    for(int i = 0;i < strlen(str);i++)
    {
        if (str[i] != currentNeed)
        {
            pOrig->push(str[i]);
            switch (str[i])
            {
            case '[':
                if (currentNeed != 0)
                {
                    pNeed->push(currentNeed);
                }
                currentNeed = ']';
                break;
            case '(':
                if (currentNeed != 0)
                {
                    pNeed->push(currentNeed);
                }
                currentNeed = ')';
                break;
            default:
                cout << "字符串不匹配" << endl;
                return 0;
            }
        }
        else
        {
            char elem;
            pOrig->pop(elem);
            if (!pNeed->pop(currentNeed))
            {
                currentNeed = 0;
            };
        }
    }
    if (pOrig->stackEmpty())
    {
        cout << "匹配"<

你可能感兴趣的:(数据结构——队列与栈)