-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.cpp
More file actions
172 lines (150 loc) · 2.4 KB
/
Copy pathlist.cpp
File metadata and controls
172 lines (150 loc) · 2.4 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
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
#include "list.h"
List::List()
{
count_ = 0;
head_ = NULL;
tail_ = NULL;
}
int List::size()
{
return count_;
}
void List::add_element(int value)
{
Node* node = new Node();
node->data = value;
node->next = NULL;
if (count_ == 0)
{
head_ = node;
tail_ = head_;
}
else
{
tail_->next = node;
tail_ = node;
}
count_+=1;
}
void List::add_element(int value, int position)
{
Node* node = new Node();
node->data = value;
if (position >= count_-1)
{
add_element(value);
return;
}
else if (position <= 0)
{
node->next = head_;
head_ = node;
count_+=1;
return;
}
Node* prev = head_;
for (int i=1; i < position; i++)
prev = prev->next;
node->next = prev->next;
prev->next = node;
count_+=1;
}
int List::get_element(int position)
{
if (position >= count_-1)
return tail_->data;
else if (position <= 0)
return head_->data;
Node* curr = head_;
for (int i=0; i < position; i++)
curr = curr->next;
return curr->data;
}
void List::move_element(int curr_pos, int next_pos)
{
if (curr_pos == next_pos)
return;
int value = get_element(curr_pos);
remove_element_by_position(curr_pos);
add_element(value,next_pos);
}
int List::search_element(int value)
{
if (count_==0)
return -1;
Node* curr = head_;
for (int i=0; i<count_; i++)
{
if (curr->data == value)
return i;
curr = curr->next;
}
return -1;
}
void List::remove_element(int value)
{
if (count_==0)
return;
if (head_->data == value)
{
Node* temp = head_;
head_ = head_->next;
count_-=1;
delete temp;
remove_element(value);
return;
}
Node* prev = head_;
for (int i=1; i<count_; i++)
{
Node* curr;
curr = prev->next;
if (curr->data == value)
{
prev->next = curr->next;
count_-=1;
i-=1;
delete curr;
continue;
}
prev = prev->next;
}
tail_=prev;
}
void List::remove_element_by_position(int position)
{
if (position < 0 || position >= count_)
return;
Node* prev;
Node* curr = head_;
if (position == 0)
head_ = curr->next;
else
{
for (int i=0; i < position; i++)
{
prev = curr;
curr = curr->next;
}
if (curr->next == NULL) //Removing last position
{
prev->next = NULL;
tail_= prev;
}
else
prev->next = curr->next;
}
delete curr;
count_-=1;
}
void List::print_list()
{
if (count_ == 0)
return;
Node* curr_node = head_;
for (int i=0; i<count_; i++)
{
cout << curr_node->data << "\n";
curr_node = curr_node->next;
}
}