您的位置:首页 > 教育 > 锐评 > 字节跳动公司简介_dw怎么把代码做成网页_百度快照收录_seo排名优化方式方法

字节跳动公司简介_dw怎么把代码做成网页_百度快照收录_seo排名优化方式方法

2024/10/5 3:25:01 来源:https://blog.csdn.net/fantasy_time_/article/details/142601443  浏览:    关键词:字节跳动公司简介_dw怎么把代码做成网页_百度快照收录_seo排名优化方式方法
字节跳动公司简介_dw怎么把代码做成网页_百度快照收录_seo排名优化方式方法

1、顺序表、栈、队列都更改成模板类

(1)顺序表

#include <iostream>
#include <cstring>using namespace std;template <typename T1,typename T2,typename T3>
class My_string
{
private:T1 *ptr;         //指向字符数组的指针T2 size;           //字符串的最大容量T3 len;            //字符串当前容量public://无参构造My_string():size(15){this->ptr = new char[size];this->ptr[0] = '\0';            //表示串为空串this->len = 0;}//有参构造My_string(const T1 src){len = strlen(src);size = len+1;ptr = new char[size];strcpy(ptr,src);}My_string(T2 num, T1 value): size(num+1),len(num){ptr = new char[size];for(int i=0;i<num;i++){ptr[i] = value;}ptr[num] = '\0';}//拷贝构造My_string(const T1 &other):size(other.size),len(other.len){ptr = new char[size];strcpy(ptr,other.ptr);}//拷贝赋值template <typename T>My_string & operator =(const T &other){if(this ==&other){return *this;}delete [] ptr;size = other.size;len = other.len;ptr = new char[size];strcpy(ptr,other.ptr);return *this;}//析构函数~My_string(){delete [] ptr;}//判空bool  empty(){return len ==0;}//尾插template <typename T>void  push_back(T value){if(len+1>=size){size *=2;char* new_ptr = new char[size];strcpy(new_ptr,ptr);delete []ptr;ptr = new_ptr;}ptr[len] = value;len++;ptr[len] = '\0';}//尾删void  pop_back(){if(len>0){len--;ptr[len]='\0';}}//at函数实现template <typename T>char &at(T index){if(index<0 ||index>=len){//throw  out_of_range("超出");}return ptr[index];}//清空函数void  clear(){len = 0;ptr[0] = '\0';}//返回C风格字符串char* data(){return ptr;}//返回实际长度int  get_length(){return len;}//返回当前最大容量int  get_size(){return size;}void show(){cout<<ptr<<" ";}};int main()
{My_string <char,int,int>s1;s1.push_back<char>('d');s1.show();}

(2)栈

#include <iostream>using namespace std;template <typename T1,typename T2,typename T3>
class My_stack
{
private:T1 *ptr;T2 size;T3 top;public:My_stack():size(20),top(-1){ptr = new char[size];}My_stack(int num){size = num;ptr = new  char[num];top = -1;}template <typename T>My_stack(const T &other){top = other.top;size = other.size;ptr = new char[size];for(int i=0;i<size;i++){ptr[i] = other.ptr[i];}}template <typename T>My_stack &operator=(const T &other){if(this==&other){return *this;}delete [] ptr;top = other.top;size = other.size;ptr = new char[size];for(int i=0;i<size;i++){ptr[i] = other.ptr[i];}return *this;}char My_top(){return ptr[top];}void My_empty(){if(top==-1){cout<<"栈为空"<<endl;}else{cout<<"栈不为空"<<endl;}}int My_size(){return top+1;}template <typename T>void My_push(const T dat){if(top+1==size){cout<<"栈满不能继续添加"<<endl;}else{ptr[++top] = dat;}}void My_pop(){ptr[top--] = 0;}};int main()
{My_stack <char,int,int>my_stack;my_stack.My_push('1');my_stack.My_push('2');my_stack.My_push('3');cout << "Top: " << my_stack.My_top() <<endl;my_stack.My_pop();my_stack.My_empty();cout << "Size: " << my_stack.My_size() <<endl;return 0;
}

(3)队列

#include <iostream>using namespace std;#include <iostream>
#include <stdexcept>template<typename T1,typename T2,typename T3,typename T4>
class My_queue
{
private:T1 *ptr;T2 size;T3 back;T4 fro;public:My_queue():size(20),back(0),fro(0){ptr = new char[size];}template<typename T>My_queue(T num){size = num;ptr = new  char[num];back = 0;fro = 0;}~My_queue(){delete [] ptr;}template<typename T>My_queue(const T &other){fro = other.fro;back = other.back;size = other.size;ptr = new char[size];for(int i=0;i<size;i++){ptr[i] = other.ptr[i];}}template<typename T>My_queue &operator=(const T &other){if(this==&other){return *this;}delete [] ptr;fro = other.fro;back = other.back;size = other.size;ptr = new char[size];for(int i=0;i<size;i++){ptr[i] = other.ptr[i];}return *this;}char My_front(){return ptr[fro];}char My_back(){return ptr[back-1];}bool My_empty(){return fro == back;}int My_size(){return back - fro;}template<typename T>void My_push(const T dat){if(back+1==size){cout<<"栈满不能继续添加"<<endl;}else{ptr[back++] = dat;}}void My_pop(){ptr[fro++] = 0;}};int main()
{My_queue <char,int,int,int> my_queue;my_queue.My_push('c');my_queue.My_push('2');cout << "Front: " << my_queue.My_front() <<endl; // 输出 1cout<<"back:"<<my_queue.My_back()<<endl;cout << "Size: " << my_queue.My_size() << endl; // 输出 1return 0;
}

2、思维导图

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com