Basic example of implementing Singly Linked List in Java

The following is a very basic example of a Singly Linked List implementationin Java. In a nutshell, it’s an object that holds a value for itself and also has a reference to another object in the list, which could be null of the object itself is the tail (i.e. last element in the linked list)

package com.icodejava.blog.published.datastructure;
/**
 * @author Kushal Paudyal
 * Created on - 11/28/2016
 * Last modified on - 11/28/2016
 * This class shows a basic example of defining a SinglyLinkedList in Java
 * Also provides a overridden constructor both of which are optional.
 */
public class SinglyLinkedList {
	
	private Object value;
	private SinglyLinkedList nextObject;
	
	public SinglyLinkedList(Object value) {
		this.value = value;
		this.nextObject = null;
	}
	
	public SinglyLinkedList(Object value, SinglyLinkedList nextObject) {
		this.value = value;
		this.nextObject = nextObject;
	}
	
	
	public Object getValue() {
		return value;
	}
	public void setValue(Object value) {
		this.value = value;
	}
	public SinglyLinkedList getNextObject() {
		return nextObject;
	}
	public void setNextObject(SinglyLinkedList nextObject) {
		this.nextObject = nextObject;
	}

}


Linked List is a data structure that possess following properties:

  • The are a group of nodes linked together by some reference. Singly linked list has reference to next node, while doubly linked list has reference to previous and next nodes.
  • Elements can be inserted indefinitely.
  • They don’t take contiguous memory locations. For this reason, they are suitable to be used even if the disk or memory is fragmented.
  • They allow sequential access to elements while arrays allow random access
  • Extra space is needed for reference. It is impractical to use for boolean or char for space wastage.

Singly linked list might not be a right data structure for all situations, so a care must be taken in choosing the data structure for your problem. As the link in the singly linked list only points to the location of the next object (and not in reverser order), singly linked list can only be traversed in forward direction. Also, you need to know the location of the first node or object to be able to traverser rest of the nodes or objects.

Java Implementation – ROT-13 Encoding Algorithm

ROT-13 is the simplest of all encoding algorithms. The idea is simple, rotate the characters by 13. If you assume characters A-Z (or a-z) to be in a circle, for any characters, go to 13 characters ahead in the circle and replace it with that character.

The following diagram taken from Wikipedia (under wikipedia commons license), demonstrates the character substitution fact clearly.

Interestingly, if you encode a plain text, to decode it, you need to re-encode it.

That means for a character ABC, encode (encode( ABC)) is ABC

The following is the Java Implementation of ROT-13 Algorithm. Since having only A-Z or a-z characters may not suffice, I have added a condition where other characters will remain the same.

/**
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * Last Modified on 5th September 2008
 */
package com.kushal.utilities;
/**Java Implementation of ROT-13 Encoding algorithm**/
public class Rot13Encryption {

	public static void main(String args[]) {
		//Original Text
		String plainText="Sanjaal.Com";
		//Let's Encode It
		String encodedText=rot13Encode(plainText);
		//Then Decide It
		String decodedText=rot13Encode(encodedText);

		System.out.println("Original Text: tt"+plainText);
		System.out.println("After ROT-13 Encoding: t"+encodedText);
		System.out.println("After ROT-13 Decoding: t"+decodedText);
	}

	/**
	 * @param textToEncode
	 * @return encoded (or decoded)text.
	 * Note: if you encode a text, and encode the result again,
	 * you will get the original text.
	 */
	public static String rot13Encode(String textToEncode) {
		String encodedText = "";
		int textLength = textToEncode.length();

		for (int i = 0; i < textLength; i++) {
			char currentChar = textToEncode.charAt(i);
			if ((currentChar >= 65 && currentChar <= 77)
					|| (currentChar >= 97 && currentChar <= 109)) {
				encodedText += (char) (currentChar + 13);
			} else if ((currentChar >= 78 && currentChar <= 90)
					|| (currentChar >= 110 && currentChar <= 122)) {
				encodedText += (char) (currentChar - 13);
			} else {
				encodedText += currentChar;
			}
		}
		return encodedText;
	}

}


—————————————-
Here is the sample output:
—————————————-

Original Text:                     Sanjaal.Com
After ROT-13 Encoding:     Fnawnny.Pbz
After ROT-13 Decoding:     Sanjaal.Com

The Servlet Basic Structure

/**
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * Created on 19th July 2008
 */
package com.kushal.servlets;
/*
 * This is a basic servlet class that shows how
 * servlet can be structured.
 */
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyFirstServlet extends HttpServlet {

/**
 * This is GET method. Request has parameters, 
 * and we write back using response.
 */	
public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
      
    PrintWriter out = response.getWriter();
    
    out.write("Hello, this is my first servlet");    
  }

/**
 * This is another kind of request with POST
 * kind of submission. We can simply reroute
 * the request to doGetmethod as below.
 */
public void doPost(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
	  doGet(request, response);	  
  }
}

/**
 * In order to be able to run this servlet, you have
 * to setup a proper application server and deploy
 * the servlet. Discussing those steps is out of
 * scope in this post.
 */