Saint Louis University |
Computer Science 2100
|
Dept. of Math & Computer Science |
Topic: Singly Linked Lists and Circular Lists
Related Reading: Ch. 3.2, 5.1.5, 5.2.5, and class notes
Due:
Friday, October 9, 10:00am
For this assignment, you must work individually.
Please make sure you adhere to the policies on academic integrity in this regard.
Our implementation of the LinkedStack destructor piggybacks on our existing implementation of the pop method as its way to delete all of the nodes allocated to a list which is being destroyed.
Give a more direct implementation of the LinkedStack::~LinkedStack() method that does not make calls to any other methods. You should instead use a loop to traverse the list while deleting nodes (and not losing track of the rest of the list as you go).
Examine the source code for our LinkedQueue implementation. That class maintains an instance variable, n, equal to the current number of nodes in the queue. This allows for the size() method to be easily implementing in O(1) time.
If the instance variable n did not exist, the size() method could be implemented using O(n) time by traversing the circular linked list to count the number of node. Give a valid such implementation of size().
Returning to the original source code for our LinkedQueue
implementation. Lines 47-50 of the pop() method includes a
conditional that differentiates between the case of popping the
last element from a queue. Consider a modification to that code
where we replace the entire four-line conditional, and instead
always execute the command
cursor->next = old->next;
in all cases.
What are the consequences of this change? Explain why the class would or would not work with such a modification.