基于自己实现顺序表实现带模板的栈和队列

队列

ArrayList是自己实现的顺序表

template<typename T>
class MyQueue
{
private:
	ArrayList<T> aq;
public:
	void enQueue(T e);
	T deQueue();
	T see();
	int getSize();
	T getVal(int index);
	friend ostream& operator <<(ostream& out, MyQueue<T>& q)
	{
		out << "tail:->";
		for (int i = q.getSize()-1; i >= 0; --i)
		{
			out << q.getVal(i) << "->";
		}
		out << ":head" << endl;
		return out;
	}
};

template <typename T>
void MyQueue<T>::enQueue(T e)
{
	aq.insert(e);
}

template <typename T>
T MyQueue<T>::deQueue()
{
	return aq.MyDeleteFirst();
}

template <typename T>
T MyQueue<T>::see()
{
	return aq.getFirst();
}

template <typename T>
int MyQueue<T>::getSize()
{
	return aq.getSize();
}

template <typename T>
T MyQueue<T>::getVal(int index)
{
	return aq.getVal(index);
}


template <typename T>
class MyStack
{
private:
	ArrayList<T> as;
public:
	bool isEmpty();
	void push(T e);
	void pop();
	T peek();
	int getSize();
	T getVal(int index);

	friend ostream& operator <<(ostream& out, MyStack<T>& s)
	{
		out << "tail:->";
		for (int i = s.getSize()-1; i >= 0; --i)
		{
			out << s.getVal(i) << "->";
		}
		out << ":head" << endl;
		return out;
	}
};

template <typename T>
bool MyStack<T>::isEmpty()
{
	return getSize() == 0;
}

template <typename T>
void MyStack<T>::push(T e)
{
	as.insert(e);
}

template <typename T>
void MyStack<T>::pop()
{
	as.MyDeleteLast();
}

template <typename T>
T MyStack<T>::peek()
{
	return as.getLast();
}

template <typename T>
int MyStack<T>::getSize()
{
	return as.getSize();
}

template <typename T>
T MyStack<T>::getVal(int index)
{
	return as.getVal(index);
}

你可能感兴趣的:(学习,数据结构,编程)