将之前实现的顺序表、栈、队列都更改成模板类
顺序表:
list.hpp
#ifndef LIST_HPP
#define LIST_HPP
#include <iostream>
#include<memory.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
//typedef int T; //类型重命名
template <typename T>
//封装一个顺序表
class SeqList
{
private:
T *ptr; //指向堆区空间的起始地址
int size; //总长度
int len = 0; //当前顺序表实际长度
public:
void init(int n)
{
//在堆区申请出一个长度为n的空间,将其实地址赋值给ptr
this->ptr = new T[n];
//给len进行初始化
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,m;
cout << "\n请输入要插入的位置:";
cin >> m;
//cout << "\n请输入要插入的数值:";
//cin >> index;
for(i=this->len; i>=m; i--)
{
this->ptr[i] = this->ptr[i-1];
}
this->ptr[i] = index;
this->len++;
}
//任意位置删除
void erase(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 << "长度为:" << this->len <<endl;
}
//获取任意位置元素
T & my_at(int index)
{
static T num;
num = this->ptr[index-1];
return num;
}
//将顺序表进行排序
void my_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;
}
cout<<"当前顺序表中的元素分别是:";
for(int i=0; i<this->len; i++)
{
cout<<this->ptr[i]<<" ";
}
cout<<endl;
}
};
#endif // LIST_HPP
main.cpp
#include "list.hpp"
int main()
{
SeqList<int> sl; //实例化一个顺序表对象
sl.init(10); //申请空间
sl.push_back(1);
sl.push_back(2);
sl.push_back(3);
sl.push_back(4);
sl.push_back(5);
sl.push_back(6);
sl.show();
sl.insert(7);
sl.show();
sl.erase(3);
sl.show();
sl.pop_back(2);
sl.show();
sl.my_size();
cout << sl.my_at(3) << endl;
sl.my_sort(1);
sl.show();
sl.my_sort(0);
sl.show();
return 0;
}
栈:
list.hpp
#ifndef LIST_HPP
#define LIST_HPP
#include <iostream>
using namespace std;
template <typename T>
class my_stack {
private:
T *data; // 存储栈元素的动态数组
int capacity; // 栈的容量
int top; // 栈顶元素的索引
public:
// 构造函数
my_stack(int size = 10) : capacity(size), top(-1)
{
data = new T[capacity];
}
// 析构函数
~my_stack()
{
delete[] data;
}
// 入栈操作
void push(const T& value)
{
if (top + 1 >= capacity)
{
resize();
}
data[++top] = value;
}
// 出栈操作
void pop()
{
if (!empty())
{
--top;
}
else
{
cout << "Stack is empty. Cannot pop." << endl;
}
}
// 查看栈顶元素
T& peek()
{
if (!empty())
{
return data[top];
}
else
{
throw out_of_range("Stack is empty. Cannot peek.");
}
}
// 检查栈是否为空
bool empty() const
{
return top == -1;
}
// 获取栈的大小
int size() const
{
return top + 1;
}
private:
// 扩容函数
void resize()
{
capacity *= 2;
T* newData = new T[capacity];
for (int i = 0; i <= top; ++i)
{
newData[i] = data[i];
}
delete[] data;
data = newData;
}
};
#endif // LIST_HPP
main.cpp
#include "list.hpp"
int main()
{
my_stack<int> stack;
// 入栈操作
stack.push(10);
stack.push(20);
stack.push(30);
cout << "栈顶元素:"<< stack.peek() << endl; // 输出: 30
cout << "栈大小:" << stack.size() << endl; // 输出: 3
// 出栈操作
stack.pop();
cout << "弹出操作后的顶部元素:" << stack.peek() << endl; // 输出: 20
// 检查栈是否为空
stack.pop();
stack.pop();
cout << "栈是否为空 " << (stack.empty() ? "是" : "否") << endl;
return 0;
}
队列:
list.hpp
#ifndef LIST_HPP
#define LIST_HPP
#include <iostream>
#include <stdexcept>
using namespace std;
template <typename T>
class my_queue {
private:
T* data; // 存储队列元素的动态数组
int capacity; // 队列的容量
int front; // 队头元素的索引
int rear; // 队尾元素的索引
int count; // 当前元素数量
public:
// 构造函数
my_queue(int size = 10) : capacity(size), front(0), rear(-1), count(0)
{
data = new T[capacity];
}
// 拷贝构造函数
my_queue(const my_queue& other)
{
capacity = other.capacity;
front = other.front;
rear = other.rear;
count = other.count;
data = new T[capacity];
for (int i = 0; i < count; ++i)
{
data[(front + i) % capacity] = other.data[(front + i) % capacity];
}
}
// 拷贝赋值运算符
my_queue& operator=(const my_queue& other)
{
if (this != &other)
{
delete[] data; // 释放旧内存
capacity = other.capacity;
front = other.front;
rear = other.rear;
count = other.count;
data = new T[capacity];
for (int i = 0; i < count; ++i)
{
data[(front + i) % capacity] = other.data[(front + i) % capacity];
}
}
return *this;
}
// 析构函数
~my_queue()
{
delete[] data;
}
// 返回队头元素
T &my_front()
{
if (empty())
{
throw out_of_range("队列为空,无法查看队头元素。");
}
return data[front];
}
// 返回队尾元素
T& back()
{
if (empty())
{
throw out_of_range("队列为空,无法查看队尾元素。");
}
return data[rear];
}
// 检查队列是否为空
bool empty() const
{
return count == 0;
}
// 获取队列的大小
int size() const
{
return count;
}
// 入队操作
void push(const T& value)
{
if (count >= capacity)
{
resize();
}
rear = (rear + 1) % capacity; // 循环队列
data[rear] = value;
count++;
}
// 直接构造元素(emplace)
template <typename... Args>
void emplace(Args&&... args)
{
if (count >= capacity)
{
resize();
}
rear = (rear + 1) % capacity; // 循环队列
data[rear] = T(std::forward<Args>(args)...); // 使用完美转发构造元素
count++;
}
// 出队操作
void pop() {
if (empty())
{
cout << "队列为空,无法出队。" << endl;
return;
}
front = (front + 1) % capacity; // 循环队列
count--;
}
// 交换两个队列
void swap(my_queue& other)
{
std::swap(data, other.data);
std::swap(capacity, other.capacity);
std::swap(front, other.front);
std::swap(rear, other.rear);
std::swap(count, other.count);
}
private:
// 扩容函数
void resize()
{
capacity *= 2;
T* newData = new T[capacity];
for (int i = 0; i < count; ++i)
{
newData[i] = data[(front + i) % capacity]; // 复制元素
}
delete[] data;
data = newData;
front = 0; // 重置队头索引
rear = count - 1; // 更新队尾索引
}
};
#endif // LIST_HPP
main.cpp
#include "list.hpp"
int main()
{
my_queue<int> queue;
// 入队操作
queue.push(10);
queue.push(20);
queue.push(30);
cout << "队头元素:" << queue.my_front() << endl; // 输出: 10
cout << "队尾元素:" << queue.back() << endl; // 输出: 30
cout << "队列大小:" << queue.size() << endl; // 输出: 3
// 出队操作
queue.pop();
cout << "出队操作后的队头元素:" << queue.my_front() << endl; // 输出: 20
// 检查队列是否为空
queue.pop();
queue.pop();
cout << "队列是否为空 " << (queue.empty() ? "是" : "否") << endl; // 输出: 是
return 0;
}