1
1
#include < iostream>
2
- #include < string>
2
+ #include < cstring>
3
+ #include < stdlib.h>
3
4
4
5
using namespace std ;
5
6
@@ -9,11 +10,13 @@ class List
9
10
typedef struct Node
10
11
{
11
12
int data;
13
+ int index;
12
14
struct Node * next;
13
15
struct Node * prev;
14
16
} node;
15
-
16
- node* list ()
17
+
18
+ // Private methods
19
+ node* m_listNode ()
17
20
{
18
21
node* head = NULL ;
19
22
head = (node*)malloc (sizeof (node));
@@ -25,14 +28,40 @@ class List
25
28
return head;
26
29
}
27
30
31
+ void m_removeNode (node* target)
32
+ {
33
+ node* nextNode = target->next ;
34
+ node* prevNode = target->prev ;
35
+
36
+ if (prevNode != NULL && nextNode != NULL )
37
+ {
38
+ prevNode->next = nextNode;
39
+ nextNode->prev = prevNode;
40
+ }
41
+ m_size--;
42
+ cout << " Removed " << target->data << endl;
43
+
44
+ free (target);
45
+
46
+ }
47
+
48
+ void copyNode (node* dest, node* src)
49
+ {
50
+ dest->next = src->next ;
51
+ dest->prev = src->prev ;
52
+ dest->data = src->data ;
53
+ }
54
+
28
55
// Private variables.
29
- int m_size = 0 ;
30
- int m_maxIndex = m_size - 1 ;
31
- node* m_head = list();
56
+ node* m_head = m_listNode();
32
57
node* m_first = m_head; // Sets the first list node to the current state of m_head
33
58
node* m_last = m_head;
59
+ node* m_lastRemovedNode = m_listNode();
60
+ int INDEX = 0 ;
61
+ int m_lastRemovedIndex;
34
62
35
63
public:
64
+ int m_size = 0 ;
36
65
// prints list
37
66
void print ()
38
67
{
@@ -41,9 +70,9 @@ class List
41
70
// with 0, so when the list is printed, we skip the first one.
42
71
if (m_size != 0 )
43
72
{
44
- if (m_head->next != NULL )
73
+ if (m_head->next != NULL )
45
74
{
46
- m_head = m_head ->next ;
75
+ m_head = m_first ->next ;
47
76
}
48
77
while (m_head != NULL )
49
78
{
@@ -58,64 +87,68 @@ class List
58
87
printf (" \n " );
59
88
}
60
89
61
- // removes element from given index
62
- int remell (int index)
90
+ int remel (int index)
63
91
{
64
- m_head = m_first;
65
- if (index != 0 )
66
- {
67
- m_head = m_head->next ; // skip the first one, it's just the initializer.
68
- }
69
- if (m_head->next == NULL )
70
- {
71
- cout << " ERROR: NULL at index " << index << " .\n " ;
72
- exit (1 );
73
- }
74
- if (index > m_size - 1 )
92
+ int maxIndex = m_size - 1 ;
93
+
94
+ // Index out of range
95
+ if (index > maxIndex)
75
96
{
76
- cout << " [ERROR]: List is empty !\n " ;
97
+ cout << " [ERROR]: Index out of range !\n " ;
77
98
exit (1 );
78
99
}
79
-
100
+
101
+ m_head = m_first->next ;
102
+ // Start at the beginning of the list and iterate over the items until reaching the desired index.
80
103
for (int i = 0 ; i <= index; i++)
81
- {
82
- if (i == index - 1 || index == 0 )
104
+ {
105
+ if (i == index && m_head != NULL )
83
106
{
84
- if (m_head != NULL )
85
- {
86
- node* targetHead = m_head->next ;
87
- int data = targetHead->data ;
88
- if (targetHead->next != NULL )
89
- {
90
- node* nextHead = targetHead->next ;
91
- nextHead->prev = m_head; // New head-prev pointer = current head
92
- m_head->next = nextHead; // current head-next pointer = nextHead
93
-
94
- free (targetHead);
95
- m_size--;
96
- return data;
97
- }
98
- // Target head is last item in list, do this...
99
- else
100
- {
101
- m_head->next = NULL ;
102
- free (targetHead);
103
- m_size--;
104
- return data;
105
- }
106
- }
107
+ int data = m_head->data ;
108
+ copyNode (m_lastRemovedNode, m_head);
109
+ m_removeNode (m_head);
110
+ return data;
107
111
}
108
112
m_head = m_head->next ;
109
113
}
114
+
110
115
cout << " [ERROR]: Something went wrong while removing element from index " << index << endl;
111
116
cout << " Make sure list is not empty, and index is not out of range.\n " ;
112
117
exit (1 );
113
118
}
114
119
120
+ void slice (int startIndex, int endIndex)
121
+ {
122
+ int maxIndex = m_size - 1 ;
123
+
124
+ // Index out of range
125
+ if (startIndex > maxIndex)
126
+ {
127
+ cout << " [ERROR]: Index out of range!\n " ;
128
+ cout << " Max index is " << maxIndex << " but you chose " << startIndex << " for slice start.\n " ;
129
+ exit (1 );
130
+ }
131
+ if (endIndex > maxIndex)
132
+ {
133
+ cout << " [ERROR]: Index out of range!\n " ;
134
+ cout << " Max index is " << maxIndex << " but you chose " << endIndex << " for slice end.\n " ;
135
+ exit (1 );
136
+ }
137
+
138
+ remel (startIndex);
139
+ for (int i = startIndex + 1 ; i <= endIndex; i++)
140
+ {
141
+ node* targetNode = m_lastRemovedNode->next ;
142
+ copyNode (m_lastRemovedNode, targetNode);
143
+ m_removeNode (targetNode);
144
+ }
145
+ }
146
+
115
147
int index (int index)
116
148
{
149
+ int maxIndex = m_size - 1 ;
117
150
m_head = m_first;
118
- if (index > m_size - 1 )
151
+ if (index > maxIndex )
119
152
{
120
153
cout << " [ERROR] No element at index " << index << " !!!\n " ;
121
154
cout << " The length of the list you are accessing is only " << m_size << endl;
@@ -139,8 +172,7 @@ class List
139
172
// appends to list
140
173
void append (int value)
141
174
{
142
-
143
- node* nextNode = list ();
175
+ node* nextNode = m_listNode ();
144
176
m_last->next = nextNode;
145
177
nextNode->prev = m_last;
146
178
nextNode->data = value;
0 commit comments