您的位置:首页 > 娱乐 > 明星 > 现在最靠谱的购物网站有哪些_常用的网站推广方法有哪些_如何创建网站教程_百度seo搜索营销新视角

现在最靠谱的购物网站有哪些_常用的网站推广方法有哪些_如何创建网站教程_百度seo搜索营销新视角

2024/10/5 18:34:15 来源:https://blog.csdn.net/m0_68379095/article/details/142602386  浏览:    关键词:现在最靠谱的购物网站有哪些_常用的网站推广方法有哪些_如何创建网站教程_百度seo搜索营销新视角
现在最靠谱的购物网站有哪些_常用的网站推广方法有哪些_如何创建网站教程_百度seo搜索营销新视角

作业:

将之前实现的顺序表、栈、队列都更改成模板类

顺序表

#include <iostream>using namespace std;template<typename T>class SeqList
{
private:T *ptr;int size;             //总长度int len = 0;          //当前顺序表实际长度public://初始化void init(int n){this->ptr = new T[n];this->len = 0;this->size = n;}//判空bool empty(){return this->len == 0;}//判满bool full(){return this->len == this->size;}//尾插void push_back(T e){//判断是否满了if(this->full()){return ;}this->ptr[len++] = e;}//插入void insert(T index){//判断是否满了if(this->full()){return ;}int i,pos;cout << "pos>>";cin >> pos;for(i=this->len; i>=pos; i--){this->ptr[i] = this->ptr[i-1];}this->ptr[i] = index;this->len++;}//任意位置删除void del(int index){if(empty() == 1){return;}int i;for(i = index - 1; i < this->len; i++){this->ptr[i] = this->ptr[i+1];}this->len--;}//尾删void pop_back(int index){if(empty() == 1){return;}int i;for(i = 0; i < index; i++){this->ptr[this->len--] = 0;}}//求长度void my_size(){cout << "len = " << this->len <<endl;}//获取任意位置元素T & at(int index){static T num;num = this->ptr[index-1];return num;}//将顺序表进行排序void sort(bool flag){int i,j;if(flag){for(i = 0; i < this->len; i++){for(j = 0; j < this->len - 1- i;j++){if(this->ptr[j]>this->ptr[j+1]){T temp = this->ptr[j];this->ptr[j] = this->ptr[j+1];this->ptr[j+1] = temp;}}}}else{for(i = 0; i < this->len; i++){for(j = 0; j < this->len - 1- i;j++){if(this->ptr[j]<this->ptr[j+1]){T temp = this->ptr[j];this->ptr[j] = this->ptr[j+1];this->ptr[j+1] = temp;}}}}}//定义展示函数void show(){//判空if(empty() == 1){return;}for(int i=0; i<this->len; i++){cout<<this->ptr[i]<<" ";}cout<<endl;}
};
int main()
{SeqList<int> s1;           //实例化一个顺序表对象s1.init(10);s1.show();s1.insert(8);s1.insert(7);s1.insert(9);s1.insert(2);s1.show();cout<<s1.at(2)<<endl;s1.del(1);s1.show();return 0;
}

运行结果:

#include <iostream>using namespace std;template<typename T>
class Stack  //定义一个栈的模板类
{
private:T *ptr;   //栈int num;  //栈顶元素数int max;  //栈的空间初始大小
public:Stack():num(-1),max(20){ptr = new T[max];   //申请空间}Stack(int t,int m):num(t),max(m){ptr = new T[max];   //申请栈空间for(int i=0;i<num+1;i++){cout<<"input>>";cin>>ptr[i];}}~Stack(){delete [] ptr;}Stack(Stack &s):ptr(new T(*s.ptr)),num(s.num),max(s.max){}//栈顶元素访问T top(){return ptr[num];}//判空bool empty(){return num==-1;}//元素数int size(){return num+1;}//向栈顶插入元素void push(T n){//判满if(num==max-1){cout<<"full"<<endl;return;}++this->num;this->ptr[num] = n;}//删除栈顶元素void pop(){//判空if(empty()){return;}this->num--;}void show(){for(int i=0;i<num+1;i++){cout<<ptr[i]<<"  ";}cout<<endl;}
};
int main()
{Stack <string> s1(4,10);   //有参构造s1.show();s1.pop();s1.show();string buff;cout<<"push>>";cin>>buff;s1.push(buff);s1.show();cout<<s1.size()<<"  "<<s1.top()<<endl;return 0;
}

运行结果:

队列

#include <iostream>
#include <stack>
using namespace std;
//队列模板类
template<typename T>
class Queue
{
private:int max;      //队列最大容量int num;      //队列内元素数T *ptr;     //容器
public:Queue():max(20),num(0){ptr = new T[this->max];}~Queue(){delete [] ptr;           //释放空间}Queue(Queue &q):max(q.max),num(q.num),ptr(new T(*q.ptr)){}    //深拷贝T front(){if(empty()){cout<<"null"<<endl;T a;return a;}return ptr[0];}T back(){if(empty()){cout<<"null"<<endl;T a;return a;}return ptr[num-1];}bool empty(){return num==0;}int size(){return num;}void push(T n){//判满if(num>=max){cout<<"full"<<endl;return;}this->num++;this->ptr[num-1] = n;}void pop(){//判空if(empty()){cout<<"null"<<endl;return;}for(int i=0;i<this->num;i++){this->ptr[i]=this->ptr[i+1];}this->num--;}void show(){for(int i=0;i<num;i++){cout<<ptr[i]<<"  ";}cout<<endl;}
};
int main()
{Queue<string> q;//判空if(q.empty()){q.push("hello");q.push("world");q.push("I");q.push("love");q.push("china");q.show();}cout<<q.back()<<endl;cout<<q.front()<<endl;cout<<q.size()<<endl;q.pop();q.show();return 0;
}

运行结果:

知识梳理:

C++11新特性之智能指针(重要)

版权声明:

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

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