自己实现栈和队列的全类型
代码:
/*******************************************/
文件名:sq.h
/*******************************************/
#ifndef SQ_H
#define SQ_H
#include <iostream>
#include <stdexcept>
using namespace std;
template <typename T>
class Mystack
{
private:
T *data;
size_t size;
size_t capacity;
void resize()
{
size_t newcapacity = 2*capacity;
T *temp=new T[newcapacity];
for(size_t i=0;i<size;i++)
{
temp[i]=data[i];
}
delete[]data;
data=temp;
capacity=newcapacity;
}
public:
Mystack():size(0),capacity(1){data=new T[capacity];}
~Mystack()
{
delete[]data;
}
Mystack &operator=(const Mystack &other) {
if (this != &other) {
delete[] data;
size = other.size;
capacity = other.capacity;
data = new T[capacity];
for (size_t i = 0; i < size; ++i) {
data[i] = other.data[i];
}
}
return *this;
}
bool empty()const
{
return size==0;
}
size_t getsize()const
{
return size;
}
T &top()
{
if (empty()) {
throw std::out_of_range("Stack<>::top(): empty stack");
}
return data[size - 1];
}
void push(const T &value)
{
if(size==capacity)
{
resize();
}
data[size++]=value;
}
void pop()
{
if(empty())
{
throw std::out_of_range("Stack<>::pop(): empty stack");
}
size--;
}
void show()
{
for(size_t i=0;i<size;i++)
{
cout<<data[i]<<" ";
}
cout<<endl;
}
};
template <typename T>
class Myqueue
{
private:
T *data;
int frontindex;
int rearindex;
size_t size;
size_t capacity;
void resize()
{
size_t newCapacity = 2 * capacity;
T *temp = new T[newCapacity];
for (size_t i = 0; i < size; i++) {
temp[i] = data[(frontindex + i) % capacity];
}
delete[] data;
data = temp;
frontindex = 0;
rearindex = size;
capacity = newCapacity;
}
public:
Myqueue():frontindex(0),rearindex(0),size(0),capacity(1){data=new T[capacity];}
~Myqueue()
{
delete[]data;
}
bool empty()const
{
return size==0;
}
size_t getsize()const
{
return size;
}
T &front()
{
if (empty()) {
throw std::out_of_range("Queue is empty");
}
return data[frontindex];
}
T &back()
{
if (empty()) {
throw std::out_of_range("Queue is empty");
}
return data[(rearindex - 1 + capacity) % capacity];
}
void push(const T &value)
{
if (size == capacity) {
resize();
}
data[rearindex] = value;
rearindex = (rearindex + 1) % capacity;
size++;
}
void pop()
{
if (empty()) {
throw std::out_of_range("Queue<>::pop(): empty queue");
}
frontindex = (frontindex + 1) % capacity;
size--;
}
void show()
{
for(size_t i=0;i<size;i++)
{
cout<< data[(frontindex + i) % capacity]<<" ";
}
cout<<endl;
}
};
#endif // SQ_H
/*******************************************/
文件名:main.cpp
/*******************************************/
#include"sq.h"
int main()
{
Mystack<int> s1;
Mystack<double> s2;
Mystack<char> s3;
Myqueue<int> q1;
Myqueue<double> q2;
Myqueue<char> q3;
s3.push('a');
s3.push('b');
s3.push('c');
cout<<"s3=";
s3.show();
cout<<"s3.size="<<s3.getsize()<<endl;
cout<<s3.top()<<endl;
Mystack<char> s4;
s4=s3;
cout<<"s4=";
s4.show();
s4.pop();
cout<<"s4=";
s4.show();
s1.push(1);
s1.push(2);
s1.push(3);
cout<<"s1=";
s1.show();
cout<<"s1.size="<<s1.getsize()<<endl;
cout<<s1.top()<<endl;
Mystack<int> s5;
s5=s1;
cout<<"s5=";
s5.show();
s5.pop();
cout<<"s5=";
s5.show();
s2.push(1.1);
s2.push(2.2);
s2.push(3.3);
cout<<"s2=";
s2.show();
cout<<s2.getsize()<<endl;
cout<<s2.top()<<endl;
Mystack<double> s6;
s6=s2;
cout<<"s6=";
s6.show();
s6.pop();
cout<<"s6=";
s6.show();
q1.push(4);
q1.push(5);
q1.push(6);
q2.push(4.4);
q2.push(5.5);
q2.push(6.6);
q3.push('d');
q3.push('e');
q3.push('f');
cout<<"q1=";
q1.show();
cout<<"q2=";
q2.show();
cout<<"q3=";
q3.show();
cout<<"q1.size="<<q1.getsize()<<endl;
cout<<"q2.size="<<q2.getsize()<<endl;
cout<<"q3.size="<<q3.getsize()<<endl;
cout<<q1.back()<<endl;
cout<<q1.front()<<endl;
cout<<q2.back()<<endl;
cout<<q2.front()<<endl;
cout<<q3.back()<<endl;
cout<<q3.front()<<endl;
Myqueue<int> q4;
Myqueue<double> q5;
Myqueue<char> q6;
q4=q1;
q5=q2;
q6=q3;
cout<<"q4=";
q4.show();
cout<<"q5=";
q5.show();
cout<<"q6=";
q6.show();
q4.pop();
q5.pop();
q6.pop();
cout<<"q4=";
q4.show();
cout<<"q5=";
q5.show();
cout<<"q6=";
q6.show();
return 0;
}