Skip to content

Commit 40583f3

Browse files
ClémentClément
authored andcommitted
Adding exam #1 and its solution.
1 parent 40242df commit 40583f3

10 files changed

Lines changed: 391 additions & 3 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,32 @@ IEnumerator IEnumerable.GetEnumerator()
181181
}
182182

183183
/* We are done realizing the ICollection class. */
184+
185+
// One last method, to remove the last cell and
186+
// returns its value.
187+
public T RemoveL()
188+
{
189+
if (isReadonly)
190+
{
191+
throw new InvalidOperationException(
192+
"List is read-only"
193+
);
194+
}
195+
if (IsEmpty())
196+
{
197+
throw new InvalidOperationException(
198+
"Cannot remove last cell from an empty list!."
199+
);
200+
}
201+
T data;
202+
203+
Cell cCell = first;
204+
while (cCell.Next != null && cCell.Next.Next != null)
205+
{
206+
cCell = cCell.Next;
207+
}
208+
data = cCell.Next.Data;
209+
cCell.Next = null;
210+
return data;
211+
}
184212
}

source/code/projects/CList_ICollection/CList_ICollection/Program.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,5 +91,31 @@ static void Main(string[] args)
9191
{
9292
Console.Write(i + " ");
9393
}
94+
// Last, we test the RemoveL method
95+
myList1.IsReadOnly = false;
96+
Console.WriteLine("\nThe list contains:");
97+
foreach (int i in myList1)
98+
{
99+
Console.Write(i + " ");
100+
}
101+
for (int i = 0; i < myList1.Count; i++)
102+
{
103+
try
104+
{
105+
Console.WriteLine(
106+
"\nValue removed: " + myList1.RemoveL()
107+
);
108+
}
109+
catch (Exception e)
110+
{
111+
Console.WriteLine(e.Message);
112+
}
113+
}
114+
115+
Console.WriteLine("\nThe list contains:");
116+
foreach (int i in myList1)
117+
{
118+
Console.Write(i + " ");
119+
}
94120
}
95121
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
class Program
4+
{
5+
static void Main()
6+
{
7+
RBTree<int> btree = new RBTree<int>();
8+
btree.Insert(1);
9+
btree.Insert(2);
10+
btree.Insert(3);
11+
btree.Insert(4);
12+
}
13+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
public class RBTree<T>
5+
{
6+
private class Node
7+
{
8+
public T Data { get; set; }
9+
public Node left;
10+
public Node right;
11+
12+
public Node(
13+
T dataP = default(T),
14+
Node leftP = null,
15+
Node rightP = null
16+
)
17+
{
18+
Data = dataP;
19+
left = leftP;
20+
right = rightP;
21+
}
22+
}
23+
private Node root;
24+
25+
public RBTree()
26+
{
27+
root = null;
28+
}
29+
public void Insert(T dataP)
30+
{
31+
root = Insert(dataP, root);
32+
}
33+
34+
private Node Insert(T dataP, Node nodeP)
35+
{
36+
if (nodeP == null)
37+
{
38+
return new Node(dataP, null, null);
39+
}
40+
else
41+
{
42+
Random gen = new Random();
43+
if(gen.NextDouble() > 0.5)
44+
{
45+
nodeP.left = Insert(dataP, nodeP.left);
46+
}
47+
else
48+
{
49+
nodeP.right = Insert(dataP, nodeP.right);
50+
}
51+
}
52+
return nodeP;
53+
}
54+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
%%% The "random" binary tree obtained by inserting 1, 2, 3 and 4 (in that order).
2+
3+
A((1))-->B((2))
4+
A-->C((3))
5+
C~~~H1:::hidden
6+
C-->D((4))

source/order

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
./exercises/data/1darrays.md
9797
./exercises/data/2darrays.md
9898
./exercises/data/lists.md
99+
./exercises/data/stacks.md
100+
./exercises/data/queues.md
101+
./exercises/data/trees.md
99102
./exercises/control/
100103
./exercises/control/conditionals.md
101104
./exercises/control/iteratives.md

source/solutions/data/lists.md

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ tags:
2626
- This restriction applies to queues or stacks (depending how "beginning" is interpreted), but not to lists.
2727
</details>
2828

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.
2935

3036
## Exercises
3137

@@ -41,7 +47,8 @@ tags:
4147
}
4248
public CList(){first = null;}
4349
}
44-
```
50+
```
51+
4552
Write…
4653
4754
#. … a `IsEmpty` property that is `true` if the `CList` calling object is empty.
@@ -57,7 +64,7 @@ tags:
5764
}
5865
```
5966
</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.
6168

6269
<details>
6370
<summary>Solution</summary>
@@ -70,6 +77,36 @@ tags:
7077
}
7178
```
7279
</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+
73110
#. … 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`.
74111

75112
<details>
@@ -84,7 +121,14 @@ tags:
84121
Console.WriteLine("myList1 is empty:" + myList1.IsEmpty);
85122
```
86123
</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+
88132
#. 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.
89133

90134
<details>

source/solutions/data/queues.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
tags:
3+
- datatypes/collections
4+
---
5+
6+
# Queues (Solutions)
7+
8+
## Multiple Choices
9+
10+
#. 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.
34+
35+
<details>
36+
<summary>Solution</summary>
37+
We get: (end ->) 30 ; 20 (<- front).
38+
</details>

source/solutions/data/stacks.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
tags:
3+
- datatypes/collections
4+
---
5+
6+
# Lists (Solutions)
7+
8+
## Multiple Choices
9+
10+
#. 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.
21+
22+
<details>
23+
<summary>Solution</summary>
24+
We would obtain: (top ->) 30; 10 (<- bottom).
25+
</details>

0 commit comments

Comments
 (0)