Skip to main content

Singly Linked List Generic Implementation in Java

File: Node.java

package org.developersbrain.solutions;
//Linked List implementation using Recursive methods
//This is a generic implementation
public class Node<T> {
Node<T> nextNode;
T t;

Node(T t){
this.t = t;
nextNode=null;
}

public Node<T> insertRear(Node<T> next){
if(next.nextNode!=null) {
next=insertRear(next.nextNode);
}
return next;
}

public void insert(T t){
Node<T> newN=new Node<T>(t);
Node<T> lastNode=insertRear(this);
//Looped through all elements to find the last element
//Because the insert is always at the end in the singly linked list
lastNode.nextNode=newN;
}

private Node<T> printNode(Node<T> node){
if(node != null){
System.out.println(node.t.toString());
//You have to overwrite the generic original class for the method to_string
//to print the output as the way you want
node=printNode(node.nextNode);
}
return node;
}

public void traverse(){
printNode(this);
}

}



File : MainClass.java

package org.developersbrain.solutions;

public class MainClass {

public static void main(String[] args){
Node<Integer> ll=new Node<Integer>(5);
ll.insert(10);
ll.insert(20);
ll.insert(30);
System.out.println("List Traversing- Integer");
System.out.println("----------------");
ll.traverse();
Node<String> ll_Names=new Node<String>("Name 1");
ll_Names.insert("Name 2");
ll_Names.insert("Name 3");
ll_Names.insert("Name 4");
System.out.println("List Traversing - String");
System.out.println("----------------");
ll_Names.traverse();

Node<Double> ll_dbl=new Node<Double>(10.23);
ll_dbl.insert(11.23);
ll_dbl.insert(12.2323);
ll_dbl.insert(34.33);
System.out.println("List Traversing- Double Values");
System.out.println("----------------");
ll_dbl.traverse();
}
}


output:
--------------------------------------------


List Traversing- Integer
----------------
5
10
20
30
List Traversing - String
----------------
Name 1
Name 2
Name 3
Name 4
List Traversing- Double Values
----------------
10.23
11.23
12.2323
34.33



Comments

Popular posts from this blog

Java Interface

Problem Statement A Java interface can only contain method signatures and fields. Interface can be used to achieve polymorphism. In this problem you will practice your knowledge on interfaces. You are given an interface   AdvancedArithmetic   which contains a method signature   public abstract int divisorSum(int n) . You need to write a class called MyCalculator which implements the interface. divisorSum   function just takes an integer as input and return the sum of all its divisors. For example divisors of 6 are 1,2,3 and 6, so   divisorSum   should return 12. Value of n will be at most 1000. Read the partially completed code in the editor and complete it. You just need to write the MyCalculator class only.   Your class shouldn't be public. Sample Input 6 Sample Output I implemented: AdvancedArithmetic 12 Explanation Divisors of 6 are 1,2,3 and 6. 1+2+3+6=12. import java.util.*; interface AdvancedArithmetic{   public abstract int divisorSum(int n

Problem: Java Exception Handling

Problem Statement Create a class myCalculator which consists of a single method power(int,int). This method takes two integers,   n   and   p , as parameters and finds   n p . If either   n   or   p   is negative, then the method must throw an exception which says "n and p should be non-negative". Please read the partially completed code in the editor and complete it. Your code mustn't be public. No need to worry about constraints, there won't be any overflow if your code is correct. Sample Input 3 5 2 4 -1 -2 -1 3 Sample Output 243 16 java.lang.Exception: n and p should be non-negative java.lang.Exception: n and p should be non-negative import  java.util.*; class myCalculator{     int power(int n,int p) throws java.lang.Exception{         int power=1;         while(p>0){             power=power*(n);             p--;         }                 if(n==0) power=0;                if(n<0 | p<0){       

Google Cloud Shell | Delete instance