Skip to content

Commit a07376e

Browse files
ClémentClément
authored andcommitted
Improving code for CList_ICollection.
1 parent 129dd95 commit a07376e

File tree

5 files changed

+373
-43
lines changed

5 files changed

+373
-43
lines changed

source/code/projects/CList/CList/Program.cs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -120,29 +120,37 @@ static void Main(string[] args)
120120
);
121121
}
122122

123-
/* Third example */
124-
CList<double> myList3 = new CList<double>();
125-
myList3.AddF(12.5);
126-
myList3.AddF(23.8);
127-
myList3.AddF(34.8);
123+
/* Third example */
124+
CList<double> myList3 = new CList<double>();
125+
myList3.AddF(12.5);
126+
myList3.AddF(23.8);
127+
myList3.AddF(34.8);
128128

129-
try
130-
{
131-
myList3.RemoveI(-1);
132-
}
133-
catch(Exception ex) { Console.WriteLine(ex.Message); }
134-
try
135-
{
136-
myList3.RemoveI(3);
137-
}
138-
catch (Exception ex) { Console.WriteLine(ex.Message); }
139-
Console.WriteLine("Before removing element at index 1:");
140-
Console.WriteLine(myList3);
141-
myList3.RemoveI(1);
142-
Console.WriteLine("After removing element at index 1:");
143-
Console.WriteLine(myList3);
144-
myList3.RemoveI(0);
145-
Console.WriteLine("After removing element at index 0:");
146-
Console.WriteLine(myList3);
129+
try
130+
{
131+
myList3.RemoveI(-1);
132+
}
133+
catch (Exception ex)
134+
{
135+
Console.WriteLine(ex.Message);
147136
}
137+
try
138+
{
139+
myList3.RemoveI(3);
140+
}
141+
catch (Exception ex)
142+
{
143+
Console.WriteLine(ex.Message);
144+
}
145+
Console.WriteLine(
146+
"Before removing element at index 1:"
147+
);
148+
Console.WriteLine(myList3);
149+
myList3.RemoveI(1);
150+
Console.WriteLine("After removing element at index 1:");
151+
Console.WriteLine(myList3);
152+
myList3.RemoveI(0);
153+
Console.WriteLine("After removing element at index 0:");
154+
Console.WriteLine(myList3);
155+
}
148156
}

source/code/projects/CList_ICollection/CList_ICollection/CList.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,7 @@ public void Add(T value)
5656
);
5757
}
5858
Cell cCell = first;
59-
if (cCell != null)
60-
{
61-
while (cCell.Next != null)
62-
// As long as the cCell Cell has a neighbour…
63-
{
64-
cCell = cCell.Next;
65-
// We move the cCell cell to this neighbour.
66-
}
67-
cCell.Next = new Cell(value, null);
68-
}
69-
else
70-
{
71-
first = new Cell(value, null);
72-
}
59+
first = new Cell(value, first);
7360
}
7461

7562
public void Clear()
@@ -118,6 +105,9 @@ public void CopyTo(T[] array, int arrayIndex)
118105
}
119106
}
120107

108+
// Remove the first node containing the value
109+
// if it exists and returns true,
110+
// returns false otherwise.
121111
public bool Remove(T value)
122112
{
123113
if (isReadonly)
@@ -137,13 +127,17 @@ public bool Remove(T value)
137127
else
138128
{
139129
Cell cCell = first;
140-
while (cCell.Next != null)
130+
while (cCell.Next != null && !removed)
141131
{
142132
if (cCell.Next.Data.Equals(value))
143133
{
144134
cCell.Next = cCell.Next.Next;
145135
removed = true;
146136
}
137+
else
138+
{
139+
cCell = cCell.Next;
140+
}
147141
}
148142
}
149143
}

source/code/projects/CList_ICollection/CList_ICollection/Program.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ static void Main(string[] args)
66
{
77
/* Simple example. */
88
CList<int> myList1 = new CList<int>();
9-
Console.WriteLine(myList1);
109
myList1.Add(1);
1110
myList1.Add(5);
1211
myList1.Add(2);
@@ -20,21 +19,43 @@ static void Main(string[] args)
2019
myList1.Add(5);
2120
myList1.Add(2);
2221
myList1.Add(2);
23-
Console.WriteLine(myList1);
2422

2523
// A benefit of realizing the ICollection interface
2624
// is that we can iterate over elements of lists now:
2725
foreach (var item in myList1)
2826
{
29-
Console.WriteLine(item);
27+
Console.Write(item + " ");
3028
}
29+
Console.WriteLine("");
30+
31+
Console.Write(
32+
"We removed an occurence of 1: "
33+
+ myList1.Remove(1)
34+
+ ".\n"
35+
);
36+
foreach (var item in myList1)
37+
{
38+
Console.Write(item + " ");
39+
}
40+
Console.WriteLine("");
41+
42+
Console.Write(
43+
"We removed an occurence of 10: "
44+
+ myList1.Remove(10)
45+
+ ".\n"
46+
);
47+
foreach (var item in myList1)
48+
{
49+
Console.Write(item + " ");
50+
}
51+
Console.WriteLine("");
3152

3253
Console.WriteLine(
33-
"Our list is read-only:" + myList1.IsReadOnly + "."
54+
"Our list is read-only: " + myList1.IsReadOnly + "."
3455
);
3556
myList1.IsReadOnly = true;
3657
Console.WriteLine(
37-
"Our list is read-only:" + myList1.IsReadOnly + "."
58+
"Our list is read-only: " + myList1.IsReadOnly + "."
3859
);
3960
try
4061
{
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
5+
public class DLList<T> : ICollection<T>
6+
{
7+
private Cell head;
8+
private Cell tail;
9+
10+
public DLList()
11+
{
12+
head = null;
13+
tail = null;
14+
}
15+
16+
private class Cell
17+
{
18+
public T Data { get; set; }
19+
public Cell Next { get; set; }
20+
public Cell Previous { get; set; }
21+
22+
public Cell(
23+
T dataP = default(T),
24+
Cell previousP = null,
25+
Cell nextP = null
26+
) // We provide default values
27+
{
28+
Data = dataP;
29+
Previous = previousP;
30+
Next = nextP;
31+
}
32+
}
33+
34+
// Empty
35+
public bool IsEmpty()
36+
{
37+
return head == null;
38+
}
39+
40+
// Add "to the right",
41+
// at the end of the list.
42+
public void Add(T value)
43+
{
44+
if (isReadonly)
45+
{
46+
throw new InvalidOperationException(
47+
"List is read-only."
48+
);
49+
}
50+
Cell cCell = head;
51+
if (cCell != null)
52+
{
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;
62+
}
63+
else
64+
{
65+
head = new Cell(value, null);
66+
tail = head;
67+
}
68+
}
69+
70+
public void Clear()
71+
{
72+
head = null;
73+
tail = null;
74+
}
75+
76+
public bool Contains(T value)
77+
{
78+
bool found = false;
79+
Cell cCell = head;
80+
while (cCell != null && !found)
81+
{
82+
if (cCell.Data.Equals(value))
83+
{
84+
found = true;
85+
}
86+
cCell = cCell.Next;
87+
}
88+
return found;
89+
}
90+
91+
// Copies the elements of the ICollection to an Array, starting at a particular Array index.
92+
public void CopyTo(T[] array, int arrayIndex)
93+
{
94+
if (array == null)
95+
throw new ArgumentNullException(
96+
"The array cannot be null."
97+
);
98+
if (arrayIndex < 0)
99+
throw new ArgumentOutOfRangeException(
100+
"The starting array index cannot be negative."
101+
);
102+
if (Count > array.Length - arrayIndex)
103+
throw new ArgumentException(
104+
"The destination array has fewer elements than the collection."
105+
);
106+
107+
Cell cCell = head;
108+
int i = 0; // keeping track of how many elements were copied.
109+
while (cCell != null)
110+
{
111+
array[i + arrayIndex] = cCell.Data;
112+
i++;
113+
cCell = cCell.Next;
114+
}
115+
}
116+
117+
public bool Remove(T value)
118+
{
119+
if (isReadonly)
120+
{
121+
throw new InvalidOperationException(
122+
"List is read-only"
123+
);
124+
}
125+
bool removed = false;
126+
if (!IsEmpty())
127+
{
128+
if (head.Data.Equals(value))
129+
{
130+
head = head.Next;
131+
if (head != null)
132+
{
133+
head.Previous = null;
134+
}
135+
removed = true;
136+
}
137+
else
138+
{
139+
Cell cCell = head;
140+
while (cCell.Next != null)
141+
{
142+
if (cCell.Next.Data.Equals(value))
143+
{
144+
cCell.Next.Previous = cCell.Previous;
145+
146+
cCell.Next = cCell.Next.Next;
147+
removed = true;
148+
}
149+
}
150+
}
151+
}
152+
return removed;
153+
}
154+
155+
public int Count
156+
{
157+
get
158+
{
159+
int size = 0;
160+
Cell cCell = head;
161+
while (cCell != null)
162+
{
163+
cCell = cCell.Next;
164+
size++;
165+
}
166+
return size;
167+
}
168+
}
169+
170+
public bool isReadonly = false;
171+
public bool IsReadOnly
172+
{
173+
get { return isReadonly; }
174+
set { isReadonly = value; }
175+
}
176+
177+
public IEnumerator<T> GetEnumerator()
178+
{
179+
Cell cCell = head;
180+
while (cCell != null)
181+
{
182+
yield return cCell.Data;
183+
cCell = cCell.Next;
184+
}
185+
}
186+
187+
IEnumerator IEnumerable.GetEnumerator()
188+
{
189+
return this.GetEnumerator(); // call the generic version of the method
190+
}
191+
192+
/* We are done realizing the ICollection class. */
193+
194+
public override string ToString()
195+
{
196+
string returned = "———";
197+
// Line above the table
198+
for (int i = 0; i < Count; i++)
199+
{
200+
returned += "————";
201+
}
202+
returned += "\n| ";
203+
// Content of the CList
204+
Cell cCell = head;
205+
while (cCell != null)
206+
{
207+
returned += $"{cCell.Data} | ";
208+
cCell = cCell.Next;
209+
}
210+
returned += "\n———";
211+
// Line below the table
212+
for (int i = 0; i < Count; i++)
213+
{
214+
returned += "————";
215+
}
216+
if (Count > 0)
217+
{
218+
returned += $"\nHead: {head.Data}\n";
219+
returned += $"Tail: {tail.Data}\n";
220+
}
221+
return returned;
222+
}
223+
}

0 commit comments

Comments
 (0)