|
| 1 | +--- |
| 2 | +tags: |
| 3 | + - datatypes/collections |
| 4 | +--- |
| 5 | + |
| 6 | +# Lists (Solutions) |
| 7 | + |
| 8 | +## Multiple Choices |
| 9 | + |
| 10 | +#. Put a checkmark in the box corresponding to true statements about the List abstract data type. |
| 11 | + |
| 12 | + - [ ] A list contains an finite collection of elements, in a particular order. |
| 13 | + - [ ] A list cannot contain multiple elements with the same value. |
| 14 | + - [x] A list must have a fixed number of elements. |
| 15 | + - [x] A list is generally endowed with an operation to test for emptiness. |
| 16 | + - [ ] Only the element at the beginning of a list can be removed. |
| 17 | + |
| 18 | + |
| 19 | + <details> |
| 20 | + <summary>Comments on the solution</summary> |
| 21 | + |
| 22 | + - A list cannot contain an *infinite* collection of elements. |
| 23 | + - A list can have repetition, the same value can be present multiple times. |
| 24 | + - At any given time, a list has a fixed size. |
| 25 | + - This is *generally* the case in definitions of lists. |
| 26 | + - This restriction applies to queues or stacks (depending how "beginning" is interpreted), but not to lists. |
| 27 | + </details> |
| 28 | + |
| 29 | + |
| 30 | +## Exercises |
| 31 | + |
| 32 | +#. Given the usual implementation of `Cell` and `CList`: |
| 33 | + |
| 34 | + ``` |
| 35 | + public class CList<T>{ |
| 36 | + private Cell first; |
| 37 | + private class Cell{ |
| 38 | + public T Data { get; set; } |
| 39 | + public Cell Next { get; set; } |
| 40 | + public Cell(T dataP, Cell nextP){Data = dataP; Next = nextP;} |
| 41 | + } |
| 42 | + public CList(){first = null;} |
| 43 | + } |
| 44 | + ``` |
| 45 | + Write… |
| 46 | + |
| 47 | + #. … a `IsEmpty` property that is `true` if the `CList` calling object is empty. |
| 48 | + |
| 49 | + <details> |
| 50 | + <summary>Solution</summary> |
| 51 | + |
| 52 | + Note that the question asks for a *property*: |
| 53 | + |
| 54 | + ``` |
| 55 | + public bool IsEmpty{ |
| 56 | + get{ return first == null; } |
| 57 | + } |
| 58 | + ``` |
| 59 | + </details> |
| 60 | + |
| 61 | + #. … the `AddF` method that add a cell at the beginning of the CList (to the left). |
| 62 | + |
| 63 | + <details> |
| 64 | + <summary>Solution</summary> |
| 65 | + |
| 66 | + The key is to use the given `Cell` constructor to create the new element: |
| 67 | + |
| 68 | + ``` |
| 69 | + public void AddF(T dataP){ |
| 70 | + first = new Cell(dataP, first); |
| 71 | + } |
| 72 | + ``` |
| 73 | + </details> |
| 74 | + |
| 75 | + #. … 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`. |
| 76 | + |
| 77 | + <details> |
| 78 | + <summary>Solution</summary> |
| 79 | + |
| 80 | + Remembering that `IsEmpty` is a property, we obtain: |
| 81 | + |
| 82 | + ``` |
| 83 | + CList<char> myList1 = new CList<char>(); |
| 84 | + myList1.AddF('b'); |
| 85 | + myList1.AddF('/'); |
| 86 | + Console.WriteLine("myList1 is empty:" + myList1.IsEmpty); |
| 87 | + ``` |
| 88 | + </details> |
| 89 | + |
| 90 | +#. 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. |
| 91 | + |
| 92 | + <details> |
| 93 | + <summary>Solution</summary> |
| 94 | + This property indicates whether the `ICollection<T>` is read-only: if set to `true`, the `ICollection<T>` object should not accept addition or removal of elements. |
| 95 | + Hence, any method involving adding (`AddF`, `AddL`, …) or removing (`Clear`, `RemoveF`, `RemoveL`, `RemoveI`, …) values should test whether `IsReadonly` is `true` before proceeding. |
| 96 | + </details> |
| 97 | + |
| 98 | +#. Explain the main differences between singly linked list and doubly linked list, and name a few methods that need to be implemented differently. |
| 99 | + |
| 100 | + <details> |
| 101 | + <summary>Solution</summary> |
| 102 | + Doubly linked lists use a `Cell` class that contains *two* references: in addition to containing a reference to the `Cell` coming "after" themselves, as in singly linked lists, they also contain a reference to the `Cell` that is "before" them. This also requires to manipulate two references for the list: in addition to one reference to the first element (now called `Head`), as in singly linked list, they contain a reference to the "last" element (called `Tail`). |
| 103 | + |
| 104 | + Clearing the list, adding and removing an element need to be implemented differently, as more references need to be updated. |
| 105 | + </details> |
| 106 | + |
| 107 | +#. For what operation(s) does doubly linked list provide a complexity gain over singly linked list? |
| 108 | + |
| 109 | + <details> |
| 110 | + <summary>Solution</summary> |
| 111 | + Inserting at the end of the list is $O(c)$ for doubly linked list, but $O(n)$ for singly linked list. |
| 112 | + In general, traversing the list in reverse order is less costly if the list is doubly linked. |
| 113 | + </details> |
0 commit comments