#ifndef QUEUE_H_
#define QUEUE_H_
#include <iostream>
#include "stack.h"
template <class T>
class Queue {
private:
//TODO
Node<T>* front;
Node<T>* back;
int length;
void copy(Node<T>* this_, Node<T>* other_);
void remove(Node<T>* n);
public:
Queue();
Queue(Queue& other);
~Queue();
Queue& operator=(Queue& other);
int get_size() const;
void push(T* t);
void push(Node<T>* n);
Node<T>* pop();
void print();
};
#include "queue.cpp"
#endif
#include "queue.h"
#include <iostream>
template <class T>
Queue<T>::Queue()
{
// TODO
front = back = NULL;
length = 0;
}
template <class T>
Queue<T>::Queue(Queue& other)
{
// TODO
if(other.length==0||this==&other)
return;
length = other.length;
if (other.front == NULL)
{
front = new Node<T>();
}
else
{
front = new Node<T>(*(other.front));
copy(this->front, other.front);
}
if (other.back == NULL)
{
back = new Node<T>();
}
else
{
back = new Node<T>(*(other.back));
copy(this->back, other.back);
}
}
template <class T>
Queue<T>::~Queue()
{
// TODO
remove(front);
remove(back);
}
template <class T>
Queue<T>& Queue<T>::operator=(Queue& other)
{
// TODO
if(other.length==0||this==&other)
return;
length = other.length;
if (other.front == NULL)
{
front = new Node<T>();
}
else
{
front = new Node<T>(*(other.front));
copy(this->front, other.front);
}
if (other.back == NULL)
{
back = new Node<T>();
}
else
{
back = new Node<T>(*(other.back));
copy(this->back, other.back);
}
return *this;
}
template <class T>
int Queue<T>::get_size() const
{
// TODO
return length;
}
template <class T>
void Queue<T>::push(T* t)
{
// TODO
if(t==NULL)
return;
if(length==0)
{
front=back=new Node<T>(t);
back->set_next(NULL);
length++;
return;
}
else
{
back->set_next(new Node<T>(t));
back=back->get_next();
back->set_next(NULL);
length++;
return;
}
}
template <class T>
void Queue<T>::push(Node<T>* n)
{
// TODO
if(n==NULL)
return;
if(length==0)
{
front=back=n;
back->set_next(NULL);
length++;
return;
}
else
{
back->set_next(n);
back=back->get_next();
back->set_next(NULL);
length++;
return;
}
}
template <class T>
Node<T>* Queue<T>::pop()
{
// TODO
if (length==0)
return NULL;
else if(length==1)
{
Node<T>*h=front;
front=back=NULL;
length--;
return h;
}
else
{
Node<T>* h = front;
front =front->get_next();
length--;
return h;
}
}
template <class T>
void Queue<T>::print()
{
// TODO
Stack<T>*s = new Stack<T>();
Node<T>*n = new Node<T>();
Node<T>*t = new Node<T>();
while (length!= 0)
{
n = this->pop();
s->push(n);
}
while (s->get_size()!=0)
{
t = s->pop();
t->print();
}
std::cout << "END" << std::endl;
return;
}
template <class T>
void Queue<T>::copy(Node<T>* this_, Node<T>* other_)
{
if (other_->get_next() == NULL)
{
return;
}
this_->set_next(new Node<T>(*(other_->get_next())));
copy(this_->get_next(), other_->get_next());
return;
}
template <class T>
void Queue<T>::remove(Node<T>* n)
{
if (n == NULL)
{
return;
}
if (n->get_next() == NULL)
{
delete n;
return;
}
Node<T>* tmp = n->get_next();
delete n;
remove(tmp);
return;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/54992.html
標籤:C語言
下一篇:【go學習筆記】四、條件和回圈
