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

CODILITY: Determine whether given string of parentheses is properly nested.

Task description A string S consisting of N characters is called  properly nested  if: S is empty; S has the form " (U) " where U is a properly nested string; S has the form " VW " where V and W are properly nested strings. For example, string " (()(())()) " is properly nested but string " ()) " isn't. Write a function: class Solution { public int solution(String S); } that, given a string S consisting of N characters, returns 1 if string S is properly nested and 0 otherwise. For example, given S = " (()(())()) ", the function should return 1 and given S = " ()) ", the function should return 0, as explained above. Assume that: N is an integer within the range [ 0 .. 1,000,000 ]; string S consists only of the characters " ( " and/or " ) ". Complexity: expected worst-case time complexity is O(N); expected worst-case space complexity is O(1) (not counting the storage requi...

Distinct: Compute number of distinct values in an array.

Task description Write a function class Solution { public int solution(int[] A); } that, given a zero-indexed array A consisting of N integers, returns the number of distinct values in array A. Assume that: N is an integer within the range [ 0 .. 100,000 ]; each element of array A is an integer within the range [ −1,000,000 .. 1,000,000 ]. For example, given array A consisting of six elements such that: A[0] = 2 A[1] = 1 A[2] = 1 A[3] = 2 A[4] = 3 A[5] = 1 the function should return 3, because there are 3 distinct values appearing in array A, namely 1, 2 and 3. Complexity: expected worst-case time complexity is O(N*log(N)); expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments). Elements of input arrays can be modified. class Solution { public int solution ( int [] A) { // write your code in Java SE 8 int len=A.length; int count= 1 ; ...

Dominator: Find an index of an array such that its value occurs at more than half of indices in the array.

Dominator Find an index of an array such that its value occurs at more than half of indices in the array. Task description A zero-indexed array A consisting of N integers is given. The dominator  of array A is the value that occurs in more than half of the elements of A. For example, consider array A such that A[0] = 3 A[1] = 4 A[2] = 3 A[3] = 2 A[4] = 3 A[5] = -1 A[6] = 3 A[7] = 3 The dominator of A is 3 because it occurs in 5 out of 8 elements of A (namely in those with indices 0, 2, 4, 6 and 7) and 5 is more than a half of 8. Write a function class Solution { public int solution(int[] A); } that, given a zero-indexed array A consisting of N integers, returns index of any element of array A in which the dominator of A occurs. The function should return −1 if array A does not have a dominator. Assume that: N is an integer within the range [ 0 .. 100,000 ]; each element of array A is an integer within the range [ −2,147,483,648 .. 2,147,483...