modified: assignment2/as2/as2.pro
modified: assignment2/as2/main.cpp
This commit is contained in:
@@ -2,6 +2,7 @@ TEMPLATE = app
|
|||||||
CONFIG += console
|
CONFIG += console
|
||||||
CONFIG -= app_bundle
|
CONFIG -= app_bundle
|
||||||
CONFIG -= qt
|
CONFIG -= qt
|
||||||
|
CONFIG += c++11
|
||||||
|
|
||||||
SOURCES += main.cpp
|
SOURCES += main.cpp
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,74 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
|
||||||
|
namespace ads {
|
||||||
|
|
||||||
|
template<typename Data> struct Node
|
||||||
|
{
|
||||||
|
Data value;
|
||||||
|
Node* next;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Node>
|
||||||
|
class List
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using DataType = decltype(Node::Data);
|
||||||
|
|
||||||
|
List() = default;
|
||||||
|
|
||||||
|
void push_front(DataType const& new_data)
|
||||||
|
{
|
||||||
|
do_push_front(new_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
DataType const& front()const
|
||||||
|
{
|
||||||
|
return head_->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pop()
|
||||||
|
{
|
||||||
|
do_pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool empty()const
|
||||||
|
{
|
||||||
|
return !head_ and !tail_;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
Node* head_{nullptr};
|
||||||
|
Node* tail_{nullptr};
|
||||||
|
std::size_t size_{0};
|
||||||
|
|
||||||
|
void do_push_front(DataType const& new_data)
|
||||||
|
{
|
||||||
|
if(empty())
|
||||||
|
head_ = tail_ = new Node{new_data, nullptr};
|
||||||
|
else
|
||||||
|
head_ = new Node{new_data, head_};
|
||||||
|
++size_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void do_pop()
|
||||||
|
{
|
||||||
|
if(empty())
|
||||||
|
throw std::runtime_error{"underflow!"};
|
||||||
|
head_ =head_->next;
|
||||||
|
--size_;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}//namespace
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
cout << "Hello World!" << endl;
|
ads::List<ads::Node<int>> l;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user