Skip to content

Commit 4b694b5

Browse files
ClémentClément
authored andcommitted
Improving RemoveI method for CList class.
1 parent f314f08 commit 4b694b5

File tree

6 files changed

+384
-289
lines changed

6 files changed

+384
-289
lines changed

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

Lines changed: 86 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,86 +2,86 @@
22

33
public class CList<T>
44
{
5-
// A CList is … a Cell.
6-
private Cell first;
5+
// A CList is … a Cell.
6+
private Cell first;
77

8-
// By default, a CList contains only an empty cell.
9-
public CList()
10-
{
11-
first = null;
12-
}
8+
// By default, a CList contains only an empty cell.
9+
public CList()
10+
{
11+
first = null;
12+
}
1313

14-
// A Cell is itself two things:
15-
// - An element of data (of type T),
16-
// - Another cell, containing the next element of data.
17-
// We implement this using automatic properties:
18-
private class Cell
19-
{
20-
public T Data { get; set; }
21-
public Cell Next { get; set; }
14+
// A Cell is itself two things:
15+
// - An element of data (of type T),
16+
// - Another cell, containing the next element of data.
17+
// We implement this using automatic properties:
18+
private class Cell
19+
{
20+
public T Data { get; set; }
21+
public Cell Next { get; set; }
2222

23-
public Cell(T dataP, Cell nextP)
24-
{
25-
Data = dataP;
26-
Next = nextP;
27-
}
23+
public Cell(T dataP, Cell nextP)
24+
{
25+
Data = dataP;
26+
Next = nextP;
2827
}
28+
}
2929

30-
// A method to add a cell at the beginning
31-
// of the CList (to the left).
32-
// We call it AddF for "Add First".
30+
// A method to add a cell at the beginning
31+
// of the CList (to the left).
32+
// We call it AddF for "Add First".
3333

34-
public void AddF(T dataP)
35-
{
36-
first = new Cell(dataP, first);
37-
}
34+
public void AddF(T dataP)
35+
{
36+
first = new Cell(dataP, first);
37+
}
3838

39-
// We will frequently test if
40-
// a CList is empty, so we introduce
41-
// a method for that:
42-
public bool IsEmpty()
43-
{
44-
return (first == null);
45-
}
39+
// We will frequently test if
40+
// a CList is empty, so we introduce
41+
// a method for that:
42+
public bool IsEmpty()
43+
{
44+
return (first == null);
45+
}
4646

47-
// A method to add a cell at the end
48-
// of the CList (to the right).
49-
// We call it AddL for 'Add Last'.
50-
public void AddL(T dataP)
47+
// A method to add a cell at the end
48+
// of the CList (to the right).
49+
// We call it AddL for 'Add Last'.
50+
public void AddL(T dataP)
51+
{
52+
if (IsEmpty())
53+
AddF(dataP);
54+
else
5155
{
52-
if (IsEmpty())
53-
AddF(dataP);
54-
else
55-
{
56-
Cell cCell = first;
57-
while (cCell.Next != null)
58-
// As long as the cCell Cell has a neighbour…
59-
{
60-
cCell = cCell.Next;
61-
// We move the cCell cell to this neighbour.
62-
}
63-
// When we are done, we can insert the cell.
64-
cCell.Next = new Cell(dataP, null);
65-
}
56+
Cell cCell = first;
57+
while (cCell.Next != null)
58+
// As long as the cCell Cell has a neighbour…
59+
{
60+
cCell = cCell.Next;
61+
// We move the cCell cell to this neighbour.
62+
}
63+
// When we are done, we can insert the cell.
64+
cCell.Next = new Cell(dataP, null);
6665
}
66+
}
6767

68-
// Property for the size of the CList.
69-
public int Size
68+
// Property for the size of the CList.
69+
public int Size
70+
{
71+
get
7072
{
71-
get
72-
{
73-
int size = 0;
74-
Cell cCell = first;
75-
while (cCell != null)
76-
// As long as the cCell Cell has a neighbour…
77-
{
78-
cCell = cCell.Next;
79-
// We move the cCell cell to this neighbour.
80-
size++;
81-
}
82-
return size;
83-
}
84-
}
73+
int size = 0;
74+
Cell cCell = first;
75+
while (cCell != null)
76+
// As long as the cCell Cell has a neighbour…
77+
{
78+
cCell = cCell.Next;
79+
// We move the cCell cell to this neighbour.
80+
size++;
81+
}
82+
return size;
83+
}
84+
}
8585

8686
// We can implement a ToString method
8787
// "the usual way", using a loop
@@ -179,20 +179,31 @@ public void RemoveL()
179179
// Method to remove the nth element if it exists.
180180
public void RemoveI(int index)
181181
{
182-
if (index > Size)
182+
if (index > Size || index < 0)
183183
{
184184
throw new IndexOutOfRangeException();
185185
}
186186
else // Some IDE will flag this "else" as redundant.
187187
{
188-
int counter = 0;
189-
Cell cCell = first;
190-
while (counter < index - 1)
188+
if (index == 0)
191189
{
192-
cCell = cCell.Next;
193-
counter++;
190+
RemoveF();
191+
}
192+
else if (index == (Size - 1))
193+
{
194+
RemoveL();
195+
}
196+
else
197+
{
198+
int counter = 0;
199+
Cell cCell = first;
200+
while (counter < index - 1)
201+
{
202+
cCell = cCell.Next;
203+
counter++;
204+
}
205+
cCell.Next = cCell.Next.Next;
194206
}
195-
cCell.Next = cCell.Next.Next;
196207
}
197208
}
198209

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,30 @@ static void Main(string[] args)
119119
"Tried to access an element that doesn't exist."
120120
);
121121
}
122-
}
122+
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);
128+
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);
147+
}
123148
}

0 commit comments

Comments
 (0)