stack栈类
stack.h
#ifndef STACK_H
#define STACK_H
#include <iostream>
#include<string>
using namespace std;
//自行封装一个栈的类,包含私有成员属性:栈的数组、记录栈顶的变量
//成员函数完成:
//构造函数、析构函数、拷贝构造函数
//入栈、出栈、清空栈、判空、判满、获取栈顶元素、求栈的大小
template <class T>
class Mystack
{
private:
T *arr;
int top;
int size;
public:
//无参构造函数
Mystack()
{
this->size = 100;
this->arr = new T[size];
this->top = -1;
}
//有参构造函数
Mystack(int arrnum)
{
this->size = arrnum;
this->arr = new T[arrnum];
this->top = -1;
}
//拷贝构造函数
Mystack(const Mystack &stk)
{
this->size = stk.size;
cout<<this->size << " " << stk.size<<endl;
this->arr = new T[size];
for(int i = 0 ; i<stk.top ; i++)
{
*(arr+i) = *(stk.arr+i);
}
this->top = stk.top;
cout<<"1top:"<<this->top <<" " << stk.top<<endl;
}
//拷贝赋值函数
Mystack& operator=(const Mystack &stk)
{
this->size = stk.size;
this->arr = new T[size];
for(int i = 0 ; i<stk.top ; i++)
{
*(arr+i) = *(stk.arr+i);
}
this->top = stk.top;
return *this;
}
//析构函数
~Mystack()
{
delete arr;
}
int getTop() const;
//判空
bool empty();
//判满
bool full();
//入栈
void push(T elem);
//出栈
void pop();
//获取栈顶元素
T& topelem();
//求栈大小
int stksize();
};
#endif // STACK_H
main.cpp
#include "stack.cpp"
//int类型
void test()
{
cout<<"stk1:"<<endl;
Mystack<int> stk(100);
for(int i = 0 ; i<10;i++)
stk.push(i+1);
cout<<"stk的栈顶元素:"<<stk.topelem()<<endl;
cout<<"stk的大小:"<<stk.stksize()<<endl;
stk.pop();
cout<<"stk的栈顶元素:"<<stk.topelem()<<endl;
cout<<"stk的大小:"<<stk.stksize()<<endl;
cout<<"stk2:"<<endl;
Mystack<int> stk2(stk);
cout<<"stk2的栈顶元素:"<<stk2.topelem()<<endl;
cout<<"stk2的大小:"<<stk2.stksize()<<endl;
stk2.pop();
cout<<"stk2的栈顶元素:"<<stk2.topelem()<<endl;
cout<<"stk2的大小:"<<stk2.stksize()<<endl;
}
//string类型
void test2()
{
Mystack<string> stk(10);
stk.push("1");
stk.push("2");
stk.push("3");
stk.push("4");
stk.push("5");
stk.push("6");
cout<<stk.topelem()<<endl;
cout<<stk.stksize()<<endl;
stk.pop();
cout<<stk.topelem()<<endl;
cout<<stk.stksize()<<endl;
}
int main()
{
test();
//test2();
return 0;
}
queue.cpp
#include"stack.h"
template <class T>
int Mystack<T>::getTop() const
{
return top;
}
template <class T>
bool Mystack<T>::empty()
{
if(top == -1)
return true;
return false;
}
template <class T>
bool Mystack<T>::full()
{
if(top == size-1)
return true;
return false;
}
//入栈
template <class T>
void Mystack<T>::push(T elem)
{
if(full())
return;
top = top + 1;
this->arr[top] = elem;
}
//出栈
template <class T>
void Mystack<T>::pop()
{
if(empty())
return;
this->top--;
}
//获取栈顶元素
template <class T>
T& Mystack<T>::topelem()
{
return *(arr+top);
}
//求栈大小
template <class T>
int Mystack<T>::stksize()
{
return this->top+1;
}
queue队列
queue.h
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>
//自行封装一个循环顺序队列的类,包含私有成员属性:存放队列的数组、队头位置、队尾位置
//成员函数完成:构造函数、析构函数、拷贝构造函数、入队、出队、清空队列、判空、判满、求队列大小
using namespace std;
template <class T>
class Queue
{
private:
T *arr;
int head;
int tail;
int size;
public:
//无参构造
Queue();
//有参构造
Queue(int elemnum);
//拷贝构造
Queue(const Queue &q);
//拷贝赋值函数
void operator=(Queue &q);
//析构函数
~Queue();
//判空
bool empty();
//判满
bool full();
//入队
void push(T elem);
//出队
void pop();
//清空队列
void clear();
//求队列大小
int queuesize();
//返回最后一个元素
T* back();
//返回第一个元素
T* front();
};
#endif // QUEUE_H
main.cpp
#include"queue.cpp"
void test()
{
Queue<int> q1(20);
for(int i = 0;i < 10;i++)
{
q1.push(i+1);
}
cout<<"第一个元素: "<<*q1.front()<<endl;
cout<<"最后一个元素: "<<*q1.back()<<endl;
q1.pop();
cout<<"第一个元素: "<<*q1.front()<<endl;
cout<<"最后一个元素: "<<*q1.back()<<endl;
Queue<int> q2(q1);
cout<<"q2的第一个元素: "<<*q2.front()<<endl;
cout<<"q2的最后一个元素: "<<*q2.back()<<endl;
Queue<int> q3;
q3 = q1;
cout<<"q3的第一个元素: "<<*q3.front()<<endl;
cout<<"q3的最后一个元素: "<<*q3.back()<<endl;
}
int main()
{
test();
return 0;
}
queue.cpp
#include"queue.h"
//无参构造
template <class T>
Queue<T>::Queue():arr(new T[100]),head(0),tail(0),size(100)
{
}
//有参构造
template <class T>
Queue<T>::Queue(int elemnum):arr(new T[elemnum]),head(0),tail(0),size(elemnum)
{
}
//拷贝构造
template <class T>
Queue<T>::Queue(const Queue &q):arr(new T[q.size]),head(q.head),tail(q.tail),size(q.size)
{
int i = q.head;
while(i != q.tail)
{
*(this->arr+i) = *(q.arr+i);
i = (i+1+q.size)%q.size;
}
}
//析构函数
template <class T>
Queue<T>::~Queue()
{
delete arr;
}
//拷贝赋值函数
template <class T>
void Queue<T>::operator=(Queue &q)
{
arr = new T[q.size];
head = q.head;
tail = q.tail;
size = q.size;
int i = q.head;
while(i != q.tail)
{
*(this->arr+i) = *(q.arr+i);
i = (i+1+q.size)%q.size;
}
return *this;
}
//判空
template <class T>
bool Queue<T>::empty()
{
return head == tail;
}
//判满
template <class T>
bool Queue<T>::full()
{
return (tail+1)%size == head;
}
//入队
template <class T>
void Queue<T>::push(T elem)
{
if(full())
cout<<"队满"<<endl;
else
{
arr[tail] = elem;
tail=(tail+1)%size;
}
}
//出队
template <class T>
void Queue<T>::pop()
{
if(empty()){}
else
head=(head+1)%size;
}
//清空队列
template <class T>
void Queue<T>::clear()
{
head = 0;
tail = head;
}
//求队列大小
template <class T>
int Queue<T>::queuesize()
{
return (tail+size-head)%size;
}
//返回最后一个元素
template <class T>
T* Queue<T>::back()
{
if(empty())
{
T *elem = nullptr;
cout<<"空队列"<<endl;
return elem;
}
else
return arr+(tail+size-1)%size;
}
//返回第一个元素
template <class T>
T* Queue<T>::front()
{
if(empty())
{
T *elem = nullptr;
cout<<"空队列"<<endl;
return elem;
}
else
return arr+head;
}