版权声明: https://blog.csdn.net/qq_41880190/article/details/83503853
Stack.h
#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
//静态栈
template<class T>
class Stack()
{
public:
Stack()
:_a(NULL)
,_size(0)
,_capacity(0)
{}
void Push(const T& x)
{
CheckCapacity();
_a[_size++] = x;
}
void Pop()
{
assert(_size > 0);
--_size;
}
T& Top()
{
assert(_size > 0);
return _a[_size-1];
}
void CheckCapacity()
{
if(_size >= capacity)
{
T* tmp = new T[_capacity*2 + 3];
if(_a)
{
size_t i = 0;
for(i = 0; i<_size;++i)
{
tmp[i] = _a[i];
}
delete[] _a;
_a = tmp;
}
}
}
~Stack() {}
protected:
T* _a;
size_t _size;
size_t _capacity;
};
Queue.h
#pragma once
#include<iostream>
using namespace std;
#include<assert.h>
template<class T>
struct QueueNode
{
T _data;
QueueNode<T>* _next;
};
template<class T>
class Queue
{
typedef QueueNode<T>* Node;
public:
Queue()
:_head(NULL)
,_tail(NULL)
{}
void Push(const T& x)
{
if(-head == NULL)
{
return ;
}
else if(_head == _tail)
{
_head = _tail = NULL;
}
else
{
Node* next = _head->_naxt;
delete _head;
_head = next;
}
}
bool Empty()
{
return _head == NULL;
}
size_t Size()
{
size_t size = 0;
Node* cur = _head;
while(cur)
{
++size;
cur = cur->_next;
}
return size;
}
~Queue() {}
protected:
Node* _head;
Node* _tail;
};
//void TestQueue()
//{
//Queue<int> q;
//q.Push(1);
//q.Push(2);
//q.Push(3);
//q.Push(4);
//while(!q.Empty())
//{
//cout<<q.Front()<<" ";
//q.Pop();
//}
//cout<<endl;
//}