当前位置: 首页 > article >正文

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


http://www.kler.cn/news/329771.html

相关文章:

  • 秋招内推2025--招联金融
  • 【MySQL】聚合函数、group by子句
  • Vue 常用的指令用法
  • “大数据+高职”:VR虚拟仿真实训室的发展前景
  • 2、Objects类(为什么重写 equals方法必须重写 hashCode方法)、无序性+随机性+不可重复性的区别
  • Android Studio 占满C盘快速解决方法
  • Matlab|考虑阶梯式碳交易与供需灵活双响应的综合能源系统优化调度
  • 《向量数据库指南》——非结构化数据挑战升级:如何高效导入向量数据库?
  • Android 开发每日定时任务
  • Elasticsearch 使用误区之六——富文本内容写入前不清洗
  • Host文件及switchhosts for mac下载
  • vue3打包疯狂报错
  • 域内用户名枚举 实验
  • 计算机网络的整体认识---网络协议,网络传输过程
  • 媒体专访 | 探寻国家级农业产业化重点龙头企业九三食品的数字化转型破局之路
  • 如何让虚拟机与本地电脑使用同一个ip
  • 基于安卓开发大型体育场管理系统的设计与实现(源码+定制+讲解)
  • 服务器虚拟化软件介绍
  • 【C++掌中宝】从std的角度来进一步了解命名空间
  • AndroidStudio导入so文件
  • 简单理解程序地址空间:Linux 中的内存映射与页表解析
  • 基于单片机语音智能导盲仪仿真设计
  • 动态顺序表的增删改查(数据结构)
  • windows 桌面采集音频
  • SpringBoot启动过程简述 和 SpringCloud 的五大组键
  • Vue下载pubsub-js中错误问题解决
  • Unity角色控制及Animator动画切换如走跑跳攻击全流程详解
  • 爬取元气手机壁纸简单案例(仅用于教学,禁止任何非法获利)
  • Activiti7 工作流引擎学习
  • 【文心智能体 | AI大师工坊】如何使用智能体插件,完成一款旅游类智能体的开发,来体验一下我的智能体『​​​​​​​厦门CityWalk』