Generics: Example: fifo Linked List |
|
/** Generics technique in Java allows us to build one class of a certain type T (anytype, wrapped as <T>). This class will plays the role of reference to any other kind of type such as char, String, Integer, Double, or any other built object. In this example, we take a linked list, the Generics Node <T> class will be used to build a FIFO linked list. We will have the choice to fill out this list by values of any type. For this program, we use String type. */ import java.io.*; import java.util.*; //The main class: The Node class: -------------------------- class Node <T> {//The main class of refernece //Two variables public T Data;//the value of the node public Node <T> Next;// The node itself. This is the key of the linked list. public Node (){//Constructor } public Node (T TheData){//Constructor Data = TheData; } public T getData() {//To get a value return Data; } public Node<T> getNext() {//To get a node return Next; } public void setData (T TheData) {//To set a value Data = TheData; } public void setNext (Node<T> Next){//To set a node //Node Next = ThisNext; this.Next= Next; //Move on the list } public void showType() {// Just a method to test the type of the value Data System.out.println("Type of T is " + Data.getClass().getName()); } }//End of the class Node // Class to use the class Node: --------------------------------- public class LifoList {// A class to use the class Node public static void main (String [] args) throws IOException { Node ThisList = ReadData(); Display (ThisList); } public static Node ReadData()throws IOException { //Initialisation: char TheResponse; //=================== Change here: ========== start========== //Change String to another TYPE in 1,2,3,4 String Data;//1 Node<String> Head = null;//2 Node<String> tempo = null;//3 //======================================== end ============= do {//Using Scanner method for the inputs to fill out the list Scanner in = new Scanner(System.in); System.out.print("\n\tEnter a data: --> "); //=================== Change here: ========== start ======== //Change nextLine() in Data = in.nextLine(); //to nextInt(), nextLong(), nextFloat(), //nextDouble(), or next(); Data = in.nextLine(); tempo = new Node<String> (Data);//4 //============================================== end ======= tempo.setNext(Head); Head = tempo; //Read the inputs using the above calss ReadUtilities TheResponse = ReadChar ("\n\tDo you want to continue ? y/n: --> "); TheResponse = (char) ( TheResponse + 'A' - 'a'); } while(TheResponse == 'Y'); return Head; } static void Display (Node ThisList){ if (ThisList != null){ System.out.println ("\n\tIn the LIFO (First In First Out) order, the list contains :"); System.out.println ("\n"); int rank = 0; while(ThisList != null){ System.out.println ("\n\t" + ++rank + ")\t" + ThisList.getData()); ThisList = ThisList.getNext();// Move on the list } }else System.out.println ("The list is empty \n"); } // Method to read keybord character: static char ReadChar(String info) throws IOException { BufferedReader TheInput = new BufferedReader ( new InputStreamReader(System.in)); System.out.print(info); return TheInput.readLine().charAt(0); } }//End of LifoList |