Multiple Choices
-
Put a checkmark in the box corresponding to true statements about the List abstract data type.
- A list contains an finite collection of elements, in a particular order.
- A list cannot contain multiple elements with the same value.
- A list must have a fixed number of elements.
- A list is generally endowed with an operation to test for emptiness.
- Only the element at the beginning of a list can be removed.
Comments on the solution
- A list cannot contain an infinite collection of elements.
- A list can have repetition, the same value can be present multiple times.
- At any given time, a list has a fixed size.
- This is generally the case in definitions of lists.
- This restriction applies to queues or stacks (depending how “beginning” is interpreted), but not to lists.
Exercises
-
Given the usual implementation of
Cell
andCList
:public class CList<T>{ private Cell first; private class Cell{ public T Data { get; set; } public Cell Next { get; set; } public Cell(T dataP, Cell nextP){Data = dataP; Next = nextP;} } public CList(){first = null;} }
Write…
-
… a
IsEmpty
property that istrue
if theCList
calling object is empty.Solution
Note that the question asks for a property:
public bool IsEmpty{ get{ return first == null; } }
-
… the
AddF
method that add a cell at the beginning of the CList (to the left).Solution
The key is to use the given
Cell
constructor to create the new element:public void AddF(T dataP){ first = new Cell(dataP, first); }
-
… a series of statements, to be inserted in a
Main
method, that a. create aCList
object capable of containingchar
, b. insert the elements'b'
and'/'
in it, c. displays whether it is empty usingIsEmpty
.Solution
Remembering that
IsEmpty
is a property, we obtain:CList<char> myList1 = new CList<char>(); myList1.AddF('b'); myList1.AddF('/'); Console.WriteLine("myList1 is empty:" + myList1.IsEmpty);
-
-
Briefly explain the purpose of the
IsReadonly
property from theICollection<T>
interface, and list at least two methods in a List implementation realizingICollection<T>
that should use it.Solution
This property indicates whether the
ICollection<T>
is read-only: if set totrue
, theICollection<T>
object should not accept addition or removal of elements. Hence, any method involving adding (AddF
,AddL
, …) or removing (Clear
,RemoveF
,RemoveL
,RemoveI
, …) values should test whetherIsReadonly
istrue
before proceeding. -
Explain the main differences between singly linked list and doubly linked list, and name a few methods that need to be implemented differently.
Solution
Doubly linked lists use a
Cell
class that contains two references: in addition to containing a reference to theCell
coming “after” themselves, as in singly linked lists, they also contain a reference to theCell
that is “before” them. This also requires to manipulate two references for the list: in addition to one reference to the first element (now calledHead
), as in singly linked list, they contain a reference to the “last” element (calledTail
).Clearing the list, adding and removing an element need to be implemented differently, as more references need to be updated.
-
For what operation(s) does doubly linked list provide a complexity gain over singly linked list?
Solution
Inserting at the end of the list is for doubly linked list, but for singly linked list. In general, traversing the list in reverse order is less costly if the list is doubly linked.