C++杂项
作业:
将之前实现的顺序表、栈、队列都更改成模板类
顺序表
#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新特性之智能指针(重要)
原文地址:https://blog.csdn.net/m0_68379095/article/details/142602386
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!