Files
159201/assignment3/as3/main.cpp

85 lines
1.2 KiB
C++
Raw Normal View History

#include <iostream>
#include <stdexcept>
namespace ads {
template<typename T>
class Queue
{
struct Node
{
T data_;
Node* next_;
};
public:
using SizeType = std::size_t;
void join(T t)
{
do_join(std::move(t));
}
void leave()
{
do_leave();
}
T const& front() const
{
return head_->data_;
}
SizeType size()const
{
return size_;
}
bool empty()const
{
return head_ or tail_;
}
private:
Node* head_{nullptr};
Node* tail_{nullptr};
SizeType size_{0};
void do_join(T && t) noexcept
{
if(empty())
head_ = tail_ = new Node{std::move(t), nullptr};
else
tail_ = tail_->next_ = new Node{std::move(t), nullptr};
++size_;
}
void do_leave()
{
if(size_ == 0)
throw std::runtime_error{"underflow!"};
if(size_ == 1)
{
delete head_;
head_ = tail_ = nullptr;
}
else
{
auto tmp = head_;
head_ = head_->next_;
delete tmp;
}
--size_;
}
};
}//namespace
int main()
{
return 0;
}