-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListFrontT.hpp
155 lines (132 loc) · 4.13 KB
/
ListFrontT.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/******************************************************************************
* Simple template implementation of a List(front insertion) class.
*
* @Authur : JeJo
* @Date : 01 - 12 -2017
* @license: free to use and distribute(no further support as well)
*
*****************************************************************************/
#ifndef LIST_FRONT_T_HPP
#define LIST_FRONT_T_HPP
// macros for name-spacing
#define JEJO_BEGIN namespace JeJo {
#define JEJO_END }
// C++ headers
#include <iostream>
#include <memory> // std::unique_ptr
#include <utility> // std::move
#include <cstddef> // std::size_t
#include <stdexcept> // std::runtime_error
#include <initializer_list> // std::initializer_list
JEJO_BEGIN
/* class 'ListFront<>': provides a class for list, where elements could be
* inserted at the front of the list.
*
* @tparam: generic type
*/
template<typename Type> class ListFront final
{
private:
/* class 'Node': provides a class for keeping resources in the list
* implementation, where elements would be inserted at the front of
* the list.
*
* @tparam: 'Node' has a data member of generic type.
*/
template<typename T> struct Node final
{
T data{};
std::unique_ptr<Node<T>> nextNode = nullptr;
~Node() { std::cout << "Deleting: " << data << '\n'; }
};
private:
// convenience (template alias)type(s)
using NodeT = Node<Type>;
using UPtrNode = std::unique_ptr<NodeT>;
// head of the list
UPtrNode _headNode = nullptr;
// size of the list
std::size_t _size = 0;
// last element of the list
Type _lastElement{};
public:
// Constructor(s)
ListFront() = default;
/* @todo: since it is a test project, further care for copying
* and moving of 'ListFront' class not been considered.
*
* For simplicity-sake, they are simply disabled.
*/
ListFront(const ListFront&) = delete;
ListFront& operator=(const ListFront&) = delete;
ListFront(ListFront&&) = delete;
ListFront& operator=(ListFront&&) = delete;
// list initialization constructor
ListFront(const std::initializer_list<Type> list)
{
for (const Type& element : list) this->insert(element);
}
// return the size of the list
std::size_t size() const noexcept { return _size; }
// return the state of the list
bool is_empty() const noexcept { return _headNode == nullptr; }
// return the last inserted element(front) of the list
const Type front() const noexcept
{
return this->is_empty()
? (Type{}) // @todo this needed to be improved
: _headNode->data;
}
// return the first inserted element(back) of the list
const Type back() const noexcept
{
return this->is_empty()
? (Type{}) // @todo this needed to be improved
: _lastElement;
}
void insert(const Type &x) noexcept
{
UPtrNode tempNode = std::make_unique<NodeT>();
tempNode->data = x;
tempNode->nextNode = std::move(_headNode);
_headNode = std::move(tempNode);
++_size; // increment the size
if (_size == 1) _lastElement = _headNode->data;
}
friend std::ostream& operator<<(
std::ostream& out, const ListFront &obj) noexcept
{
out << "The list is: ";
const NodeT *tempNode = obj._headNode.get();
for ( ; tempNode ; tempNode = tempNode->nextNode.get())
out << tempNode->data << " ";
return delete tempNode, tempNode = nullptr, out << '\n';
}
};
/*************** test function to be called in the main() *****************/
template<typename Type>
void testListFront(const std::initializer_list<Type> list)
{
ListFront<Type> obj{ list };
std::cout << std::boolalpha
<< "is empty: " << obj.is_empty()
<< "\nthe element at front: " << obj.front() << '\n';
std::cout << "Enter number of elements in the array: ";
std::size_t size; std::cin >> size;
while (size--)
{
std::cout << "Enter the element: ";
Type x;
std::cin >> x;
obj.insert(x);
std::cout << "The size: " << obj.size() << ", "
<< "is empty: " << obj.is_empty() << ", "
<< "The front: " << obj.front() << ", "
<< "The back: " << obj.back() << ", "
<< obj << '\n';
}
std::cout << "\nTest success\n";
}
JEJO_END
#endif // LIST_FRONT_T_HPP
/*****************************************************************************/