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.
-
Implementing a list as a doubly linked list (as opposed to singly linked list) allows to …
- use fewer attributes.
- keep track of the end of the list.
- store more elements.
- insert at the beginning of the list faster.
Exercises
-
Given the usual implementation of
CellandCList: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
IsEmptyproperty that istrueif theCListcalling object is empty.Solution
Note that the question asks for a property:
public bool IsEmpty{ get{ return first == null; } } -
… a
AddFmethod that takes an argument of typeTand adds it at the beginning (“to the left”) of theCListcalling object.Solution
The key is to use the given
Cellconstructor to create the new element:public void AddF(T dataP){ first = new Cell(dataP, first); } -
… a
RemoveLmethod that remove the value at the end (“to the right”) of theCListcalling object and returns it.Solution
public T RemoveL() { if (first == null) { throw new InvalidOperationException( "Cannot remove last cell from an empty list!." ); } T data; Cell cCell = first; while ( cCell.Next != null && cCell.Next.Next != null ) { cCell = cCell.Next; } data = cCell.Next.Data; cCell.Next = null; return data; } -
… a series of statements, to be inserted in a
Mainmethod, that a. create aCListobject capable of containingchar, b. insert the elements'b'and'/'in it, c. displays whether it is empty usingIsEmpty.Solution
Remembering that
IsEmptyis a property, we obtain:CList<char> myList1 = new CList<char>(); myList1.AddF('b'); myList1.AddF('/'); Console.WriteLine("myList1 is empty:" + myList1.IsEmpty); -
If the
AddFandRemoveLwere 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?Solution
It would be a queue.
-
-
Briefly explain the purpose of the
IsReadonlyproperty 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 whetherIsReadonlyistruebefore 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
Cellclass that contains two references: in addition to containing a reference to theCellcoming “after” themselves, as in singly linked lists, they also contain a reference to theCellthat 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.