|
9 | 9 |
|
10 | 10 | ### Abstract Data Type |
11 | 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 | 12 | ### Difference with array |
32 | 13 |
|
33 | | -The `List` class serves a similar purpose than arrays, but with a few notable differences: |
34 | | - |
35 | | -- Lists do not need to have a number of elements fixed ahead of time, |
36 | | -- Lists automatically expand when elements are added, |
37 | | -- Lists automatically shrink when elements are removed, |
38 | | -- Lists require to have the `using System.Collections.Generic;` statement at the beginning of the file, |
39 | | -- Lists have many built-in methods. |
40 | | - |
41 | | -The [complete description of the `List` class](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-9.0) describes all the useful methods, we simply give a quick overview below. |
42 | | - |
43 | | -## Syntax |
44 | | - |
45 | | -We discuss here the "built-in" implementation of lists in C#. |
46 | | - |
47 | | -### Creation |
48 | | - |
49 | | -The syntax to create an empty list of `string` named `nameList` and a list of `int` named `valueList` containing 1, 2 and 3 is: |
50 | | - |
51 | | -```{download="./code/projects/Lists.zip"} |
52 | | -!include`snippetStart="// containing 1, 2 and 3.", snippetEnd="// We count the elements and add to the list of strings.", dedent=4` code/projects/Lists/Lists/Program.cs |
53 | | -``` |
54 | | - |
55 | | -### Adding Elements |
56 | | - |
57 | | -Adding an element to the list is done using the `Add` method, and counting the number of elements is done using the `Count` property: |
58 | | - |
59 | | -``` |
60 | | -!include`snippetStart="// We count the elements and add to the list of strings.", snippetEnd="// We can access the first element with [0].", dedent=4` code/projects/Lists/Lists/Program.cs |
61 | | -``` |
62 | | - |
63 | | -Note that we did not need to resize the `nameList` manually: its size went from 0 to 1 after we added "Bob", and from 1 to 2 after we added "Sandrine". |
64 | | - |
65 | | -### Accessing Elements |
66 | | - |
67 | | -#### Using the indexer access operator |
68 | | - |
69 | | -Accessing an element can be done using the same operator as with arrays (the `[]` operator, called ["indexer operator"](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#indexer-operator-)): |
70 | | - |
71 | | -``` |
72 | | -!include`snippetStart="// Reading it:", snippetEnd="// Writing it:", dedent=4` code/projects/Lists/Lists/Program.cs |
73 | | -``` |
74 | | - |
75 | | -will display "Bob". Note that this syntax can be used to change the value of an element that already exist. |
76 | | -For example, |
77 | | - |
78 | | -``` |
79 | | -!include`snippetStart="// Writing it:", snippetEnd="// The following is incorrect:", dedent=4` code/projects/Lists/Lists/Program.cs |
80 | | -``` |
81 | | - |
82 | | -would replace the first value in the list ("Bob") with "Robert". |
83 | | - |
84 | | -Note that while accessing or replacing an element using the `[]` operator inside a list is fine, *you cannot add new elements to the list using this syntax*. |
85 | | -For example, |
86 | | - |
87 | | -``` |
88 | | -nameList[2] = "Sandrine"; |
89 | | -``` |
90 | | - |
91 | | -would raise an exception since there is no third element to our list. |
92 | | - |
93 | | -#### Using `foreach` |
94 | | - |
95 | | -Another way of accessing the elements in a list is to use `foreach` loops: |
96 | | - |
97 | | -``` |
98 | | -!include`snippetStart="// Accessing using foreach:", snippetEnd="// Removing an element:", dedent=4` code/projects/Lists/Lists/Program.cs |
99 | | -``` |
100 | | - |
101 | | -### Removing Elements |
102 | | - |
103 | | -An element can be removed from the list using the `RemoveAt` method. |
104 | | -If `nameList` contains "Robert" and "Sandrine", then after the following statement, |
105 | | - |
106 | | -``` |
107 | | -!include`snippetStart="// Removing an element:", snippetEnd="// also shrink the list.", dedent=4` code/projects/Lists/Lists/Program.cs |
108 | | -``` |
109 | | - |
110 | | -it would only contain "Sandrine" and its size would be 1. That is, the first element would be deleted and the list would shrink. |
| 14 | +## Possible Implementation |
111 | 15 |
|
112 | | -Another way of removing an element is to use the `Remove` method. |
113 | | -Suppose we have the following list: |
114 | | - |
115 | | -``` |
116 | | -!include`snippetStart="// Creating a new example", snippetEnd="// and then removing the first 1 in it:", dedent=4` code/projects/Lists/Lists/Program.cs |
| 16 | +```{download="./code/projects/Tree.zip"} |
| 17 | +!include code/projects/Tree/Tree/BTree.cs |
117 | 18 | ``` |
118 | 19 |
|
119 | | -then using |
120 | | - |
121 | | -``` |
122 | | -!include`snippetStart="// and then removing the first 1 in it:", snippetEnd="// Testing if 0 was removed:", dedent=4` code/projects/Lists/Lists/Program.cs |
| 20 | +```{download="./code/projects/Tree.zip"} |
| 21 | +!include code/projects/Tree/Tree/BSTree.cs |
123 | 22 | ``` |
124 | 23 |
|
125 | | -would remove "1" from the list, and the list would become -1, 0, 2, 3, 2, 5. |
126 | | - |
127 | | -Observe that `Remove` returns a `bool`, so that for instance the following |
128 | | - |
129 | | -``` |
130 | | -!include`snippetStart="// Testing if 0 was removed:", snippetEnd="// and finally removing the first occurrence of 2:", dedent=4` code/projects/Lists/Lists/Program.cs |
131 | | -``` |
132 | | - |
133 | | -would not only remove 0 from the list, but also display "0 was removed". |
134 | | - |
135 | | -Finally, if the value is present multiple times in the list, then only its first occurrence is removed. |
136 | | -For example, if the list is -1, 2, 3, 2, 5, then after executing |
137 | | - |
138 | | -``` |
139 | | -!include`snippetStart="// and finally removing the first occurrence of 2:", snippetEnd="// so that the list contains:", dedent=4` code/projects/Lists/Lists/Program.cs |
140 | | -``` |
141 | 24 |
|
142 | | -it would become -1, 3, 2, 5: observe that only the first occurrence of 2 was removed from the list. |
0 commit comments