Skip to content

Commit f891863

Browse files
ClémentClément
authored andcommitted
Improving DLLists.
1 parent a07376e commit f891863

File tree

2 files changed

+102
-16
lines changed

2 files changed

+102
-16
lines changed

source/code/projects/DLList_ICollection/DLList_ICollection/DLList.cs

Lines changed: 90 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,15 @@ public void Add(T value)
4747
"List is read-only."
4848
);
4949
}
50-
Cell cCell = head;
51-
if (cCell != null)
50+
if (head == null)
5251
{
53-
while (cCell.Next != null)
54-
// As long as the cCell Cell has a neighbour…
55-
{
56-
cCell = cCell.Next;
57-
// We move the cCell cell to this neighbour.
58-
}
59-
Cell newNode = new Cell(value, cCell, null);
60-
cCell.Next = newNode;
61-
tail = newNode;
52+
head = new Cell(value, null, null);
53+
tail = head;
6254
}
6355
else
6456
{
65-
head = new Cell(value, null);
66-
tail = head;
57+
tail.Next = new Cell(value, tail, null);
58+
tail = tail.Next;
6759
}
6860
}
6961

@@ -137,7 +129,7 @@ public bool Remove(T value)
137129
else
138130
{
139131
Cell cCell = head;
140-
while (cCell.Next != null)
132+
while (cCell.Next != null && !removed)
141133
{
142134
if (cCell.Next.Data.Equals(value))
143135
{
@@ -146,6 +138,10 @@ public bool Remove(T value)
146138
cCell.Next = cCell.Next.Next;
147139
removed = true;
148140
}
141+
else
142+
{
143+
cCell = cCell.Next;
144+
}
149145
}
150146
}
151147
}
@@ -213,11 +209,91 @@ public override string ToString()
213209
{
214210
returned += "————";
215211
}
212+
213+
// We go through the list in the other direction:
214+
215+
returned += "\n———";
216+
// Line above the table
217+
for (int i = 0; i < Count; i++)
218+
{
219+
returned += "————";
220+
}
221+
returned += "\n| ";
222+
// Content of the CList
223+
cCell = tail;
224+
while (cCell != null)
225+
{
226+
returned += $"{cCell.Data} | ";
227+
cCell = cCell.Previous;
228+
}
229+
returned += "\n———";
230+
// Line below the table
231+
for (int i = 0; i < Count; i++)
232+
{
233+
returned += "————";
234+
}
235+
216236
if (Count > 0)
217237
{
218238
returned += $"\nHead: {head.Data}\n";
219239
returned += $"Tail: {tail.Data}\n";
220240
}
221241
return returned;
222242
}
243+
244+
public void RemoveF()
245+
{
246+
if (head != null)
247+
{
248+
if (head.Next != null)
249+
{
250+
head.Next.Previous = null;
251+
}
252+
head = head.Next;
253+
}
254+
}
255+
256+
public void RemoveL()
257+
{
258+
if (tail != null)
259+
{
260+
if (tail.Previous != null)
261+
{
262+
tail.Previous.Next = null;
263+
}
264+
tail = tail.Previous;
265+
}
266+
}
267+
268+
// Method to remove the nth element if it exists.
269+
public void RemoveI(int index)
270+
{
271+
if (index > Count || index < 0)
272+
{
273+
throw new IndexOutOfRangeException();
274+
}
275+
else // Some IDE will flag this "else" as redundant.
276+
{
277+
if (index == 0)
278+
{
279+
RemoveF();
280+
}
281+
else if (index == (Count - 1))
282+
{
283+
RemoveL();
284+
}
285+
else
286+
{
287+
int counter = 0;
288+
Cell cCell = head;
289+
while (counter < index - 1)
290+
{
291+
cCell = cCell.Next;
292+
counter++;
293+
}
294+
cCell.Next = cCell.Next.Next;
295+
cCell.Next.Previous = cCell;
296+
}
297+
}
298+
}
223299
}

source/code/projects/DLList_ICollection/DLList_ICollection/Program.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ static void Main(string[] args)
1313
myList1.Add(2);
1414
myList1.Add(1);
1515
Console.WriteLine(myList1);
16+
17+
myList1.RemoveI(3);
18+
Console.WriteLine(myList1);
19+
20+
myList1.RemoveF();
21+
Console.WriteLine(myList1);
22+
myList1.RemoveL();
23+
Console.WriteLine(myList1);
24+
25+
myList1.RemoveI(1);
26+
Console.WriteLine(myList1);
27+
1628
myList1.Clear();
1729
myList1.Add(1);
1830
myList1.Add(3);
@@ -23,15 +35,13 @@ static void Main(string[] args)
2335
myList1.Add(2);
2436
myList1.Add(22);
2537
Console.WriteLine(myList1);
26-
/*
2738
Console.WriteLine("Removed 3: " + myList1.Remove(3));
2839
Console.WriteLine(myList1);
2940
Console.WriteLine("Removed 21: " + myList1.Remove(21));
3041
Console.WriteLine(myList1);
3142
Console.WriteLine("Removed 2: " + myList1.Remove(2));
3243
Console.WriteLine(myList1);
3344

34-
*/
3545
// A benefit of realizing the ICollection interface
3646
// is that we can iterate over elements of lists now:
3747
foreach (var item in myList1)

0 commit comments

Comments
 (0)