-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.h
106 lines (99 loc) · 1.54 KB
/
LinkedList.h
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
#pragma once
template<class T>
struct Node
{
Node<T>* prev;
T key;
Node<T>* next;
};
template<class T>
class LinkedList
{
private:
Node<T>* head;
public:
LinkedList();
~LinkedList();
void insert(T data);
void remove(Node<T>* node);
void insertsort();
Node<T>* GetHead();
};
template<class T>
inline LinkedList<T>::LinkedList():head(nullptr)
{
}
template<class T>
inline LinkedList<T>::~LinkedList()
{
Node<T>* pointer = head;
while (pointer != nullptr)
{
auto npointer = pointer;
pointer = pointer->next;
delete npointer;
}
}
template<class T>
inline void LinkedList<T>::insert(T data)
{
Node<T>* newnode = new Node<T>();
newnode->key = data;
newnode->prev = nullptr;
newnode->next = head;
if (head != nullptr)
{
head->prev = newnode;
}
head = newnode;
}
template<class T>
inline void LinkedList<T>::remove(Node<T>* node)
{
if (node == head)
{
head = node->next;
}
else
{
node->prev->next = node->next;
}
delete node;
}
template<class T>
inline void LinkedList<T>::insertsort()
{
Node<T>* pointer = nullptr;
if (head != nullptr)
{
pointer = head->next;
}
while (pointer != nullptr)
{
T ndata = pointer->key;
Node<T>* npointer = pointer->prev;
while (npointer != nullptr)
{
if (npointer->key > ndata)
{
npointer->next->key = npointer->key;
npointer = npointer->prev;
}
else
{
npointer->next->key = ndata;
break;
}
}
if (npointer == nullptr)
{
head->key = ndata;
}
pointer = pointer->next;
}
}
template<class T>
inline Node<T>* LinkedList<T>::GetHead()
{
return head;
}