DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
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"
7
8namespace DotNetDupe {
9 namespace System {
10 namespace Collections {
11 namespace Generic {
12
16 template <typename T>
17 class LinkedListNode : public Object {
18 public:
22
25 LinkedListNode(const T& val) : Value(val), Next(nullptr), Previous(nullptr) {}
26
27 void* operator new(size_t size) {
29 }
30 void operator delete(void* 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
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}
Provides methods for creating, manipulating, searching, and sorting arrays.
Defines common cross-platform macros, export decorators, and fundamental types.
Defines the exception thrown when a method call is invalid for the object's current state.
Base object class for DotNetDupe mirroring .NET System.Object.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition Array.h:29
int GetCount() const
Gets the number of nodes actually contained in the LinkedList.
Definition LinkedList.h:59
LinkedListNode< T > * AddFirst(const T &value)
Definition LinkedList.h:69
LinkedListNode< T > * AddLast(const T &value)
Definition LinkedList.h:82
LinkedList()
Initializes a new instance of the LinkedList class that is empty.
Definition LinkedList.h:50
LinkedListNode< T > * GetLast() const
Gets the last node of the LinkedList.
Definition LinkedList.h:67
~LinkedList() override
Destructor. Clears all nodes.
Definition LinkedList.h:53
LinkedListNode< T > * GetFirst() const
Gets the first node of the LinkedList.
Definition LinkedList.h:63
LinkedListNode< T > * Previous
Gets the previous node in the LinkedList.
Definition LinkedList.h:21
LinkedListNode(const T &val)
Initializes a new instance of the LinkedListNode class, containing the specified value.
Definition LinkedList.h:25
LinkedListNode< T > * Next
Gets the next node in the LinkedList.
Definition LinkedList.h:20
The exception that is thrown when a method call is invalid for the object's current state.
Supports all classes in the DotNetDupe class hierarchy.
Definition Object.h:18
void * AllocateCollectionBuffer(size_t size)
Internal memory allocator for generic collections.
Definition Object.cpp:39
void FreeCollectionBuffer(void *p)
Frees collection buffer allocated with AllocateCollectionBuffer.
Definition Object.cpp:44