顺序表类模板:
//头文件
#ifndef HEADER_123_H
#define HEADER_123_H
#include <iostream>
using namespace std;
template <typename t>
class node {
private:
t* ptr;
int size;
int len = 0;
public:
node() : ptr(nullptr), size(0), len(0) {} //无参构造
node(t n,int s)//有参构造
{
ptr = new t[s];
for(int i=0;i<s;i++)
{
ptr[i] = n;
}
len = s;
size = s;
}
~node()// 析构函数
{
delete[] ptr;
}
void init(int n)//初始化
{
if (n <= 0)
{
return;
}
ptr = new t[n];
len = 0;
size = n;
}
bool empty()//判空
{
return len == 0;
}
bool full()// 判满
{
return len == size;
}
void push_back(t e)// 尾插
{
if (full())
{
cout << "顺序表已满,无法尾插" << endl;
return;
}
ptr[len++] = e;
}
void show()// 遍历输出
{
cout << "当前顺序表中的元素分别是:";
for (int i = 0; i < len; i++)
{
cout << ptr[i] << " ";
}
cout << endl;
}
void insert(int pow, t e)// 任意位置插入
{
if (full())
{
cout << "顺序表已满,无法插入" << endl;
return;
}
else if (pow < 1 || pow > len + 1)
{
cout << "插入位置错误" << endl;
return;
} else
{
for (int i = len; i >= pow - 1; i--)
{
ptr[i + 1] = ptr[i];
}
ptr[pow - 1] = e;
len++;
}
}
void pop_back()// 尾删
{
if (empty())
{
cout << "顺序表已空,无法删除" << endl;
return;
}
len--;
}
void erase(int pos)// 任意位置删除
{
if (empty())
{
cout << "顺序表已空,无法删除" << endl;
return;
} else if (pos < 1 || pos > len) {
cout << "删除位置错误" << endl;
return;
}
for (int i = pos - 1; i < len - 1; i++) {
ptr[i] = ptr[i + 1];
}
len--;
}
t at(int pow)// 获取任意位置元素
{
if (pow < 1 || pow > len)
{
cout << "获取位置错误" << endl;
return t();
}
return ptr[pow - 1];
}
void sort(bool flag)// 排序
{
for (int i = 1; i < len; i++)
{
for (int j = 0; j < len - i; j++)
{
if (flag)
{
if (ptr[j] > ptr[j + 1])
{
t temp = ptr[j];
ptr[j] = ptr[j + 1];
ptr[j + 1] = temp;
}
}
else
{
if (ptr[j] < ptr[j + 1])
{
t temp = ptr[j];
ptr[j] = ptr[j + 1];
ptr[j + 1] = temp;
}
}
}
}
}
};
#endif // HEADER_123_H
//主函数
#include "123.hpp"
int main()
{
node<int> s1(5,5);
s1.show();
if(s1.empty())
{
cout<<"s1为空"<<endl;
}
else
{
cout<<"s1不为空"<<endl;
}
if(s1.full())
{
cout<<"s1为满"<<endl;
}
else
{
cout<<"s1不为满"<<endl;
}
s1.pop_back();
s1.push_back(6);
s1.show();
s1.erase(4);
s1.show();
cout<<s1.at(4)<<endl;
s1.sort(0);
s1.show();
return 0;
}
栈的类模板:
//头文件
#ifndef HEADER_123_H
#define HEADER_123_H
#include <iostream>
using namespace std;
template <typename t>
class node
{
private:
t *ptr;
int max; //总长度
int top;//下标
public:
//无参构造
node():max(10)
{
this->top = -1;
this->ptr = new t[max];
}
//有参构造
node(int n,t s)
{
this->top = n-1;
this->max = this->top+1;
this->ptr = new t[this->max];
for(int i=0;i<n;i++)
{
ptr[i] = s;
}
}
//析构函数
~node()
{
delete [] ptr;
}
//拷贝赋值函数
node & operator=(const node &other)
{
if(this!=&other)
{
delete[] this->ptr;
this->top = other.top;
this->max = other.max;
this->ptr = new t[this->max];
for(int i=0;i<=this->top;i++)
{
this->ptr[i] = other.ptr[i];
}
}
return *this;
}
//拷贝构造函数
node(const node& other)
{
this->top = other.top;
this->max = other.max;
this->ptr = new t[this->max];
for(int i=0;i<=other.top;i++)
{
this->ptr[i] = other.ptr[i];
}
}
//访问第一个元素
t front()
{
if(empty())
{
cout<<"栈空,无法返回第一个元素"<<endl;
return t();
}
return this->ptr[0];
}
//访问最后一个元素
t back()
{
if(empty())
{
cout<<"栈空,无法返回最后一个元素"<<endl;
return t();
}
return this->ptr[top];
}
//判空
bool empty()
{
return this->top == -1;
}
//判满
bool full()
{
return this->top == this->max-1;
}
//返回容纳的元素数
int size()
{
return this->top+1;
}
//向栈尾部插入元素
void push(t a)
{
if (full())
{
cout << "栈已满,无法插入元素" << endl;
return;
}
this->top += 1;
this->ptr[this->top] = a;
}
//删除首元素
void pop()
{
if (empty())
{
cout << "栈空,无法删除首元素" << endl;
return;
}
this->ptr[0] = t();
for (int i = 1; i <= top; i++)
{
this->ptr[i-1] = this->ptr[i];
}
this->top -= 1;
}
//输出
void show()
{
for(int i=0;i<=top;i++)
{
cout<<ptr[i];
}
cout<<endl;
}
};
#endif
//主函数
#include "123.hpp"
using namespace std;
int main()
{
node <char>s1(5,'a');
s1.show();
node <char>s2(5,'c');
s1 = s2;
s1.show();
node <char> s3 = s1;
s3.show();
cout<<s3.front()<<endl;
cout<<s3.back()<<endl;
cout<<s3.size()<<endl;
s3.pop();
s3.show();
s3.push('b');
s3.show();
return 0;
}