Skip to main content
ICT
Lesson AB29 - Linked List
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

LAB ASSIGNMENT AB29.1 page 14 of 16

List1

Background:

  1. The lesson contains a program that stored a linked list of five nodes, but in reverse order. The objective of this lab assignment is to create a linked list with the nodes assembled in the order that they are generated. If a loop is used to create the values 1-5, the resulting list should look like this:

    The next value to be inserted would go at the right end of the list. To avoid traversing the entire list each time, two references will provide access at the front (first = left end) and the end (last = right end) of the list.

  2. Your lab work can begin with the example programs from the lesson: ListDemo.java, SinglyLinkedList.java, and ListNode.java.

  3. An additional instance variable, last, should be added to the SinglyLinkedList class to supply direct access to the last node in the list. The constructor should also be modified to initialize the variable.

  4. Create an addLast method to create and add a ListNode (found in ListNode.java) to the end of the list. The method will have to deal with two cases this time, an empty list case and the general case of adding to the end. A two case solution is required because the links are different for each case. The following code/pseudocode is provided:

    void addLast(Object value)
    // Takes in value, creates a new node, adds the new node
    // at the end of the list.
    {
      if an empty list then
        set both first and last to reference the newly constructed node
      else
        construct a new node at the end of the list
    }

  5. Add a getLast method to return the last element of the list.

  6. Add a size method that returns a count of the number of nodes in the list.

  7. Adjust the code in the other routines as needed.

Assignment:

  1. Using the guidelines described in the Background section above, code a linked list that stores the 20 integers from 1-20 in ascending order.

  2. After the list is created, call getLast to display the contents of the last node to the screen.

  3. Call printList to traverse the list and print out the 20 numbers in one line on the screen.

  4. Use the size method to display the number of nodes in the list.

Instructions:

  1. Turn in your source code and a printed run output.

  2. The run output will consist of the value of the first node in the list, the value of the last node in the list, 20 numbers and a count of how many nodes are in the list. An example run output is shown below:

    First Element: 1
    Last Element: 20
    SinglyLinkedList: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
    Nodes: 20

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.