You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/solutions/data/lists.md
+47-3Lines changed: 47 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,6 +26,12 @@ tags:
26
26
- This restriction applies to queues or stacks (depending how "beginning" is interpreted), but not to lists.
27
27
</details>
28
28
29
+
#. Implementing a list as a doubly linked list (as opposed to singly linked list) allows to …
30
+
31
+
- [] use fewer attributes.
32
+
- [x] keep track of the end of the list.
33
+
- [] store more elements.
34
+
- [] insert at the beginning of the list faster.
29
35
30
36
## Exercises
31
37
@@ -41,7 +47,8 @@ tags:
41
47
}
42
48
public CList(){first = null;}
43
49
}
44
-
```
50
+
```
51
+
45
52
Write…
46
53
47
54
#. … a `IsEmpty` property that is `true` if the `CList` calling object is empty.
@@ -57,7 +64,7 @@ tags:
57
64
}
58
65
```
59
66
</details>
60
-
#. … the `AddF` method that add a cell at the beginning of the CList (to the left).
67
+
#. … a `AddF` method that takes an argument of type `T` and adds it at the beginning ("to the left") of the `CList` calling object.
61
68
62
69
<details>
63
70
<summary>Solution</summary>
@@ -70,6 +77,36 @@ tags:
70
77
}
71
78
```
72
79
</details>
80
+
#. … a `RemoveL` method that remove the value at the end ("to the right") of the `CList` calling object and returns it.
81
+
82
+
<details>
83
+
<summary>Solution</summary>
84
+
```
85
+
public T RemoveL()
86
+
{
87
+
if (first == null)
88
+
{
89
+
throw new InvalidOperationException(
90
+
"Cannot remove last cell from an empty list!."
91
+
);
92
+
}
93
+
T data;
94
+
95
+
Cell cCell = first;
96
+
while (
97
+
cCell.Next != null && cCell.Next.Next != null
98
+
)
99
+
{
100
+
cCell = cCell.Next;
101
+
}
102
+
data = cCell.Next.Data;
103
+
cCell.Next = null;
104
+
return data;
105
+
}
106
+
```
107
+
</details>
108
+
109
+
73
110
#. … a series of statements, to be inserted in a `Main` method, that a. create a `CList` object capable of containing `char`, b. insert the elements `'b'` and `'/'` in it, c. displays whether it is empty using `IsEmpty`.
74
111
75
112
<details>
@@ -84,7 +121,14 @@ tags:
84
121
Console.WriteLine("myList1 is empty:" + myList1.IsEmpty);
85
122
```
86
123
</details>
87
-
124
+
125
+
#. If the `AddF` and `RemoveL` were the only two methods to add to and to remove from the list, what would be the name of the data-structure we actually just implemented?
126
+
127
+
<details>
128
+
<summary>Solution</summary>
129
+
It would be a queue.
130
+
</details>
131
+
88
132
#. Briefly explain the purpose of the `IsReadonly` property from the `ICollection<T>` interface, and list at least two methods in a List implementation realizing `ICollection<T>` that should use it.
#. A queue is generally endowed with operations called…
11
+
12
+
- [ ] Requeue
13
+
- [x] Dequeue
14
+
- [x] Enqueue
15
+
- [ ] Unqueue
16
+
17
+
#. A queue implements which principle?
18
+
19
+
- [] LIFO
20
+
- [x] FIFO
21
+
- [] LILO
22
+
- [] FOFI
23
+
24
+
#. LIFO stands for…
25
+
26
+
- [] Least Is First Out
27
+
- [] Last Is First Outside
28
+
- [x] Last In First Out
29
+
- [] Low Input Fast Output
30
+
31
+
## Exercises
32
+
33
+
#. Suppose given an empty `Queue` object, and assume that we store the values 10 and 20 (in that order), and then remove one and insert 30. Draw the resulting queue, labelling explicitly the front (or beginning) and end of your queue.
#. A stack is generally endowed with operations called…
11
+
12
+
- [x] Pop
13
+
- [x] Push
14
+
- [] Pull
15
+
- [x] Peek
16
+
- [] Pounce
17
+
18
+
## Exercises
19
+
20
+
#. Suppose given an empty `Stack` object, and assume that we store the values 10 and 20 (in that order), and then remove one and insert 30. Draw the resulting stack, labelling explicitly the bottom and top of your stack.
0 commit comments