Assignment 9

CSIS 10B
Assignment 9 Linked Lists
Due: next Wed
Assignment (10 points) Do either Basic or Advanced.
Basic -- do all these exercises in Lab9 download (in mod9)
Part I -- Developing non-encapsulated linked list examples
a.
b.
c.
d.
Drawing linked lists based on code
Modifying linked lists directly
changing node data
Begininning an encapsulated linked list class (StrLinkedList)
Part II -- Implementing List Methods
1) Finish the StrLinkedList class and test
2) count Make a method to count the number of occurrences of a certain string within the
nodes of a list. As an example, if your list contains:
first -> ant->rat->frog->bat->rat->bat->cat->rat->null
the method call list.count("rat") will return the value 3, while list.count("bat") will return
the value 2. Do this by creating a node pointer p and traversing the list, adding one to a
counter variable every time the node data equals the target value.
3) InsertABeforeB(String a, String b) Write a method that for every String b found in the list,
String a is inserted before it. We are going to do this two ways. First--only use pointers to
traverse the list and create nodes to insert manually. As a challenge, you are not allowed to
use ANY OTHER METHODS OF StrLinkedList. As an example, suppose your list contains:
first -> ant->rat->frog->bat->rat->bat->cat->rat->null
and the method list.insertABeforeB("dog","rat") is invoked, the list will be modified to this:
first -> ant->dog->rat->frog->bat->dog->rat->bat->cat->dog->rat->null
To solve this problem, create a node reference (pointer) p and step through the list with a
while loop. Whenever p.next.data (data in the node after p) equals the value in parameter
b, a new node will be created with its data set to parameter a and inserted into the list
following the node referred to by p.
4) InsertABeforeB2(String a, String b) Since working with object references as pointers can be
confusing, it is often helpful to build capabilities on top of existing features. To demonstrate
that, we are now going to repeat problem 3 only now the challenge is, NO POINTERS! NO
SLLNodes! This version can ONLY USE METHODS WE HAVE ALREADY DEFINED such as size,
get, and add. In other words, this method will not use pointers at all but use the existing
methods of the StrLinkedList class. This is how we can abstract out of complicated and
messy coding situations (working with references/pointers) by simply creating methods that
do the fundamental building blocks we want to use for higher level processing (add, get,
remove).
5) Application Load a file of words into a java.util.LinkedList object by inserting them in sorted
order (in other words, you will "insertion sort" the data coming in from the file). To do this,
read each word from the file. Then, for each word, start a loop that examines (gets) each
word from the list, compares it with the word just read, and as long as the word read is
smaller than the word from the list. After this loop exits, insert the word at the next cell in
the list. When you are finished a print of your list of words will show them in sorted order
(it's not the best way to sort words but it works!)
Part III Short Answer (do these without using your java compiler! Just enter the answers into file)
6) Add statements to the bottom of your SLL_Demo_App program showing the sequence of
values in the lists produced by the following statements. Solve this by drawing pictures of
the nodes and how they are linked together. Then type the final sequence into your
program file.
a) list.first = new SLLNode("a", null);
list.first = new SLLNode ("b", list.first);
b) list.first = new SLLNode ( "a", null);
list.first = new SLLNode ( "b", new SLLNode ("c", list.first));
c) list.first = new SLLNode ( "d", new SLLNode ("c", new SLLNode ( "b", null)));
list.first.next.next.next= new SLLNode ( 20, null) ;
7) Suppose we have a linked list of the following values, and SLLNode references n1 and n2 as
shown
n1
n2
|
|
V
V
list.first-> 10 -> 20 -> 50 -> 2 -> null
Add more answers to your program file that indicate what is printed by the following
statements--or indicate why the statements will fail (each problem reverts to list as shown
above):
a) System.out.println( n1.next.element);
b) System.out.println( n2.next.next. element);
c) n2.setData( n1.next);
list.printFirstToLast();
d) n2.setData( n1.next.next.data );
list.printFirstToLast();
e) n1.setNext( new SLLNode(21, n1.next);
list.printFirstToLast();
8) Extra Credit (1 point): make a reverse method for the StrLinkedList class that reverses the
list by rearranging pointers so that the tail node ends up at the beginning and the first node
ends up at the tail. There are easier ways to do this but I'd like to see you use the "three
pointers" method:
Run three pointers (front, middle, back) down the list in order, starting at head: front is on
one node, middle is just behind it, and back in turn is one behind. Once the code to run the
three pointers down the list is clear and tested in a drawing, add code to reverse the .next
pointer of the middle node during the iteration.
Also, add code to take care of the empty list and to adjust the first pointer itself.
Advanced: Doubly Linked Lists with Dummy Nodes (Text, p215)