Repository navigation
Expand file tree
/
Copy pathListDef.inl
More file actions
115 lines (114 loc) · 2.54 KB
/
Copy pathListDef.inl
File metadata and controls
115 lines (114 loc) · 2.54 KB
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
#include "leetcode.h"
namespace Solution
{
template <typename T>
DoubleLinkedList<T>::DoubleLinkedList() {
m_nCount = 0;
m_pHead = nullptr;
}
template <typename T>
DoubleLinkedList<T>::~DoubleLinkedList()
{
while (m_nCount >= 0)
{
DLListNode* pNode = popAtIndex(0);
delete pNode;
}
}
template <typename T>
T DoubleLinkedList<T>::get(int index)
{
DLListNode* pNode = _GetNodeAtIndex(index);
if (nullptr == pNode)
return -1;
return pNode->m_nVal;
}
template <typename T>
DLListNode<T>* DoubleLinkedList<T>::addAtHead(T val) {
return addAtIndex(0, val);
}
template <typename T>
DLListNode<T>* DoubleLinkedList<T>::addAtTail(T val) {
return addAtIndex(m_nCount, val);
}
template <typename T>
DLListNode<T>* DoubleLinkedList<T>::addAtIndex(int index, T val) {
DLListNode* pNode = _GetNodeAtIndex(index == m_nCount ? 0 : index);
if (nullptr == pNode)
{
if (0 != m_nCount || 0 != index)
return;
DLListNode* pNewNode = _GetDLNode(val);
pNewNode->m_pPrev = pNewNode;
pNewNode->m_pNext = pNewNode;
m_pHead = pNewNode;
++m_nCount;
return;
}
DLListNode* pNewNode = _GetDLNode(val);
if (nullptr == pNewNode)
return;
pNewNode->m_pPrev = pNode->m_pPrev;
pNewNode->m_pNext = pNode;
pNode->m_pPrev->m_pNext = pNewNode;
pNode->m_pPrev = pNewNode;
if (0 == index)
m_pHead = pNewNode;
++m_nCount;
}
template <typename T>
void DoubleLinkedList<T>::deleteAtIndex(int index) {
DLListNode* pNode = popAtIndex(index);
delete pNode;
}
template <typename T>
DLListNode<T>* DoubleLinkedList<T>::popAtIndex(int index)
{
DLListNode* pNode = _GetNodeAtIndex(index);
if (nullptr == pNode)
return;
pNode->m_pPrev->m_pNext = pNode->m_pNext;
pNode->m_pNext->m_pPrev = pNode->m_pPrev;
if (0 == index)
{
m_pHead = pNode->m_pNext;
}
--m_nCount;
if (0 == m_nCount)
{
m_pHead = nullptr;
}
pNode->m_pPrev = nullptr;
pNode->m_pNext = nullptr;
return pNode;
}
template<typename T>
DLListNode<T>* DoubleLinkedList<T>::pop_back()
{
return popAtIndex(m_nCount - 1);
}
template<typename T>
DLListNode<T>* DoubleLinkedList<T>::pop_front()
{
return popAtIndex(0);
}
template <typename T>
DLListNode<T>* DoubleLinkedList<T>::_GetDLNode(T val)
{
DLListNode* ret = new DLListNode(val);
return ret;
}
template <typename T>
DLListNode<T>* DoubleLinkedList<T>::_GetNodeAtIndex(int index)
{
if (index < 0 || index >= m_nCount)
return nullptr;
DLListNode* pNode = m_pHead;
while (index > 0)
{
pNode = pNode->m_pNext;
--index;
}
return pNode;
}
}