首页 > 其他分享 >【数据结构】2.栈和队列

【数据结构】2.栈和队列

时间:2023-10-02 17:45:13浏览次数:37  
标签:const 队列 void queue int arrayLength 数据结构 size

1.栈

1.1栈的抽象父类

#pragma once
template<class T>
class Stack
{
public:
    // 析构函数
    virtual ~Stack() {}
    // 栈是否为空
    virtual bool empty() const = 0;
    // 栈的大小
    virtual int size() const = 0;
    // 栈顶元素
    virtual T& top() = 0;
    // 出栈
    virtual void pop() = 0;
    // 入栈
    virtual void push(const T& theElement) = 0;
};

1.2栈的数组实现

【数据结构】1.线性表的数组描述和链式描述 - imXuan - 博客园 (cnblogs.com)

#pragma once
#include"stack.h"
#include"arrayList.hpp"
#include<iostream>
using namespace std;
template<class T>
class DerivedArrayStack :private ArrayList<T>, public Stack<T>
{
public:
    DerivedArrayStack(int initialCapacity = 10) :ArrayList<T>(initialCapacity) {}
    // 栈是否为空
    bool empty() const { return ArrayList<T>::empty(); }
    // 栈的大小
    int size() const { return ArrayList<T>::size(); }
    // 栈顶元素
    T& top() 
    {
        if (ArrayList<T>::empty())
            cout << "栈为空" << endl;
        else 
            return ArrayList<T>::get(ArrayList<T>::size() - 1);
    }
    // 出栈
    void pop()
    {
        if (ArrayList<T>::empty())
            cout << "栈为空" << endl;
        else
        {
            cout << "出栈元素为:" << ArrayList<T>::get(ArrayList<T>::size() - 1) << endl;
            ArrayList<T>::erase(ArrayList<T>::size() - 1);
        }            
    }
    // 入栈
    void push(const T& theElement)
    {
        ArrayList<T>::insert(ArrayList<T>::size(), theElement);
    }
};

1.3栈的链式实现

【数据结构】1.线性表的数组描述和链式描述 - imXuan - 博客园 (cnblogs.com)

#pragma once
#include"chain.hpp"
#include"stack.h"
#include<iostream>
using namespace std;
template<class T>
class DerivedLinkedStack :private Chain<T>, public Stack<T>
{
public:
    DerivedLinkedStack(int initialCapacity = 10) :Chain<T>(initialCapacity) {}
    // 栈是否为空
    bool empty() const { return Chain<T>::empty(); }
    // 栈的大小
    int size() const { return Chain<T>::size(); }
    // 栈顶元素
    T& top()
    {
        if (Chain<T>::empty())
            cout << "栈为空" << endl;
        else
            return Chain<T>::get(Chain<T>::listSize - 1);
    }
    // 出栈
    void pop() 
    {
        if (Chain<T>::empty())
            cout << "栈为空" << endl;
        else
        { 
            cout << "出栈元素为:" << Chain<T>::get(Chain<T>::listSize - 1) << endl;
            Chain<T>::erase(Chain<T>::listSize - 1);
        }
    }
    // 入栈
    void push(const T& theElement)
    {
        Chain<T>::insert(Chain<T>::size(), theElement);
    }
};

2.队列

2.1队列的抽象父类

#pragma once
template<class T>
class queue
{
public:
    virtual ~queue() {}
    virtual bool empty() const = 0;
    virtual int size() const = 0;
    virtual T& front() = 0;
    virtual T& back() = 0;
    virtual void pop() = 0;
    virtual void push(const T& theElement) = 0;
};

2.2队列的数组实现

队列由一个数组来描述,队首指针是队首的前一个元素,队尾指针是队尾所在元素,当队首和队尾指针重叠时表示队列为空;当 (队尾指针+1)%数组长度 等于队首指针时队列为满

假设push数据时队列已满,需要为存储队列的数组进行扩充,我们将AB首先移动到起始位置,然后将C~G移动到AB之后,具体代码参见 push()

  

#pragma once
#include"queue.h"
#include"arrayList.hpp"
#include<iostream>
using namespace std;
template<class T>
class arrayQueue :public queue<T>
{
private:
    int theFront;       // 第一个元素的前一个
    int theBack;        // 最后一个元素
    int arrayLength;    // 队列长度
    T* queue;           // 队列本体
public:
    arrayQueue(int initialCapacity = 10);
    ~arrayQueue() { delete[] queue; }
    bool empty() const { return theFront == theBack; }
    int size() const
    {
        return (theBack - theFront + arrayLength) % arrayLength;
    }
    T& front()
    {
        if (theFront == theBack)
        {
            cout << "队列为空" << endl;
        }
        return queue[(theFront + 1) % arrayLength];
    }
    T& back()
    {
        if (theFront == theBack)
        {
            cout << "队列为空" << endl;
        }
        return queue[theBack];
    }
    void pop()
    {
        if (theFront == theBack)
        {
            cout << "队列为空" << endl;
            return;
        }
        cout << queue[(theFront + 1) % arrayLength];
        theFront = (theFront + 1) % arrayLength;
        queue[theFront].~T();
    }
    void push(const T& theElement);
};


template<class T>
arrayQueue<T>::arrayQueue(int initialCapacity)
{
    if (initialCapacity < 1)
    {
        cout << "initialCapacity 必须为正整数" << endl;
        return;
    }
    arrayLength = initialCapacity;
    queue = new T[arrayLength];
    theFront = 0;
    theBack = 0;
}

template<class T>
void arrayQueue<T>::push(const T& theElement)
{
    if ((theBack + 1) % arrayLength == theFront)
    {
        T* newQueue = new T[2 * arrayLength];

        int start = (theFront + 1) % arrayLength;
        if (start < 2)
            copy(queue + start, queue + start + arrayLength - 1, newQueue);
        else
        {
            copy(queue + start, queue + arrayLength, newQueue);
            copy(queue, queue + theBack + 1, newQueue + arrayLength - start);
        }

        theFront = 2 * arrayLength - 1;
        theBack = arrayLength - 2;
        arrayLength *= 2;
        queue = newQueue;
    }

    theBack = (theBack + 1) % arrayLength;
    queue[theBack] = theElement;
}

2.3队列的链式实现

【数据结构】1.线性表的数组描述和链式描述 - imXuan - 博客园 (cnblogs.com)

#pragma once
#include "queue.h"
#include "chainNode.hpp"
#include <iostream>
using namespace std;

template<class T>
class linkedQueue : public queue<T>
{
private:
    chainNode<T>* queueFront;  // 头指针
    chainNode<T>* queueBack;   // 尾指针
    int queueSize;             // 队列大小

public:
    linkedQueue(int initialCapacity = 10)
    {
        queueFront = NULL; queueSize = 0;
    }
    ~linkedQueue();
    bool empty() const {return queueSize == 0;}
    int size() const { return queueSize;}
    T& front()
    {
        if (queueSize == 0)
            cout << "队列为空" << endl;
        return queueFront->element;
    }
    T& back()
    {
        if (queueSize == 0)
            cout << "队列为空" << endl;
        return queueBack->element;
    }
    void pop();
    void push(const T&);
};

template<class T>
linkedQueue<T>::~linkedQueue()
{
    while (queueFront != NULL)
    {
        chainNode<T>* nextNode = queueFront->next;
        delete queueFront;
        queueFront = nextNode;
    }
}

template<class T>
void linkedQueue<T>::pop()
{
    if (queueFront == NULL)
    {
        cout << "队列为空" << endl;
        return;
    }
    chainNode<T>* nextNode = queueFront->next;
    delete queueFront;
    queueFront = nextNode;
    queueSize--;
}

template<class T>
void linkedQueue<T>::push(const T& theElement)
{
    chainNode<T>* newNode = new chainNode<T>(theElement, NULL);

    if (queueSize == 0)
        queueFront = newNode;
    else
        queueBack->next = newNode;
    queueBack = newNode;

    queueSize++;
}

 

标签:const,队列,void,queue,int,arrayLength,数据结构,size
From: https://www.cnblogs.com/stux/p/17738713.html

相关文章

  • 基础数据结构:数组实现的单链表(静态链表)、双链表
    1、单链表(静态链表)以AcWing.826为例,题目要求如下:实现一个单链表,链表初始为空,支持三种操作:向链表头插入一个数;删除第k个插入的数后面的数;在第k个插入的数后插入一个数。现在要对该链表进行M次操作,进行完所有操作后,从头到尾输出整个链表。注意:题目中第k个插入的数并不是指当......
  • 【数据结构】串
    串串的顺序实现简单的模式匹配算法KMP算法KMP算法的进一步优化串的顺序实现初始化#defineMaxSize50typedefcharElemType;//顺序存储表示typedefstruct{ElemTypedata[MaxSize];intlength;}SString;/***初始化串*/voidInitString(SString*string)......
  • 【数据结构】线性表
    线性表顺序表链式存储单链表双链表知识目录顺序表概念:用一组地址连续的存储单元依次存储线性表的数据元素,这种存储结构的线性表称为顺序表。特点:逻辑上相邻的数据元素,物理次序也是相邻的。只要确定好了存储线性表的起始位置,线性表中任一数据元素都可以随机存取,所以线性表的顺序存......
  • 【数据结构】栈、队列和数组
    栈、队列和数组栈队列数组数组的顺序表示和实现顺序表中查找和修改数组元素矩阵的压缩存储特殊矩阵稀疏矩阵栈初始化#defineMaxSize50//栈中元素的最大个数typedefcharElemType;//数据结构typedefstruct{inttop;//栈顶指针ElemTypedata[MaxSize];//存放栈中......
  • 深入理解算法与数据结构
    ......
  • Redis数据结构
    本文大部分知识整理自网上,在正文结束后都会附上参考地址。如果想要深入或者详细学习可以通过文末链接跳转学习。前言本文主要介绍关于Redis的五种基本数据结构的底层实现原理,然后来分析我们常用的使用场景。先简单回顾一下知识点。Redis是一个开源(BSD许可)的,内存中的数据结......
  • C++常见算法&数据结构模版
    各种常见算法&数据结构模板1.最长不下降子序列(LIS)1.1\(O(n^2)\)做法点击查看代码for(inti=1;i<=n;i++){ cin>>a[i]; dp[i]=1; } for(inti=1;i<=n;i++){ for(intj=1;j<i;j++){ if(a[i]>a[j]){ dp[i]=max(dp[i],dp[j]+......
  • Go每日一库之155:go-spew(输出 Go 数据结构)
    对于应用的调试,我们经常会使用fmt.Println来输出关键变量的数据。或者使用log库,将数据以log的形式输出。对于基础数据类型,上面两种方法都可以比较方便地满足需求。对于一些结构体类型数据,通常我们可以先将其序列化后再输出。如果结构体中包含不可序列化的字段,比如func类型......
  • Java语言通过三种方法来实现队列
    队列关于作者作者介绍......
  • 多重背包单调队列优化
    引用自:动态规划-背包问题(01背包、完全背包、多重背包)#include<cstdio>#include<algorithm>#include<cstring>usingnamespacestd;constintmaxn=100005;intn,m,cnt;intv[102],num[102],dp[maxn];structQueue{intpos,val;}q[maxn];intmain(){......