Single linked list : Link List « Collections Data Structure « Java






Single linked list

      



/**
 * Copyright (c) 2005 Elie Levy <elie.levy@zilonis.org>
 * All rights reserved
 * 
 * This License governs use of the accompanying Software, and your use of the
 * Software constitutes acceptance of this license.
 * 
 * You may use this Software for any non-commercial purpose, subject to the
 * restrictions in this license. Some purposes which can be non-commercial are
 * teaching, academic research, and personal experimentation. You may also
 * distribute this Software with books or other teaching materials, or publish
 * the Software on websites, that are intended to teach the use of the 
 * Software.
 * 
 * 
 * You may not use or distribute this Software or any derivative works in any
 * form for commercial purposes. Examples of commercial purposes would be
 * running business operations, licensing, leasing, or selling the Software, or
 * distributing the Software for use with commercial products.
 * 
 * You may modify this Software and distribute the modified Software for
 * non-commercial purposes, however, you may not grant rights to the Software 
 * or derivative works that are broader than those provided by this License. 
 * For example, you may not distribute modifications of the Software under
 * terms that would permit commercial use, or under terms that purport to 
 * require the Software or derivative works to be sublicensed to others.
 * 
 * You may use any information in intangible form that you remember after
 * accessing the Software. However, this right does not grant you a license to
 * any of the copyrights or patents for anything you might create using such
 * information.
 * 
 * In return, we simply require that you agree:
 * 
 * Not to remove any copyright or other notices from the Software.
 * 
 * 
 * That if you distribute the Software in source or object form, you will
 * include a verbatim copy of this license.
 * 
 * 
 * That if you distribute derivative works of the Software in source code form
 * you do so only under a license that includes all of the provisions of this
 * License, and if you distribute derivative works of the Software solely in
 * object form you do so only under a license that complies with this License.
 * 
 * 
 * That if you have modified the Software or created derivative works, and
 * distribute such modifications or derivative works, you will cause the
 * modified files to carry prominent notices so that recipients know that they
 * are not receiving the original Software. Such notices must state: (i) that
 * you have changed the Software; and (ii) the date of any changes.
 * 
 * 
 * THAT THE SOFTWARE COMES "AS IS", WITH NO WARRANTIES. THIS MEANS NO EXPRESS,
 * IMPLIED OR STATUTORY WARRANTY, INCLUDING WITHOUT LIMITATION, WARRANTIES OF
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR ANY WARRANTY OF TITLE
 * OR NON-INFRINGEMENT. ALSO, YOU MUST PASS THIS DISCLAIMER ON WHENEVER YOU
 * DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
 * 
 * 
 * THAT NEITHER ZILONIS NOR THE AUTHOR WILL BE LIABLE FOR ANY DAMAGES RELATED
 * TO THE SOFTWARE OR THIS LICENSE, INCLUDING DIRECT, INDIRECT, SPECIAL,
 * CONSEQUENTIAL OR INCIDENTAL DAMAGES, TO THE MAXIMUM EXTENT THE LAW PERMITS,
 * NO MATTER WHAT LEGAL THEORY IT IS BASED ON. ALSO, YOU MUST PASS THIS
 * LIMITATION OF LIABILITY ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR 
 * DERIVATIVE WORKS.
 * 
 * 
 * That if you sue anyone over patents that you think may apply to the Software
 * or anyone's use of the Software, your license to the Software ends
 * automatically.
 * 
 * 
 * That your rights under the License end automatically if you breach it in any
 * way.
 * 
 * 
 * Elie Levy reserves all rights not expressly granted to you in this 
 * license.
 *  
 */


import java.util.Iterator;

/**
 * A very lite single linked list
 */
public class LiteList<Element> implements Iterable<Element> {
    
    private Node first;

    public void add(Element element) {
  first = new Node(element,first);
    }

    public Iterator<Element> iterator() {
  return new LiteIterator();
    }

    private class LiteIterator implements Iterator<Element> {
  Node current;

  public LiteIterator() {
      current = first;
  }
  
  public boolean hasNext() {
      return (current!=null);
  }

  public Element next() {
      Element result = current.getElement();
      current = current.getNext();
      return result;
  }

  public void remove() {
  }
    }

    private class Node {
  private Element element;
  private Node next;
  
  public Node(Element element, Node next) {
      this.element = element;
      this.next = next;
  }

  public Element getElement() {
      return element;
  }

  public Node getNext() {
      return next;
  }
    } 

}

   
    
    
    
    
    
  








Related examples in the same category

1.Use for each loop to go through elements in a linkedlist
2.Use addFirst method to add value to the first position in a linked list
3.To insert an object into a specific position into the list, specify the index in the add method
4.Updating LinkedList Items
5.Convert LinkedList to Array with zero length array
6.Convert LinkedList to Array with full length array
7.Checking what item is first in line without removing it: element
8.Removing the first item from the queue: poll
9.Convert a LinkedList to ArrayList
10.Add elements at beginning and end of LinkedList Java example
11.Check if a particular element exists in LinkedList Java example
12.Create an object array from elements of LinkedList Java example
13.Get elements from LinkedList Java example
14.Get first and last elements from LinkedList Java example
15.Get SubList from LinkedList Java example
16.Iterate through elements of Java LinkedList using Iterator example
17.Remove all elements or clear LinkedList Java example
18.Iterate through elements of Java LinkedList using ListIterator example
19.Remove first and last elements of LinkedList Java example
20.Remove range of elements from LinkedList Java example
21.Remove specified element from LinkedList Java example
22.Replace an Element of LinkedList Java example
23.Search elements of LinkedList Java example
24.Add or insert an element to ArrayList using Java ListIterator Example
25.Finding an Element in a Sorted List
26.Create a list with an ordered list of strings
27.Search for a non-existent element
28.Use an Iterator to cycle through a collection in the forward direction.
29.Implementing a Queue with LinkedList
30.Implementing a Stack
31.Using a LinkedList in multi-thread
32.Convert Collection to ArrayList
33.Wrap queue to synchronize the methods
34.Making a stack from a LinkedListMaking a stack from a LinkedList
35.Double LinkedList
36.Doubly Linked listDoubly Linked list
37.A class for you to extend when you want object to maintain a doubly linked list
38.A simple Doubly Linked list class, designed to avoid O(n) behaviour on insert and delete.A simple Doubly Linked list class, designed to avoid O(n) behaviour on insert and delete.
39.A List helper class that attempts to avoid unneccessary List creation.
40.This program demonstrates operations on linked lists
41.Simple linked list class which uses a Comparator to sort the nodes.
42.This implementation of LinkedList that is optimized for element removal.