Showing posts with label LinkedList. Show all posts
Showing posts with label LinkedList. Show all posts

Friday, February 12, 2016

Designing your own iterable stuff

Or, simply, implementing iterator logic in your own class, on which you can iterate or even use for-each loop. Because, for-each works only on Iterator based collections.
Following is a generic Java 7 code that takes into account a simple custom linked list implementation that is generic and can be iterated over, using following steps.
1. The stuff we want to iterate upon has to be Iterable and expose
iterator(). 
2. Design a java.util.Iterator by overriding hasNext(), next() and remove().

Monday, December 16, 2013

LinkedList: Node creation and display example


package ds;

/**
 * @author Rajdeep
 * 
 * Note
 * Creating the list:
 * 1. 'start' refers to the first node and 'now' is made to refer to the latest
 *   added node.
 * 2. after creating the first linkedlist object and assigning value,
 *   'start' points to it because we are checking for start==null
 *   'start' is null in first node addition only.
 *   'now' also points to it, this being the latest node.
 * 3. Second node onwards, start is never null, so control goes to else part.
 *   'now.next' points to nothing because its of reference type.
 *   so assign 'now.next' to the newly created object after
 *   checking 'now.next==null'.
 * 4. Now 'start' pointed or the first object's (node's) 'next' part is linked
 *   to the 2nd object since now was referring to it, and then after
 *   'while' execution, 'now' points to the newly created node, referred by
 *   the local 'temp' variable.
 * 5. e.g.: start -> [10|add1] at add0 -> [20|add2] at add1 -> [30|NULL] at add2;
 *   'now' points to this add2 having info 30, and 'now.next' is NULL,
 *   so that when another node is created, 'now.next' refers to the new
 *   node, and then 'now' refers to the new node, and so on.
 * 
 * Displaying the list:
 * We start from the starting node, and display 'info' part of each node, and
 * move on to next node by checking the current node's 'next' part is not null.
 */

// This is the Linked List node
class LinkedList{
 int info;   //has value
 LinkedList next; //for pointing to next node
}

// Demonstration on creation of node and displaying the values
public class LinkedListDemo {

 static LinkedList start = null;
 static LinkedList now = null;
 
 public static void main(String[] args) {
  createNode(0);
  createNode(1);
  createNode(5);
  createNode(7);
  createNode(10);
  createNode(11);
  createNode(54);
  createNode(76);
  
  display();
 }
 
 static void createNode(int val){
  LinkedList temp = new LinkedList();
  
  temp.info = val;
  if (start==null){
   start = temp;
   now = temp;
  }
  else{
   while(now.next==null){
    now.next=temp; 
   }
   now = temp;
  }
 }
 
 static void display(){
  LinkedList temp = start;
  System.out.print("[");
  while(temp!=null){
   System.out.print(temp.info + "|");
   temp = temp.next;
  }
  System.out.println("\b]");
 }

}
The output will be:
[0|1|5|7|10|11|54|76]
This was a demo program for my understanding, and now I understand how the nodes are connected, and so the term "linked list".