|
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 |
| - } |
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; } |
22 |
| - |
23 |
| - public Cell(T dataP, Cell nextP) |
| 8 | + // By default, a CList contains only an empty cell. |
| 9 | + public CList() |
24 | 10 | {
|
25 |
| - Data = dataP; |
26 |
| - Next = nextP; |
| 11 | + first = null; |
27 | 12 | }
|
28 |
| - } |
29 | 13 |
|
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". |
| 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; } |
33 | 22 |
|
34 |
| - public void AddF(T dataP) |
35 |
| - { |
36 |
| - first = new Cell(dataP, first); |
37 |
| - } |
| 23 | + public Cell(T dataP, Cell nextP) |
| 24 | + { |
| 25 | + Data = dataP; |
| 26 | + Next = nextP; |
| 27 | + } |
| 28 | + } |
38 | 29 |
|
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 |
| - } |
| 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". |
46 | 33 |
|
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 |
| 34 | + public void AddF(T dataP) |
55 | 35 | {
|
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); |
| 36 | + first = new Cell(dataP, first); |
65 | 37 | }
|
66 |
| - } |
67 | 38 |
|
68 |
| - // Property for the size of the CList. |
69 |
| - public int Size |
70 |
| - { |
71 |
| - get |
| 39 | + // We will frequently test if |
| 40 | + // a CList is empty, so we introduce |
| 41 | + // a method for that: |
| 42 | + public bool IsEmpty() |
72 | 43 | {
|
73 |
| - int size; |
74 |
| - if (IsEmpty()) |
75 |
| - { |
76 |
| - size = 0; |
77 |
| - } |
78 |
| - else |
79 |
| - { |
80 |
| - size = 1; |
81 |
| - Cell cCell = first; |
82 |
| - while (cCell.Next != null) |
83 |
| - // As long as the cCell Cell has a neighbour… |
| 44 | + return (first == null); |
| 45 | + } |
| 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) |
| 51 | + { |
| 52 | + if (IsEmpty()) |
| 53 | + AddF(dataP); |
| 54 | + else |
84 | 55 | {
|
85 |
| - cCell = cCell.Next; |
86 |
| - // We move the cCell cell to this neighbour. |
87 |
| - size++; |
| 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); |
88 | 65 | }
|
89 |
| - } |
90 |
| - return size; |
91 | 66 | }
|
92 |
| - } |
| 67 | + |
| 68 | + // Property for the size of the CList. |
| 69 | + public int Size |
| 70 | + { |
| 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 | + } |
93 | 85 |
|
94 | 86 | // We can implement a ToString method
|
95 | 87 | // "the usual way", using a loop
|
|
0 commit comments