|
2 | 2 |
|
3 | 3 | public class CList<T> |
4 | 4 | { |
5 | | - // A CList is … a Cell. |
6 | | - private Cell first; |
| 5 | + // A CList is … a Cell. |
| 6 | + private Cell first; |
7 | 7 |
|
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 | + } |
13 | 13 |
|
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; } |
22 | 22 |
|
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; |
28 | 27 | } |
| 28 | + } |
29 | 29 |
|
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". |
33 | 33 |
|
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 | + } |
38 | 38 |
|
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 | + } |
46 | 46 |
|
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 |
51 | 55 | { |
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); |
66 | 65 | } |
| 66 | + } |
67 | 67 |
|
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 |
70 | 72 | { |
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 | + } |
85 | 85 |
|
86 | 86 | // We can implement a ToString method |
87 | 87 | // "the usual way", using a loop |
@@ -179,20 +179,31 @@ public void RemoveL() |
179 | 179 | // Method to remove the nth element if it exists. |
180 | 180 | public void RemoveI(int index) |
181 | 181 | { |
182 | | - if (index > Size) |
| 182 | + if (index > Size || index < 0) |
183 | 183 | { |
184 | 184 | throw new IndexOutOfRangeException(); |
185 | 185 | } |
186 | 186 | else // Some IDE will flag this "else" as redundant. |
187 | 187 | { |
188 | | - int counter = 0; |
189 | | - Cell cCell = first; |
190 | | - while (counter < index - 1) |
| 188 | + if (index == 0) |
191 | 189 | { |
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; |
194 | 206 | } |
195 | | - cCell.Next = cCell.Next.Next; |
196 | 207 | } |
197 | 208 | } |
198 | 209 |
|
|
0 commit comments