CS 173 Introduction to Computer Science II Spring 2019

Homework #2

Task

Starting with the singly linked list (SLL) and doubly linked list (DLL) code we developed in class, complete the following methods:

  • (5 points) SLL: Write a non-static function, clear(), that operates on this singly linked list to remove all elements from the list. Verify your code works for lists of size 0, 1, and a larger number of items.
  • (5 points) DLL: Write a non-static function, clear(), that operates on this doubly linked list to remove all elements from the list. Verify your code works for lists of size 0, 1, and a larger number of items.
  • (10 points) SLL: Write a static iterative function (using a for or while loop), concat(), that takes two singly linked lists, say L1, and L2, as parameters and returns a new SLL containing the original items of L1 followed by the items of L2. You will need to remove the generic "" references from your function declaration. Add code to the main method demonstrating your function; you will need to invoke it as SinglyLinkedList.concat(...) Verify your code works for lists of size 0, 1, and a larger number of items. This method should be nondestructive, meaning the original list is not destroyed.
  • (10 points) DLL: Write a static recursive function, concat(), that takes two doubly linked lists, say L1, and L2, as parameters and returns a new DLL containing the original items of L1 followed by the items of L2. You will need to remove the generic "" references from your function declaration; this will cause a warning which you can ignore. Add code to the main method demonstrating your function; you will need to invoke it as DoublyLinkedList.concat(...) You may need a recursive helper function. Verify your code works for lists of size 0, 1, and a larger number of items. Consider using a recursive helper function where you have an additional node parameter that keeps track of the current position in the list. This method should be nondestructive, meaning the original list is not destroyed.
  • (10 points) SLL: Write a static recursive function, reverse(), that takes a singly linked list as a parameter and returns a new SLL containing the original items in reverse order. Add code to the main method demonstrating your function. You will need to remove the generic "" references from your function declaration. You may need a recursive helper function. Verify your code works for lists of size 0, 1, and a larger number of items. Consider using a recursive helper function where you have an additional node parameter that keeps track of the current position in the list. This method should be nondestructive, meaning the original list is not destroyed.

Deliverables

Send your code for the above problems as an attachment to an email to me.

References

  • Chapter 3 of our text.
  • Chapter 5 of our text.


Copyright © 2019, David A. Reimann. All rights reserved.