-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked-List.h
230 lines (229 loc) · 5.02 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
template <typename type>
class LinkedListNode
{
public:
LinkedListNode<type> *next = NULL, *prev = NULL ;
type data ;
};
template <typename type>
class LinkedList
{
typedef LinkedListNode<type> ListNode ;
public:
void push_back(type val)
{
if(is_empty())
{
head = new ListNode ;
head->data = val ;
last = head ;
}
else
{
ListNode *temp = new ListNode ;
temp->data = val ;
last->next = temp ;
temp->prev = last ;
temp->next = NULL ;
last = temp ;
}
len++ ;
}
void push_front (type &val)
{
if (is_empty())
{
head = new ListNode ;
head->data = val ;
last = head ;
}
else
{
ListNode *temp = new ListNode ;
temp->data = val ;
temp->prev = NULL ;
temp->next = head ;
head->prev = temp ;
head = temp ;
}
len++ ;
}
void push_using_index(type val, int &index)
{
if(index == 0)
push_front(val) ;
else if(index == len)
push_back(val) ;
else if(index > len)
cout<<"not found!"<<endl ;
else
{
auto newNode = new ListNode ;
auto temp = get_node_using_index(index) ;
newNode->data = val ;
newNode->prev = temp->prev ;
newNode->next = temp ;
temp->prev = newNode ;
newNode->prev->next = newNode ;
len++ ;
}
}
void push_after(ListNode *curr, type NewData)
{
if(last == curr)
push_back(NewData) ;
else
{
auto NewNode = new ListNode ;
NewNode->data = NewData ;
NewNode->next = curr->next ;
NewNode->prev = curr ;
NewNode->next->prev = NewNode ;
curr->next = NewNode ;
len++ ;
}
}
ListNode* findd(type curr)
{
for(auto Node = head ; Node != NULL ; Node = Node->next )
if(Node->data == curr)
return Node ;
return NULL ;
}
void pop_back()
{
if(!is_empty())
{
if(last->prev != NULL)
{
last = last->prev ;
delete last->next ;
last->next = NULL ;
}
else
{
delete head ;
head = last = NULL ;
}
len-- ;
}
else
cout<<"is empty :/\n" ;
}
void pop_front()
{
if(!is_empty())
{
if(head == last)
{
delete head ;
head = last = NULL ;
}
else
{
head = head->next ;
delete head->prev ;
head->prev = NULL ;
}
len-- ;
}
}
void pop_using_index(int index)
{
if(index == 0)
pop_front() ;
else if(index == get_len()-1 )
pop_back() ;
else
{
auto curr = get_node_using_index(index) ;
pop_using_LinkedListNode(curr) ;
}
}
void pop_using_LinkedListNode(ListNode* temp)
{
if(temp == head)
pop_front() ;
else if(temp == last )
pop_back() ;
else
{
temp->prev->next = temp->next ;
temp->next->prev = temp->prev ;
delete temp ;
len-- ;
}
}
ListNode* operator[] (int index)
{
auto node = head ;
for (int i = 0; i < index ; i++)
{
if(node == NULL)
cout<<"false index"<<endl ;
node = node->next;
}
return node;
}
int get_len()
{
return len ;
}
int get_index(type nodeToFind)
{
auto temp = head ;
int index = 0 ;
while(temp)
{
if(temp->data == nodeToFind)
return index ;
temp = temp->next ;
index++ ;
}
cout<<"not found3"<<endl ;
return -1 ;
}
bool is_empty()
{
return head == NULL ;
}
ListNode* get_node_using_index(int index)
{
auto curr = head ;
for(int i = 0 ; i<index ; i++ )
{
if(curr == NULL)
cout<<"false index"<<endl ;
curr = curr->next ;
}
return curr ;
}
ListNode* begin()
{
return head ;
}
ListNode* end()
{
return last ;
}
void display()
{
int i = 0 ;
curr = head ;
while(curr != NULL)
{
i++ ;
cout<<curr->data<<" " ;
curr = curr->next ;
}
cout<<endl ;
if(i != len)
cout<<"mistake"<<endl ;
}
private:
ListNode *head = NULL, *last = NULL, *curr = NULL ;
int len = 0 ;
};