-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked_List.h
135 lines (110 loc) · 1.96 KB
/
Linked_List.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
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
#pragma once
template <typename T>
class List_Node
{
public:
T data;
List_Node *next;
List_Node *previous;
};
template <typename T>
class Linked_List
{
public:
Linked_List ();
Linked_List (int Max);
~Linked_List ();
List_Node<T> add ();
void add (T Data);
void set_data (int index, T Data);
T & get_data (int index);
int count = 0;
private:
List_Node<T> *head, *tail;
};
template <typename T>
Linked_List<T>::Linked_List ()
{
head = nullptr;
tail = nullptr;
}
template <typename T>
Linked_List<T>::Linked_List (int Count)
{
count = Count;
List_Node *node = nullptr;
List_Node *last_node = nullptr;
for (int i = 0; i < count; i += 1)
{
node = new List_Node;
if (i == 0)
{
node->previous = nullptr;
head = node;
}
else
{
node->previous = last_node;
last_node->next = node;
}
if (i == max) tail = node;
last_node = node;
}
}
template <typename T>
Linked_List<T>::~Linked_List () {}
template <typename T>
List_Node<T> Linked_List<T>::add ()
{
List_Node<T> *node = new List_Node<T>;
if (count == 0)
{
head = node;
}
else
{
tail->next = node;
node->previous = tail;
}
tail = node;
count += 1;
return *node;
}
template <typename T>
void Linked_List<T>::add (T Data)
{
//List_Node<T> node = add ();
List_Node<T> *node = new List_Node<T>;
if (count == 0)
{
head = node;
}
else
{
tail->next = node;
node->previous = tail;
}
tail = node;
count += 1;
node->data = Data;
}
template <typename T>
void Linked_List<T>::set_data (int index, T Data)
{
List_Node<T> *node = head;
for (int i = 0; i < index; i += 1)
{
node = node->next;
}
node->data = Data;
}
template <typename T>
T & Linked_List<T>::get_data (int index)
{
List_Node<T> *node = head;
for (int i = 0; i < index; i += 1)
{
node = node->next;
}
return node->data;
}