栈的模拟实现
#pragma once
#include<iostream>
using namespace std;
//顺序栈
template<class T>
class SeqStack {
public:
SeqStack(size_t capacity = 10)//用户自行提供容量,如果用户不提供使用默认容量
:_array(new T[sizeof(T) * capacity])
, _capacity(capacity)
, _top(0)
{}
//扩容
void change_capacity() {
size_t new_capacity = _capacity;
T* temp = new T[sizeof(T) * new_capacity];
memmove(temp, _array,sizeof(T)*_capacity);
delete[] _array;
_array = temp;
_capacity = new_capacity;
}
//压栈
void push(const T& data) {
//如果栈已满的情况下进行扩容
if (_top == _capacity) {
change_capacity();
}
//插入元素
_array[_top] = data;
_top++;
}
//出栈,只需要让数组访问不到最后一个元素即可,改变size即可
void pop() {
if (_top == 0)
return;
_top--;
}
//返回栈顶元素
T& top() {
assert(_top != 0);
return _array[_top-1];
}
//判满
bool ISfull() {
return _top == _capacity;
}
//判空
bool Isempty() {
return _top == 0;
}
//析构函数,回收申请的空间
~SeqStack()
{
delete[] _array;
_capacity = _top = 0;
}
private:
T* _array;
size_t _capacity;
size_t _top;
};
//定义链表节点
template<class T>
struct ListStackNode {
ListStackNode(const T& data = T())
:_data(data)
,next(nullptr)
{}
T _data;
ListStackNode<T>* next;
};
//链栈
template<class T>
class ListStack {
public:
typedef ListStackNode<T> Node;
public:
//构造函数
ListStack()
:_head(nullptr)
,_size(0)
{}
//压栈
void push(const T& data) {
Node* newnode = new Node(data);
if (_size == 0)
_head = newnode;
else {
newnode->next = _head;
_head = newnode;
}
_size++;
}
//出栈
void pop() {
if (_size == 0)
return;
Node* delnode = _head;
_head = _head->next;
delete delnode;
delnode = nullptr;
}
//返回栈顶元素
void front() {
if (_size == 0)
return;
return _head->_data;
}
//判空
//链栈不需要扩容,只需要申请空间即可
bool Isempty() {
return _size == 0;
}
private:
Node* _head;
size_t _size;
};
队列的模拟实现
#pragma once
#include<iostream>
using namespace std;
template<class T>
//顺序队列
class SeqQueue {
public:
SeqQueue(const size_t& capacity=8)
:_array(new T[sizeof(T)*capacity])
,_capacity(capacity)
,_size(0)
{}
bool Isfull() {
return _size == _capacity;
}
bool ISempty() {
return _size == 0;
}
void change_capacity() {
size_t new_capacity = _capacity;
T* temp = new T[sizeof(T) * new_capacity];
memmove(temp, _array, sizeof(T) * _capacity);
delete[] _array;
_array = temp;
_capacity = new_capacity;
}
void push(const T& data) {
if (Isfull())
change_capacity();
_array[_size] = data;
_size++;
}
void pop() {
if (ISempty())
return;
for (int i = 0; i < _size; i++) {
_array[i] = _array[i + 1];
}
_size--;
}
T& front() {
if (_size != 0)
return _array[0];
}
~SeqQueue()
{
delete[] _array;
_capacity = _size = 0;
_array = nullptr;
}
private:
T* _array;
size_t _capacity;
size_t _size;
};
template<class T>
struct QueueNode {
QueueNode(const T& data=T())
:_data(data)
, next(nullptr)
{}
T _data;
QueueNode<T>* next;
};
template<class T>
class LinkQueue {
public:
typedef QueueNode<T> Node;
public:
LinkQueue()
:_head(nullptr)
,_size(0)
{}
bool Is_empty() {
return _size == 0;
}
Node* tail_queue() {
Node* cur = _head;
while (cur->next)
cur = cur->next;
return cur;
}
void push(const T& val){
Node* newnode = new Node(val);
if (Is_empty())
_head = newnode;
else {
Node* tail = tail_queue();
tail->next = newnode;
}
_size++;
}
void pop() {
Node* cur = _head;
_head = _head->next;
delete cur;
cur = nullptr;
}
T& Front() {
if (_size != 0)
return _head->_data;
}
~LinkQueue()
{
while (_head != nullptr) {
Node* cur = _head;
_head = _head->next;
delete cur;
cur = nullptr;
}
_size = 0;
}
private:
Node* _head;
size_t _size;
};