The program

This program is an exercise in generic (singly) linked lists. You will write a parameterized list class with just a few operations. Then you will create lists from this class using two different classes for the type parameter creating a "list of lists". You will write a driver program which will create and display a list of lists. Each (inner) list will have a name and contain integers. Running the program may look like: See image.

Internals

Your program will have three classes: GList, Header, and a main class. The Header class is used to store a String (as a name for an inner list) and an instance of GList, for an inner list of integers. In your main class then you will create an (outer) list of Header.

Class GList

This class will have an inner GNode > class with a data field of type T and a next field for linking, a constructor (taking a T parameter to initialize the data field) and get and set methods for both fields. You may use the GNode class which we wrote in class for this.

GList will have two (private, of course) fields, both of type GNode >: head and cursor. The headfield will simply be a head pointer for the linked list. The cursorfield will be used to point to nodes in the list. The (only) public methods in this class are:

  • public void insertFirst(T t): puts a new node containing t as a new first element of the list and makes cursor point to this node.
  • public void insertNext(T t): puts a new node containing t into the list after the node pointed to by cursor and makes cursor point to this node.
  • public T getFirstItem(): points cursor to the first node and returns the value stored in that node.
  • public T getNextItem(): advances cursor one place in the list and returns the value stored at that node.
  • public boolean hasNextItem(): returns whether or not the node pointed to by cursor is followed by another node.

A list can then be traversed by methods outside of the GNode class by first calling getFirstItem() once and then calling getNextItem() while hasNextItem() returnstrue. A linked list can be built in a similar manner using insertFirst(T t) and insertNext(T t).

Class Header

This class is: See image.

Notice that the type passed for the type parameter is Integer. Integer is the wrapper class for int. It must be used to store integers since int is a primary type and we can only store classes in a parameterized class. Because of "auto-boxing" and "auto-unboxing" you can assign int values to Integerand read Integer values as ints.

You can now make lists of (inner) lists where each inner list has a name by creating a list of type GList.

The main class

In your main you will create GList

list; In a loop you will ask the user if he or she wishes to create a list. As long as the user answers yes you will create new inner lists by adding nodes to list. To create an inner list you will first ask the user for a name for this inner list (to be stored in the name field of Header) and you will then ask the user, in another loop, if he or she wishes to add a number. As long as the user wishes to add numbers to the list the program will accept them. Your program should generate output similar to the example shown above.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.