Skip to content

Commit 7e08598

Browse files
ClémentClément
authored andcommitted
Improved previous notes on lists.
1 parent f85e2f0 commit 7e08598

File tree

3 files changed

+92
-78
lines changed

3 files changed

+92
-78
lines changed

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

Lines changed: 66 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,94 +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-
}
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()
2410
{
25-
Data = dataP;
26-
Next = nextP;
11+
first = null;
2712
}
28-
}
2913

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; }
3322

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+
}
3829

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".
4633

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)
5535
{
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);
6537
}
66-
}
6738

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()
7243
{
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
8455
{
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);
8865
}
89-
}
90-
return size;
9166
}
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+
}
9385

9486
// We can implement a ToString method
9587
// "the usual way", using a loop

source/lectures/data/lists.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,29 @@ tags:
77

88
## Introduction
99

10+
### Abstract Data Type
11+
12+
Described [abstractly](./lectures/data/intro#abstract-data-types), [a list](https://en.wikipedia.org/wiki/List_(abstract_data_type)) is
13+
14+
- a finite collection of elements,
15+
- in a particular order,
16+
- that may contain the same element multiple times.
17+
18+
The fact that it may contain the same element multiple times makes it different from a set, the fact that it is ordered makes it different from a [multiset](https://en.wikipedia.org/wiki/Multiset).
19+
20+
Generally, it has operations to…
21+
22+
- … create an empty list,
23+
- … test for emptiness,
24+
- … add an element at "the beginning" of the list,
25+
- … add an element at "the end" of the list,
26+
- … remove an element at "the beginning" of the list,
27+
- … remove an element at "the end" of the list,
28+
- … access an element at index $i$.
29+
30+
31+
### Difference with array
32+
1033
The `List` class serves a similar purpose than arrays, but with a few notable differences:
1134

1235
- Lists do not need to have a number of elements fixed ahead of time,
@@ -19,6 +42,8 @@ The [complete description of the `List` class](https://learn.microsoft.com/en-us
1942

2043
## Syntax
2144

45+
We discuss here the "built-in" implementation of lists in C#.
46+
2247
### Creation
2348

2449
The syntax to create an empty list of `string` named `nameList` and a list of `int` named `valueList` containing 1, 2 and 3 is:

source/solutions/misc/complexity.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,11 @@
4848

4949
#. Write a code snippet (no need to include `using` statements or `Main` header) that displays the sum of all the values in a `score` `int` array that you can suppose declared and initialized. What is the worst case time complexity of the algorithm you wrote, relative to the size $n$ of the array `score`?
5050

51-
<details><summary>Solution</summary>
52-
```
51+
<details><summary>Solution</summary>```
5352
int sum = 0;
5453
for(int i = 0; i < score.Length; i++){sum += score[i];}
5554
Console.WriteLine($"The sum is {sum}.");
5655
```
5756

5857
This algorithm is linear: it goes through the array once.
5958
</detail>
60-
61-

0 commit comments

Comments
 (0)