CSci 2101 Data Structures: Lab 4
For this lab we will be using the implementation of LinkedList class
that we did in class. Please copy/paste the code for Node.java,
LinkedList.java, and TestLinkedLists.java from this page.
Problem 1 (paper-and-pencil)
For the following program draw the memory picture at two points of the
program execution: right before each of
the print statement gets executed. Based on your picture, what would
each of the statements print? Run the
program to check your result. If they are different from what your
pictures predict, correct the pictures.
public class WhatHappens {
public static void main(String [] args) {
Node n1 = new Node(1);
Node n2 = new Node(2);
Node n3 = n1;
n3.setNext(n2);
System.out.println( (n1.getNext()).getData() );
n3 = new Node(3);
(n1.getNext()).setNext(n3);
System.out.println( (n2.getNext()).getData() );
}
}
Submit the two memory pictures (on paper). You don't need to submit
any files for this problem.
Problem 2
In the file TestLinkedLists.java write a
loop to count the number of nodes of the list with data value
5. Test the program, submit ALL your test results.
Problem 3
In the file TestLinkedLists.java write a
loop to find the sum of all elements of a linked list. Test the
program carefully, submit all your test cases.
This is a lab from CSci 2101 course.