CSci 2101 Data Structures: Lab 6

Problem 1

Part 1 (not graded). Consider this implementation of linked lists and the following program:

public class TestCounters {
    public static void main(String [] args) {
	LinkedList list = new LinkedList();

	for (int i = 1; i < 50; i = 2 * i) {
	    Node n = new Node(i);
	    list.addFront(n);
	}
	
	Node current = list.getFirst();
	while (current != null) {
	    System.out.println(current.getData());
	    current = current.getNext();
	}
	
    }
}
What will be printed by this program? Run the program to check your answer.

Part 2 (graded). In the class Node add a static counter for the constructor and a static counter for each of the other three methods. The counter for a method will be incremented every time the method gets called. Add a static method printCounters() to print out all of the counters. Add a call to printCounters() at the end of the program TestCounters.java to find out how many time each method was called. Write the results in comments and explain all the numbers.

This approach can be used to compare efficiency of two different implementations of the same data structure.


This is a lab from CSci 2101 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.