Beginning of implementation of linked lists. The Node class:

public class Node {
    private int data;
    private Node next;

    public Node(int d) {
	data = d;
	next = null; // can skip this line: null is the default value

    public int getData() {
	return data;

    // return the next node
    public Node getNext() {
	return next;

    // set the node 
    public void setNext(Node n) {
	next = n;

The linked list class: started in class on Friday, not complete yet. The two missing methods are AddFront and removeFront.

public class LinkedList {
    private Node first;

    public LinkedList() {
	first = null;

    public boolean isEmpty() {
	return (first == null);

    public Node getFirst() {
	return first;

    // to be continued...

The testing program for nodes. This is not the same program that we worked with in class. Draw the object diagram for this one. What will printed? Run the program to test your hypothesis.

public class TestLinkedLists {
    public static void main(String [] args) {
	// testing nodes:
	Node first = new Node(5);
	Node second = new Node(6);
	System.out.println( (first.getNext()).getData() );

This is an example 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.