Skip to content

Commit 32fc4a2

Browse files
authored
added "slice" method,
"slice" method takes 2 arguments, start-index, and end-index . It will remove all elements from the start-index to the end-index. list.slice(4, 8) // Removes elements from index 4 to and including index 8.
1 parent 4736e35 commit 32fc4a2

File tree

3 files changed

+92
-60
lines changed

3 files changed

+92
-60
lines changed

linkedLists.h

Lines changed: 83 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <iostream>
2-
#include <string>
2+
#include <cstring>
3+
#include <stdlib.h>
34

45
using namespace std;
56

@@ -9,11 +10,13 @@ class List
910
typedef struct Node
1011
{
1112
int data;
13+
int index;
1214
struct Node* next;
1315
struct Node* prev;
1416
} node;
15-
16-
node* list()
17+
18+
// Private methods
19+
node* m_listNode()
1720
{
1821
node* head = NULL;
1922
head = (node*)malloc(sizeof(node));
@@ -25,14 +28,40 @@ class List
2528
return head;
2629
}
2730

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+
2855
// 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();
3257
node* m_first = m_head; // Sets the first list node to the current state of m_head
3358
node* m_last = m_head;
59+
node* m_lastRemovedNode = m_listNode();
60+
int INDEX = 0;
61+
int m_lastRemovedIndex;
3462

3563
public:
64+
int m_size = 0;
3665
// prints list
3766
void print()
3867
{
@@ -41,9 +70,9 @@ class List
4170
// with 0, so when the list is printed, we skip the first one.
4271
if(m_size != 0)
4372
{
44-
if(m_head->next != NULL)
73+
if(m_head->next != NULL)
4574
{
46-
m_head = m_head->next;
75+
m_head = m_first->next;
4776
}
4877
while(m_head != NULL)
4978
{
@@ -58,64 +87,68 @@ class List
5887
printf("\n");
5988
}
6089

61-
// removes element from given index
62-
int remell(int index)
90+
int remel(int index)
6391
{
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)
7596
{
76-
cout << "[ERROR]: List is empty!\n";
97+
cout << "[ERROR]: Index out of range!\n";
7798
exit(1);
7899
}
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.
80103
for(int i = 0; i <= index; i++)
81-
{
82-
if (i == index - 1 || index == 0)
104+
{
105+
if (i == index && m_head != NULL)
83106
{
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;
107111
}
108112
m_head = m_head->next;
109113
}
114+
110115
cout << "[ERROR]: Something went wrong while removing element from index " << index << endl;
111116
cout << "Make sure list is not empty, and index is not out of range.\n";
112117
exit(1);
113118
}
114119

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+
115147
int index(int index)
116148
{
149+
int maxIndex = m_size - 1;
117150
m_head = m_first;
118-
if (index > m_size - 1)
151+
if (index > maxIndex)
119152
{
120153
cout << "[ERROR] No element at index " << index << "!!!\n";
121154
cout << "The length of the list you are accessing is only " << m_size << endl;
@@ -139,8 +172,7 @@ class List
139172
// appends to list
140173
void append(int value)
141174
{
142-
143-
node* nextNode = list();
175+
node* nextNode = m_listNode();
144176
m_last->next = nextNode;
145177
nextNode->prev = m_last;
146178
nextNode->data = value;

list-test.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ int main()
88
{
99
List nums;
1010

11-
int count = 100;
12-
13-
for(int i = 0; i < count; i++)
11+
int count = 10;
12+
13+
for(int i = 0; i <= count; i++)
1414
{
1515
nums.append(i);
1616
cout << "Appended " << i << endl;
1717
}
18-
for(int i = 0; i < count; i++)
19-
{
20-
int num = nums.remell(0);
21-
cout << "Removed " << num << endl;
22-
}
23-
18+
nums.slice(5, 8);
19+
nums.print();
20+
nums.slice(2, 4);
21+
nums.print();
22+
23+
2424
}

lists

336 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)