Scientific Programming II

ModSim

Generics Tutorial


Node.java

// The <T> after the class name tells Java that this class
// uses generics. During program execution, the T will be replaced
// with the class being stored.
public class Node<T> 
{
    // Since our data is the thing we're storing, we'll
    // set it's type to T.
    private T data;
    private Node next;
   
    // All instances when we have 'int' to work with the data
    // should be changed to T.
    public Node(T value, Node ref) {
        data = value;
        next = ref;
    }
  
    public T getData() {
        return data;
    }
  
    public Node getNextReference() {
        return next;
    }
  
    public void setNextReference(Node value) {
        next = value;
    }
    
    public String toString()
    {
        return "Data: " + data;
    }
}

LinkedList.java

// As before, the T signifies the type we're storing.
public class LinkedList<T>
{
    private Node<T> head;
    private Node<T> tail;
    
    public LinkedList()
    {
        head = null;
        tail = null;
    }
 
    public void insertAtBack(T num)
    {
        if (head == null) {
            head = new Node(num, null);
            tail = head;
        } else {
            Node tempref = tail;
            tail = new Node(num, null);
            tempref.setNextReference(tail);
        }
    }
 
    public void insertAtFront(T num)
    {
        if (head == null) {
            head = new Node(num, null);
            tail = head;
        } else {
            Node tempref = new Node(num, head);
            head = tempref;
        }
    }
 
    public void removeFromBack()
    {
        if (this.isEmpty()) {
            System.out.println("The list is empty.");
        } else if (head == tail) {
            head = null;
            tail = head;
        } else {
            Node prev = head;
            Node curr = head;
            while (curr != tail) {
                prev = curr;
                curr = curr.getNextReference();
            }
            prev.setNextReference(null);
            tail = prev;
        }
    }
 
    public void removeFromFront()
    {
        if (this.isEmpty()) {
            System.out.println("The list is empty.");
        } else {
            head = head.getNextReference();
        }
    }
 
    public boolean isEmpty()
    {
        return head == null;
    }
 
    public void printList()
    {
        if (head == null) {
            System.out.println("The list is empty");
        } else {
            Node tempref = head;
            System.out.print("The values so far: ");
            while (tempref != null) {
                System.out.print(tempref.getData());
                tempref = tempref.getNextReference();
 
                if (tempref != null) {
                    System.out.print(" --> ");
                }
            }
            System.out.println();
        }
    }
 
    public boolean contains(T num)
    {
        Node tempRef = head;
        
        while (tempRef != null) {
            if (tempRef.getData().equals(num)) {
                return true;
            }
            tempRef = tempRef.getNextReference();
        }
        return false;
    }
}

LinkedListTest.java

import java.util.Scanner;
 
public class LinkedListTest
{
    public static void Menu()
    {
        System.out.println("1 - Insert At Back");
        System.out.println("2 - Quit");
        System.out.println("3 - Insert At Front");
        System.out.println("4 - Remove From Front");
        System.out.println("5 - Remove From Back");
    }
    
    public static void main(String[] args)
    {
        System.out.println("Linked Lists");
        
        Scanner scanner = new Scanner(System.in);
        Scanner scanner1 = new Scanner(System.in);
        
        int choice = 0, status = 0;
        double entry = 0;
        boolean flag = true;
        LinkedList<Double> list = new LinkedList<Double>();
        
        while (flag) {
            Menu();
            status = 0;
            
            try {
                choice = Integer.parseInt(scanner.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("Integer Only - Enter Again");
                status = 1;
            }
            
            if (choice == 1) {
                System.out.println("Enter a value?");
                entry = scanner1.nextDouble();
                list.insertAtBack(entry);
            } else if (choice == 2) {
                flag = false;
                System.out.println("End of Program");
            } else if (choice == 3) {
                System.out.println("Enter a value?");
                entry = scanner1.nextDouble();
                list.insertAtFront(entry);
            } else if (choice == 4) {
                list.removeFromFront();
            } else if (choice == 5) {
                list.removeFromBack();
            } else if (status == 0) {
                System.out.println("Invalid Entry - Enter Again");
            }
            
            list.printList();
        }
    }
}