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

news2024/9/28 5:05:14

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.coloradmin.cn/o/2172376.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

饿了么 ui表单 有滚动条的时候 右上角多一节

// 当没有滚动条的时候 :deep(.el-table__body-wrapper.is-scrolling-none~.el-table__fixed-right) {right: 0px !important;}// 当有滚动条的时候 默认偏移距离:deep(.el-table--scrollable-y .el-table__fixed-right) {right: 13px !important;}修改完 不显示滚动条

localhost 自动被 redirect 到 https 地址的问题

不知道为什么, 前端项目启动以后自动将 http://localhost 重定向到了 https://localhost, 我并没有添加任何 hsts 的中间件, 所以并不是这个原因, 而且代码之前是好使的, 但是由于我安装了某个证书后, 导致出现了这个问题。 在edge浏览器中输入edge://net-internals/#hsts 或是…

【React】自定义hook函数

1. 概念 本质&#xff1a;函数 2. 例子 需求&#xff1a;实现点击按钮的展示与隐藏子组件 2.1 不封装直接实现 import { useState } from react function Son() {return <div>子组件</div> }function App() {const [isShow, setIsShow] useState(true)funct…

虚拟环境默认安装到C盘的修改办法

问题&#xff1a; 创建的虚拟环境默认安装到了C盘。 将路径改成D盘下。 解决办法&#xff1a; 我是按照博客w11下载anaconda在d盘&#xff0c;新建的虚拟环境总是在c盘怎么解决_如何保证anaconda的全在e盘-CSDN博客 中的方法1解决的。 用记事本打开.condarc文档&#xff0…

前端学习笔记-JS进阶篇-01

作用域&解构&箭头函数 1、作用域 作用域&#xff08;scope&#xff09;规定了变量能够被访问的“范围”&#xff0c;离开了这个“范围”变量便不能被访问 1.1、局部作用域 局部作用域分为函数作用域和块作用域 1.1.1、函数作用域 在函数内部声明的变量只能在函数…

OpenCV 进行图像分割

介绍 图像分割是将数字图像划分互不相交的区域的过程,它可以降低图像的复杂性,从而使分析图像变得更简单。 图像分割技术 阈值法 基于边缘的分割 基于区域的分割 基于聚类的分割 基于分水岭的方法 基于人工神经网络的分割 在这里,我们选择基于聚类的分割 与分类算法不同,…

gitlab使用小结

GitLab 是一个基于 Git 的代码托管平台&#xff0c;提供了丰富的功能来管理代码仓库、CI/CD、项目管理等。以下是一些常用的 GitLab 命令和示例&#xff0c;帮助你更好地使用 GitLab。 1、 克隆仓库 克隆一个远程仓库到本地&#xff1a; git clone gitgitlab.example.com:us…

SSH连接提示秘钥无效

说明&#xff1a;本文记录一次使用SSH连接服务器失效的问题。 使用SSH命令连接服务器&#xff0c; ssh -i ssh秘钥路径 user192.xx.xx.xx提示下面的错误&#xff1b; Load key "shuhe.bin": invalid format aochuang192.xx.xx.xx: Permission denied (publickey,g…

cocos打包后发布web,控制台报错.plist资源下载404

web加载报错 download failed: assets/main/native/0a/0a1a5e41-7d91-4a5d-9552-2c10e5fc5867.plist, status: 404&#xff0c; 应该是MIME属性没有设置允许下载.plist后缀的文件。 对于linux应该改nginx或apache&#xff0c;允许下载该类文件。 我部署在了windows服务器上&am…

Mybatis(进阶部分)

四 Mybatis完成CURD&#xff08;二&#xff09; 4.5 多条件CRUD 之前的案例中&#xff0c;接口里方法的形参个数都是1个&#xff1b;如果方法形参是两个或者两个以上时&#xff0c;MyBatis又该如何获取获取参数呢&#xff1f; Mybatis提供了好几种方式&#xff0c;可以获取多…

SpringMVC5-域对象共享数据

目录 使用ServletAPI向request域对象共享数据 使用ModelAndView向request域对象共享数据 使用Model向request域对象共享数据 使用map向request域对象共享数据 使用ModelMap向request域对象共享数据 Model、ModelMap、Map的关系 向session域共享数据 向application域共享…

【TabBar嵌套Navigation案例-新特性页面-基本框架 Objective-C语言】

一、我们来说这个示例程序里边的这个背景图片 1.首先呢,这个里边呢,我们这个新特性页面, 整个儿,是一个CollectionViewController,然后,我们做一下,先来做一下CollectionViewControlle,然后,我们把这个背景图片,先加上去, 这个时候,我要先创建一个新特性页面的模块…

开发提效的工具tabby快速入门

1.什么是tabby&#xff1f; Tabby is an open-source, self-hosted AI coding assistant. With Tabby, every team can set up its own LLM-powered code completion server with ease. 官方网站&#xff1a;https://tabby.tabbyml.com/ 2.tabby服务安装(Hugging Face Spaces…

28 - 移除元素

解答代码&#xff1a; int removeElement(int* nums, int numsSize, int val) {int j 0;for (int i 0; i < numsSize; i){if (nums[i] ! val){nums[j] nums[i];j;}}return j;}

机器学习笔记(李宏毅老师2021/2022课程)【更新中】

目录 前言 课程预览 第一讲 机器学习基本概念 前言 本文主要记录在听李宏毅老师的课时对应做的课堂笔记 课程&#xff1a; (强推)李宏毅2021/2022春机器学习课程_哔哩哔哩_bilibili 课程预览 机器学习找函数 &#xff08;找一个人类写不出来的复杂函数&#xff09; 课程侧…

代码随想录Day17 图论-2

103. 水流问题 本题思路很简单 要求我们找到可以满足到达两个边界的单元格的坐标 有一个优化的思路就是 我们从边界的节点向中间遍历 然后用两个数组表示 一个是第一组边界的数组 一个是第二边界的数组 如果两个数组都遍历到了某一个单元格 就说明该单元格时满足题目要求的 #…

【Linux笔记】在VMware中,为基于NAT模式运行的CentOS虚拟机设置固定的网络IP地址

一、配置VMware虚拟网络 1、打开VMware虚拟网络编辑器&#xff1a; 点击VMware主界面上方的“编辑”菜单&#xff0c;选择“虚拟网络编辑器”。 2、选择NAT模式网络&#xff1a; 在虚拟网络编辑器中&#xff0c;选择VMnet8&#xff08;或其他NAT模式的网络&#xff09;。 取消勾…

ubuntu18.04 NVIDIA驱动 CUDA cudnn Anaconda安装

1、安装NVIDIA驱动 a.查看推荐驱动 ubuntu-drivers devicesb.打开软件更新&#xff0c;选择相应的显卡 c.重启查看安装情况&#xff0c;输入nvidia-smi 2、安装CUDA 下载链接https://developer.nvidia.com/cuda-toolkit-archive 安装CUDA&#xff1a; sudo bash cuda_11…

ECharts图表图例3

java 用ecplise软件 可视化图表 代码&#xff1a; <! DOCTYPE html > < html > < head > < meta charset " UTF -8"> <1--引入 ECharts 脚本--> < script src " js / echarts . js "></ script > <…

力扣(leetcode)每日一题 2516 每种字符至少取 K 个 | 滑动窗口

2516. 每种字符至少取 K 个 给你一个由字符 a、b、c 组成的字符串 s 和一个非负整数 k 。每分钟&#xff0c;你可以选择取走 s 最左侧 还是 最右侧 的那个字符。 你必须取走每种字符 至少 k 个&#xff0c;返回需要的 最少 分钟数&#xff1b;如果无法取到&#xff0c;则返回…