Skip to content

Commit 087031c

Browse files
committed
Merge branch 'main' of github.com:princomp/princomp.github.io
2 parents e077ebb + 470577d commit 087031c

13 files changed

Lines changed: 492 additions & 49 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+
}

source/code/projects/Tree/Tree/BSTree.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ private Node Insert(T dataP, Node nodeP)
3333
return nodeP;
3434
}
3535

36-
// Finding into a BSTree
36+
// Finding into a BSTree
3737

38-
public override bool Find(T dataP)
38+
public override bool Find(T dataP)
39+
{
40+
bool found = false;
41+
if (root != null)
3942
{
40-
bool found = false;
41-
if (root != null)
42-
{
43-
found = Find(dataP, root);
44-
}
45-
return found;
43+
found = Find(dataP, root);
4644
}
45+
return found;
46+
}
4747

48-
private bool Find(T dataP, Node nodeP)
48+
private bool Find(T dataP, Node nodeP)
4949
{
5050
bool found = false;
5151
if (nodeP != null)

source/code/projects/Tree/Tree/BTree.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ public Node(
2121
left = leftP;
2222
right = rightP;
2323
}
24-
// Displaying a Node is only
25-
// displaying its data.
24+
25+
// Displaying a Node is only
26+
// displaying its data.
2627
public override string ToString()
2728
{
2829
return Data.ToString();
@@ -108,7 +109,7 @@ public virtual bool Find(T dataP)
108109
bool found = false;
109110
if (root != null)
110111
{
111-
found = Find(root, dataP);
112+
found = Find(dataP, root);
112113
}
113114
return found;
114115
}

source/code/projects/Tree/Tree/Program.cs

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,40 @@ static void Main()
77
BSTree<int> btree = new BSTree<int>();
88
Console.WriteLine(btree);
99

10-
/*
11-
* The purpose of this example is to illustrate that
12-
* the order in which values are inserted matter.
13-
*/
14-
btree.Clear();
15-
btree.Insert(10);
16-
btree.Insert(6);
17-
btree.Insert(13);
18-
btree.Insert(12);
19-
btree.Insert(14);
20-
Console.WriteLine(btree);
21-
btree.Clear();
22-
btree.Insert(14);
23-
btree.Insert(12);
24-
btree.Insert(13);
25-
btree.Insert(6);
26-
btree.Insert(10);
27-
Console.WriteLine(btree);
10+
/*
11+
* The purpose of this example is to illustrate that
12+
* the order in which values are inserted matter.
13+
*/
14+
btree.Clear();
15+
btree.Insert(10);
16+
btree.Insert(6);
17+
btree.Insert(13);
18+
btree.Insert(12);
19+
btree.Insert(14);
20+
Console.WriteLine(btree);
21+
btree.Clear();
22+
btree.Insert(14);
23+
btree.Insert(12);
24+
btree.Insert(13);
25+
btree.Insert(6);
26+
btree.Insert(10);
27+
Console.WriteLine(btree);
2828

29-
/* Second example, we begin by clearing the tree. */
29+
/* Second example, we begin by clearing the tree. */
3030

31-
btree.Clear();
32-
btree.Insert(10);
33-
// Attempting to insert the same value
34-
// twice raises an exception:
35-
try
36-
{
37-
btree.Insert(10);
38-
}
39-
catch(Exception e)
40-
{
41-
Console.WriteLine(e.Message);
42-
}
43-
btree.Insert(11);
31+
btree.Clear();
32+
btree.Insert(10);
33+
// Attempting to insert the same value
34+
// twice raises an exception:
35+
try
36+
{
37+
btree.Insert(10);
38+
}
39+
catch (Exception e)
40+
{
41+
Console.WriteLine(e.Message);
42+
}
43+
btree.Insert(11);
4444
btree.Insert(30);
4545
btree.Insert(7);
4646
Console.WriteLine(btree);
@@ -80,7 +80,5 @@ static void Main()
8080
{
8181
Console.WriteLine(i + " occurs: " + btree.Find(i));
8282
}
83-
84-
8583
}
8684
}
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>

0 commit comments

Comments
 (0)