DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
Loading...
Searching...
No Matches
LinkedList.h
Go to the documentation of this file.
1
#pragma once
2
3
#include "
Common.h
"
4
#include "
System/Object.h
"
5
#include "
System/Array.h
"
6
#include "
System/InvalidOperationException.h
"
7
8
namespace
DotNetDupe
{
9
namespace
System
{
10
namespace
Collections
{
11
namespace
Generic
{
12
16
template
<
typename
T>
17
class
LinkedListNode
:
public
Object
{
18
public
:
19
T
Value
;
20
LinkedListNode<T>
*
Next
;
21
LinkedListNode<T>
*
Previous
;
22
25
LinkedListNode
(
const
T& val) :
Value
(val),
Next
(nullptr),
Previous
(nullptr) {}
26
27
void
*
operator
new
(
size_t
size) {
28
return
System::AllocateCollectionBuffer
(size);
29
}
30
void
operator
delete
(
void
* p) {
31
System::FreeCollectionBuffer
(p);
32
}
33
};
34
41
template
<
typename
T>
42
class
LinkedList
:
public
Object
{
43
private
:
44
LinkedListNode<T>
* m_pHead;
45
LinkedListNode<T>
* m_pTail;
46
int
m_iCount;
47
48
public
:
50
LinkedList
() : m_pHead(nullptr), m_pTail(nullptr), m_iCount(0) {}
51
53
~LinkedList
()
override
{
54
Clear
();
55
}
56
59
int
GetCount
()
const
{
return
m_iCount; }
60
63
LinkedListNode<T>
*
GetFirst
()
const
{
return
m_pHead; }
64
67
LinkedListNode<T>
*
GetLast
()
const
{
return
m_pTail; }
68
69
LinkedListNode<T>
*
AddFirst
(
const
T& value) {
70
LinkedListNode<T>
* pNode =
new
LinkedListNode<T>
(value);
71
if
(!m_pHead) {
72
m_pHead = m_pTail = pNode;
73
}
else
{
74
pNode->
Next
= m_pHead;
75
m_pHead->Previous = pNode;
76
m_pHead = pNode;
77
}
78
m_iCount++;
79
return
pNode;
80
}
81
82
LinkedListNode<T>
*
AddLast
(
const
T& value) {
83
LinkedListNode<T>
* pNode =
new
LinkedListNode<T>
(value);
84
if
(!m_pTail) {
85
m_pHead = m_pTail = pNode;
86
}
else
{
87
m_pTail->Next = pNode;
88
pNode->
Previous
= m_pTail;
89
m_pTail = pNode;
90
}
91
m_iCount++;
92
return
pNode;
93
}
94
95
void
RemoveFirst
() {
96
if
(!m_pHead)
throw
System::InvalidOperationException
(
"LinkedList is empty."
);
97
LinkedListNode<T>
* pTemp = m_pHead;
98
m_pHead = m_pHead->
Next
;
99
if
(m_pHead) {
100
m_pHead->Previous =
nullptr
;
101
}
else
{
102
m_pTail =
nullptr
;
103
}
104
delete
pTemp;
105
m_iCount--;
106
}
107
108
void
RemoveLast
() {
109
if
(!m_pTail)
throw
System::InvalidOperationException
(
"LinkedList is empty."
);
110
LinkedListNode<T>
* pTemp = m_pTail;
111
m_pTail = m_pTail->
Previous
;
112
if
(m_pTail) {
113
m_pTail->Next =
nullptr
;
114
}
else
{
115
m_pHead =
nullptr
;
116
}
117
delete
pTemp;
118
m_iCount--;
119
}
120
121
bool
Remove
(
const
T& value) {
122
LinkedListNode<T>
* pCurr = m_pHead;
123
while
(pCurr) {
124
if
(pCurr->
Value
== value) {
125
if
(pCurr->
Previous
) pCurr->
Previous
->Next = pCurr->
Next
;
126
else
m_pHead = pCurr->
Next
;
127
128
if
(pCurr->
Next
) pCurr->
Next
->Previous = pCurr->
Previous
;
129
else
m_pTail = pCurr->
Previous
;
130
131
delete
pCurr;
132
m_iCount--;
133
return
true
;
134
}
135
pCurr = pCurr->
Next
;
136
}
137
return
false
;
138
}
139
140
bool
Contains
(
const
T& value)
const
{
141
LinkedListNode<T>
* pCurr = m_pHead;
142
while
(pCurr) {
143
if
(pCurr->
Value
== value)
return
true
;
144
pCurr = pCurr->
Next
;
145
}
146
return
false
;
147
}
148
149
void
Clear
() {
150
LinkedListNode<T>
* pCurr = m_pHead;
151
while
(pCurr) {
152
LinkedListNode<T>
* pNext = pCurr->
Next
;
153
delete
pCurr;
154
pCurr = pNext;
155
}
156
m_pHead = m_pTail =
nullptr
;
157
m_iCount = 0;
158
}
159
160
Array<T>
ToArray
()
const
{
161
Array<T>
arrResult(m_iCount);
162
LinkedListNode<T>
* pCurr = m_pHead;
163
int
iIndex = 0;
164
while
(pCurr) {
165
arrResult[iIndex++] = pCurr->
Value
;
166
pCurr = pCurr->
Next
;
167
}
168
return
arrResult;
169
}
170
};
171
172
}
173
}
174
}
175
}
Array.h
Provides methods for creating, manipulating, searching, and sorting arrays.
Common.h
Defines common cross-platform macros, export decorators, and fundamental types.
InvalidOperationException.h
Defines the exception thrown when a method call is invalid for the object's current state.
Object.h
Base object class for DotNetDupe mirroring .NET System.Object.
DotNetDupe::System::Array
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition
Array.h:29
DotNetDupe::System::Collections::Generic::LinkedList::Clear
void Clear()
Definition
LinkedList.h:149
DotNetDupe::System::Collections::Generic::LinkedList::GetCount
int GetCount() const
Gets the number of nodes actually contained in the LinkedList.
Definition
LinkedList.h:59
DotNetDupe::System::Collections::Generic::LinkedList::AddFirst
LinkedListNode< T > * AddFirst(const T &value)
Definition
LinkedList.h:69
DotNetDupe::System::Collections::Generic::LinkedList::ToArray
Array< T > ToArray() const
Definition
LinkedList.h:160
DotNetDupe::System::Collections::Generic::LinkedList::RemoveLast
void RemoveLast()
Definition
LinkedList.h:108
DotNetDupe::System::Collections::Generic::LinkedList::AddLast
LinkedListNode< T > * AddLast(const T &value)
Definition
LinkedList.h:82
DotNetDupe::System::Collections::Generic::LinkedList::LinkedList
LinkedList()
Initializes a new instance of the LinkedList class that is empty.
Definition
LinkedList.h:50
DotNetDupe::System::Collections::Generic::LinkedList::Remove
bool Remove(const T &value)
Definition
LinkedList.h:121
DotNetDupe::System::Collections::Generic::LinkedList::Contains
bool Contains(const T &value) const
Definition
LinkedList.h:140
DotNetDupe::System::Collections::Generic::LinkedList::GetLast
LinkedListNode< T > * GetLast() const
Gets the last node of the LinkedList.
Definition
LinkedList.h:67
DotNetDupe::System::Collections::Generic::LinkedList::RemoveFirst
void RemoveFirst()
Definition
LinkedList.h:95
DotNetDupe::System::Collections::Generic::LinkedList::~LinkedList
~LinkedList() override
Destructor. Clears all nodes.
Definition
LinkedList.h:53
DotNetDupe::System::Collections::Generic::LinkedList::GetFirst
LinkedListNode< T > * GetFirst() const
Gets the first node of the LinkedList.
Definition
LinkedList.h:63
DotNetDupe::System::Collections::Generic::LinkedListNode
Represents a node in a LinkedList.
Definition
LinkedList.h:17
DotNetDupe::System::Collections::Generic::LinkedListNode::Previous
LinkedListNode< T > * Previous
Gets the previous node in the LinkedList.
Definition
LinkedList.h:21
DotNetDupe::System::Collections::Generic::LinkedListNode::Value
T Value
The value contained in the node.
Definition
LinkedList.h:19
DotNetDupe::System::Collections::Generic::LinkedListNode::LinkedListNode
LinkedListNode(const T &val)
Initializes a new instance of the LinkedListNode class, containing the specified value.
Definition
LinkedList.h:25
DotNetDupe::System::Collections::Generic::LinkedListNode::Next
LinkedListNode< T > * Next
Gets the next node in the LinkedList.
Definition
LinkedList.h:20
DotNetDupe::System::InvalidOperationException
The exception that is thrown when a method call is invalid for the object's current state.
Definition
InvalidOperationException.h:16
DotNetDupe::System::Object
Supports all classes in the DotNetDupe class hierarchy.
Definition
Object.h:18
DotNetDupe::System::Collections::Generic
Definition
Dictionary.h:15
DotNetDupe::System::Collections
Definition
BlockingCollection.h:15
DotNetDupe::System
Definition
Action.h:11
DotNetDupe::System::AllocateCollectionBuffer
void * AllocateCollectionBuffer(size_t size)
Internal memory allocator for generic collections.
Definition
Object.cpp:39
DotNetDupe::System::FreeCollectionBuffer
void FreeCollectionBuffer(void *p)
Frees collection buffer allocated with AllocateCollectionBuffer.
Definition
Object.cpp:44
DotNetDupe
Definition
IServiceCollection.h:7
Include
System
Collections
Generic
LinkedList.h
Generated by
1.18.0