BuiltWithNOF
Test Prep

Given the above linked structure:     

1. Write a code segment to print the all the names in the list. Use System.out.println

2. Write a class to hold the data, including the constructor. Pass in 2 strings

3. Give a line of code to change Joe’s grade to B+

4. Write the several lines of code it would take to insert a node between Joe and Sue  (Sam, F)

5. Write the lines of code it would take to delete Sue from the list

6. Write a loop to print out the names of all the people with an A grade, assuming a much larger linked list.  Use .equals (if (s.equals(“A”))

7. Write code to add a new entry to the end of the list, assuming a much larger list.

8. Write code to add a new entry before the first person in the list with the name of Ed. (assume a larger list and Ed is in the list)

Answers:

New problems for this same list:

9. Write code to insert a new node (“Ned”,”C+”) as the third from the last entry, assuming at least 4 entries.

10.  Write code to count how many “B” grades there were and print the percent of B grades from the entire list

11.  Write code to insert (“Bob”,”B”) before the first score less than a C grade. Assume a much larger list and use .compareTo method (check sun doc site)

I will make another list soon with other similar questions.  Please post WebX questions, answer WebX questions, and email me directly with unresolved issues or code questions

 

 

 

All of these answers assume a class ListNode (you can call it what you want, but it must be consistent) defined in the answer for queston #2

1. ListNode temp = first
     while (temp!=null)
     {
             System.out.println(temp.name);
             temp = temp.next;
     }

2.   public class ListNode
       {
               String name;
               String grade;
               ListNode next;

               public ListNode(String n, String g)
               {
                         name = n;
                         grade = g;
                         next = null;
               }
       }

3. first.next.grade = “B+”;

4. temp = new ListNode(“Sam”,”F”);
     temp.next = first.next.next;
     first.next.next = temp;

5. first.next.next = first.next.next.next;

6. ListNode t = first;    // I am using t instead of temp.. my choice..
     while (t!=null)
     {
           if (t.grade.equals(“A”))
                 System.out.println(t.name);
           t = t.next;
     }

7. ListNode junk = first; // using junk this time.. why not?
     while (junk.next != null)   // look ahead one so you have a
           //pointer to the node BEFORE the last one.. This does not
           // include the possibility junk stats null...
     {
             junk = junk.next
     }        // at this point, junk points to the last node in the list.

     ListNode myNewOne = new ListNode(“Zack”,”C”);
     junk.next = myNewOne;  // what was null now points to Zack

8. ListNode temp = new ListNode(“Bob”,”B”); // I choose to add Bob..
     // now find Ed

     ListNode t = first;  // note I could not have used temp again.. Why?
     while (t.next.name.equals(“Ed”)) // find the node BEFORE Ed
     {
             t=t.next;
       }

     temp.next = t.next;    // now the new node (Bob) points to Ed’s node
     t.next = temp;   The node before Ed now points to Bob
     // now Bob is just before Ed in the List, assuming Ed is not the first
     // (can you fix this?) and Ed exists  (can you fix this?)
    

[Home] [Syllabus] [Sessions]