C++:采用模板封装顺序表,栈,队列

1.顺序表:

list.hpp

复制代码
#ifndef LIST_HPP
#define LIST_HPP
#include <iostream>

using namespace std;

template <class L>

class Seqlist
{
private:
    L *ptr;
    L size;
    L len=0;

public:
    void init(L n)
    {
        //堆区申请空间(大小为n)
        this->ptr=new L[n];
        //bzero(this->ptr,sizeof(L)*n);


        this->len=0;
        this->size=n;
        //
    }

    bool empty()
    {
        return this->len==0;
    }

    bool full()
    {
        return this->len==this->size;
    }

    void push_back(L e)//尾插
    {
        if(this->full())
        {
            return ;
        }
        this->ptr[len++]=e;
    }
    //插入
    void insert(L index,L num)
    {
        if(full())
        {
            return;
        }
        for(L i=len-1;i>=index-1;i--)
        {
            ptr[i+1]=ptr[i];
        }

        ptr[index-1]=num;
        len++;
    }
    //任意位置删除
    void erase(L index)
    {
        if(empty())
        {
            return;
        }
        for(L i=index;i<len;i++)
        {
            ptr[i-1]=ptr[i];
            ptr[i]=NULL;
        }
        len--;
    }
    //尾删
    void pop_back()
    {
        if(empty())
        {
            return;
        }
        ptr[len-1]=NULL;
        len--;

    }


    void show()
    {//判空
        if(len==0)
        {
            return;
        }

        cout<<"顺序表"<<endl;
        for(int i=0;i<len;i++)
        {
            cout<<ptr[i]<<ends;
        }

        cout<<endl;
    }
    //当前长度
    L cur_size()
    {
        if(empty())
        {
            return 0;
        }
        else
        {
            return len;
        }
    }

    L at(L index)
    {
        L num;
        num=ptr[index-1];
        return num;
    }

    void sort(bool flag)
    {
        if(flag)
        {
            for(int i=1;i<len;i++)
            {
                for(int j=0;j<len-i;j++)
                {
                    if(ptr[j]>ptr[j+1])
                    {
                        L temp;
                        temp=ptr[j];
                        ptr[j]=ptr[j+1];
                        ptr[j+1]=temp;
                    }
                }
            }
        }
        else
        {
            for(int i=1;i<len;i++)
            {
                for(int j=0;j<len-i;j++)
                {
                    if(ptr[j]<ptr[j+1])
                    {
                        L temp;
                        temp=ptr[j];
                        ptr[j]=ptr[j+1];
                        ptr[j+1]=temp;
                    }
                }
            }

        }
    }

};
#endif // LIST_HPP

2.栈:

strack.hpp

复制代码
#ifndef STACK_HPP
#define STACK_HPP

#include <iostream>
#include <exception>
using namespace std;

template <class T>
class StackNode
{
public:
    T data;//存储数据
    StackNode *next;//指向下一个节点

    //构造函数
    StackNode(T d):data(d),next(nullptr){}

};
template <class T>
class Stack
{
private:
    StackNode<T> *top;//指向栈顶
    int len;//栈中元素数量

public:

    Stack():top(nullptr),len(0){}
    //析构函数
    ~Stack()
    {
        while(!empty())
        {
            pop();
        }
    }

    bool empty() //判断栈是否为空
    {
        return top==nullptr;
    }

    int size()//获取栈的大小
    {
        return len;
    }
    //压栈操作


    void push(T element)
    {
        StackNode<T>  *newnode=new StackNode<T>(element);//申请空间并初始化
        newnode->next=top;
        top=newnode;
        len++;
    }
    //出栈操作
    T pop()
    {
        if(empty())
        {
            throw out_of_range("空栈");
        }
        StackNode<T>  *temp=top;
        T value=top->data;
        top=top->next;
        delete temp;
        len--;
        return value;
    }
    //查看栈顶元素
    T look_top()
    {
        if(empty())
        {
            throw out_of_range("空栈");
        }
        return top->data;
    }
    //清空栈
    void clear()
    {
        while (!empty())
        {
            pop();
        }
    }
    Stack& operator=(const Stack& other)
    {
        if (this != &other)
        {
            clear(); // 清空当前栈
            // 复制元素
            StackNode<T>  *current = other.top;
            while (current != nullptr)
            {
                push(current->data);
                current = current->next;
            }
        }
        return *this;
    }

    void swap(Stack& other)
    {
        StackNode<T> * tempTop = top;
        top = other.top;
        other.top = tempTop;

        T templen = len;
        len = other.len;
        other.len = templen;
    }
};

#endif // STACK_HPP

3.队列:

queue.hpp

复制代码
#ifndef QUEUE_H
#define QUEUE_H

#include <iostream>
#include <exception>
using namespace std;


template <class T>
class QueueNode
{
public:
    T data;
    QueueNode *next;

    //有参构造
    QueueNode(T d):data(d),next(nullptr){}
};
template <class T>
class Queue
{
private:
    QueueNode<T>* front; // 指向队列头部的指针
    QueueNode<T>* rear;  // 指向队列尾部的指针
    int count;        // 队列中元素的数量

public:

    // 构造函数
    Queue() : front(nullptr), rear(nullptr), count(0)
    {

    }

    ~Queue()
    {
        clear();
    }
    void clear()
    {
        while (front != nullptr)
        {
            QueueNode<T>* temp = front;
            front = front->next;
            delete temp;
        }
        rear = nullptr;
        count = 0;
    }

    // 查看队列前端元素
    T frontElement()
    {
        if (empty())
        {
            throw std::out_of_range("空队列");
        }
        return front->data;
    }
    // 查看队列尾部元素
    T backElement()
    {
        if (empty())
        {
            throw std::out_of_range("空队列");
        }
        return rear->data;
    }
    //判断队列是否为空
    bool empty()
    {
        return front == nullptr;
    }
    // 获取队列的大小
    int size()
    {
        return count;
    }

    // 入队操作
    void push(T element)
    {
        QueueNode<T>* newNode = new QueueNode<T>(element);
        if (rear == nullptr)  // 队列为空
        {
            front = rear = newNode;
        } else
        {
            rear->next = newNode;
            rear = newNode;
        }
        count++;
    }

    // 出队操作
    T pop()
    {
        if (empty()) {
            throw std::out_of_range("空队列");
        }
        QueueNode<T>* temp = front;
        T dequeuedValue = front->data;
        front = front->next;
        if (front == nullptr) {
            rear = nullptr;
        }
        delete temp;
        count--;
        return dequeuedValue;
    }

    void swap(Queue& other) {
        using std::swap;
        swap(front, other.front);
        swap(rear, other.rear);
        swap(count, other.count);
    }

    Queue& operator=(const Queue& other)
    {
            if (this != &other)
            {
                Queue temp(other); // 临时对象,用于深拷贝
                swap(temp); // 与临时对象交换内容
            }
            return *this;
        }

};
#endif // QUEUE_H
相关推荐
CoderYanger15 分钟前
递归、搜索与回溯-穷举vs暴搜vs深搜vs回溯vs剪枝:12.全排列
java·算法·leetcode·机器学习·深度优先·剪枝·1024程序员节
Slow菜鸟19 分钟前
Java开发规范(八)| 安全规范—企业级应用的“架构级底线”
java·开发语言·安全
憨憨崽&26 分钟前
进击大厂:程序员必须修炼的算法“内功”与思维体系
开发语言·数据结构·算法·链表·贪心算法·线性回归·动态规划
毕设源码-邱学长33 分钟前
【开题答辩全过程】以 基于Java的公职备考在线学习系统的设计与实现为例,包含答辩的问题和答案
java·开发语言·学习
水天需0101 小时前
C++ 三种指针转换深度解析
c++
二川bro1 小时前
模型部署实战:Python结合ONNX与TensorRT
开发语言·python
联系QQ:276998851 小时前
电化学与冷启动仿真的赝电容计算及GITT扩散系数研究——阻抗分析拟合与全电池电容器性能评估
开发语言
chem41111 小时前
C 语言 函数指针和函数指针数组
c语言·数据结构·算法
言言的底层世界2 小时前
c++中STL容器及算法等
开发语言·c++·经验分享·笔记
Mr_WangAndy2 小时前
C++17 新特性_第一章 C++17 语言特性___has_include,u8字符字面量
c++·c++40周年·c++17新特性·__has_include·u8字面量