2024.9.4

devtools/2024/10/18 19:41:18/
#include <iostream>
#include <cstring>
using namespace std;template<typename T>
class Stack
{
private:int len;int count = 0;T *stack;
public:Stack():len(10)             //无参构造{stack = new T[len];stack[len] = {0};}Stack(int len):len(len)         //有参构造{stack = new T[len];stack[len] = {0};}Stack(Stack &other):len(other.len)  //拷贝构造函数{stack = new T[len];for(int i=0;i<len;i++){stack[i] = other.stack[i];}}Stack &operator=(const Stack &other)    //拷贝赋值函数{if(this != &other){this->count = other.count;this->len = other.len;int *newstack = new T[this->len];for(int i=0;i<len;i++){newstack[i] = other.stack[i];}delete [] stack;stack = newstack;}return *this;}~Stack()        //析构函数{delete [] stack;}T top();bool empty();int size();int push(T n);void expand();void show();int pop();
};
template<typename T>
T Stack<T>::top()
{if(empty()){cout<<"error" <<endl;return -1;}return stack[count-1];
}
template<typename T>
bool Stack<T>::empty()
{return count == 0;
}template<typename T>
int Stack<T>::size()
{return count;
}template<typename T>
int Stack<T>::push(T n)
{if(count == len){expand();}stack[count++] = n;return 0;
}template<typename T>
void Stack<T>::expand()
{len = len * 2;int *newstack = new T[len];for(int i=0;i<count;i++){newstack[i] = stack[i];}delete [] stack;stack = newstack;
}
template<typename T>
void Stack<T>::show()
{for(int i=count-1;i>=0;i--){cout<<stack[i]<<" ";}cout<<endl;
}template<typename T>
int Stack<T>::pop()
{if(empty()){cout<<"error"<<endl;return -1;}stack[--count] = 0;return 0;
}
template<typename K>
class Queue
{
private:int len;int count = 0;K *queue;
public:Queue():len(10){queue = new K[len];queue[len] = {0};};Queue(int n):len(n){queue = new K[len];queue[len] = {0};};Queue(Queue &other):len(other.len){queue = new K[len];for(int i=0;i<len;i++){queue[i] = other.queue[i];}};Queue &operator=(const Queue &other)    //拷贝赋值函数{if(this != &other){this->count = other.count;this->len = other.len;int *newqueue = new K[this->len];for(int i=0;i<len;i++){newqueue[i] = other.queue[i];}delete [] queue;queue = newqueue;}return *this;}~Queue(){delete []queue;};K front();K back();bool empty();int size();int push(K n);void pop();void expand();void show();
};
template<typename K>
K Queue<K>::front()
{if(empty()){cout<<"error"<<endl;return  -1;}return queue[count-1];
}
template<typename K>
K Queue<K>::back()
{if(empty()){cout<<"error"<<endl;return  -1;}return queue[0];
}
template<typename K>
bool Queue<K>::empty()
{return count == 0;
}
template<typename K>
int Queue<K>::size()
{return count;
}
template<typename K>
int Queue<K>::push(K n)
{if(count == len){expand();}queue[count++] = n;return 0;
}
template<typename K>
void Queue<K>::pop()
{for(int i=0;i<count;i++){queue[i] = queue[i+1];}count--;
}
template<typename K>
void Queue<K>::expand()
{len = len * 2;int *newqueue = new K[len];for(int i=0;i<count;i++){newqueue[i] = queue[i];}delete [] queue;queue = newqueue;
}
template<typename K>
void Queue<K>::show()
{for(int i=0;i<count;i++){cout<<queue[i]<<"  ";}cout<<endl;
}
int main()
{Stack<int> s1(3);s1.push(1);s1.push(2);s1.push(3);s1.push(3);s1.pop();Stack<int> s2;s2 = s1;s1.show();s2.show();cout<<s2.top()<<"   "<<s2.size()<<endl;cout<<"************"<<endl;Queue<int> q1(3);q1.push(1);q1.push(2);q1.push(3);q1.show();q1.pop();q1.show();return 0;
}


http://www.ppmy.cn/devtools/107389.html

相关文章

2021年大厂Java面试题(基础+框架+系统架构+分布式+实战)

Java线程的状态 进程和线程的区别&#xff0c;进程间如何通讯&#xff0c;线程间如何通讯 HashMap的数据结构是什么&#xff1f;如何实现的。和HashTable&#xff0c;ConcurrentHashMap的区别 Cookie和Session的区别 索引有什么用&#xff1f;如何建索引&#xff1f; Arra…

浅聊kubernetes RBAC

RBAC 基于角色&#xff08;Role&#xff09;的访问控制&#xff08;RBAC&#xff09;是一种基于组织中用户的角色来调节控制对计算机或网络资源的访问的方法。 RBAC 鉴权机制使用 rbac.authorization.k8s.io API 组来驱动鉴权决定&#xff0c; 允许你通过 Kubernetes API 动态…

GPT-4o mini轻量级大模型颠覆AI的未来

GPT-4o mini发布&#xff0c;轻量级大模型如何颠覆AI的未来&#xff1f; 引言 随着人工智能技术的飞速发展&#xff0c;大型AI模型的发布已成常态。然而&#xff0c;庞大的计算资源和存储空间限制了它们在广泛场景中的应用。为满足市场需求&#xff0c;轻量级大模型应运而生&a…

大语言模型的超参数含义: Top-P 采样; Top-P 采样;logit_bias:

目录 大语言模型的超参数含义 Top-P 采样 频率惩罚(Frequency Penalty) top_k: logit_bias: top_logprobs: max_tokens: 大语言模型的超参数含义 Top-P 采样 含义:一种采样替代方法,称为核采样。模型考虑top_p概率质量的token结果。例如,0.1表示仅考虑组成前10%…

云计算的成本:您需要了解的 AWS 定价信息

AWS 定价方案、免费套餐优惠以及通过预先预留容量来降低总体成本的选项。 欢迎来到雲闪世界。越来越多的企业开始转向云基础设施而非本地数据中心&#xff0c;云领域的竞争空前激烈。主要参与者甚至不惜削减成本并提供令人难以置信的折扣&#xff0c;以在云市场中占据一席之地。…

Docker笔记-启动容器时,时间与宿主机保持一致

背景 启动容器时&#xff0c;有时容器内部时区或时间与宿主机不一样&#xff0c;有些开源的软件使用起来会有问题&#xff0c;所以需要进行同步 更新宿主机时间 宿主机使用ntp服务&#xff0c;更新时间 yum install ntp ntpdate cn.pool.ntp.org date 时间与北京时间一样后…

计算机网络(三) —— 简单Udp网络程序

目录 一&#xff0c;初始化服务器 1.0 辅助文件 1.1 socket函数 1.2 填充sockaddr结构体 1.3 bind绑定函数 1.4 字符串IP和整数IP的转换 二&#xff0c;运行服务器 2.1 接收 2.2 处理 2.3 返回 三&#xff0c;客户端实现 3.1 UdpClient.cc 实现 3.2 Main.cc 实现 …

谈谈AI领域的认知误区、机会点与面临的挑战

谈谈AI领域的认知误区、机会点与面临的挑战 最近2年&#xff0c;AI 技术的火爆&#xff0c;到处都能看到大家在讨论AI 的发展与机会。这里我们讨论一下AI 认知的误区&#xff0c;机会点和面临的挑战。 by kimmking AI 认知的误区 这年头掀起了所有人讨论AI热潮的同时&#xf…