Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ LinkedList Basic Usage     - Fedora10 use Git version Configuration Management (Linux)

- Simple comparison of MySQL and Oracle in a sql analytical details (Database)

- Linux PXE unattended installation PXE-E32: TFTP OPen timeout the solution (Linux)

- Wireless LAN security solutions (Linux)

- The strings in Python reside (Programming)

- Getting Started with Linux system to learn: how to use tcpdump to capture TCP SYN, ACK and FIN packets (Linux)

- To execute the cp command prompt type skip folder under CentOS (Linux)

- Between the two to achieve the main MySQL database synchronization from (Database)

- Oracle online redefinition (Database)

- Build the first ASP.NET 5 Web project in Mac OS X Yosemite 10.10.3 (Server)

- MongoDB 3.0 New Features (Database)

- XP virtual machine under VirtualBox solve occupy 100% CPU problem (Linux)

- Linux crontab command format example (Linux)

- Ubuntu 14.04 Nvidia graphics driver installation and settings (Linux)

- C ++ virtual functions Classic Insights (Programming)

- Log4cplus logging facility configuration, installation, testing (Linux)

- Ubuntu under Spark development environment to build (Server)

- Install VLC player on Ubuntu 14.10 (Linux)

- Understanding Java classes (Programming)

- Linux batch copy data script (Linux)

 
         
  LinkedList Basic Usage
     
  Add Date : 2017-04-13      
         
       
         
  LinkedList class is a two-way list, list each node contains a previous and a reference to the element.
LinkedList constructor as follows
1. public LinkedList (): - generate an empty list
2. public LinkedList (Collection col): copy constructor

1, the first and last elements of the list get
import java.util.LinkedList;


public class LinkedListTest {
  public static void main (String [] args) {
    LinkedList < String> lList = new LinkedList < String> ();
    lList.add ( "1");
    lList.add ( "2");
    lList.add ( "3");
    lList.add ( "4");
    lList.add ( "5");

 

    System.out.println ( "The first element of the list is:" + lList.getFirst ());
    System.out.println ( "the last element of the list is:" + lList.getLast ());
  }
}

2. Get a list element

for (String str: lList) {
      System.out.println (str);
    }

3, sub-table is generated from the list

List subl = lList.subList (1, 4);
    System.out.println (subl);
    lst.remove (2);
    System.out.println (lst);
    System.out.println (lList);

List subl = lList.subList (1, 4);
    System.out.println (subl);
    lst.remove (2);
    System.out.println (lst);
    System.out.println (lList);

4, add elements: Add a single element
 If no index is specified, then the element will be added to the list last.
public boolean add (Object element)
public boolean add (int index, Object element)
You can also put a stack or queue list had to deal with:
public boolean addFirst (Object element)
public boolean addLast (Object element)
addLast () method and add non-indexed () method to achieve the same effect.

import java.util.LinkedList;

public class LinkedListTest {
  public static void main (String [] a) {
    LinkedList list = new LinkedList ();
    list.add ( "A");
    list.add ( "B");
    list.add ( "C");
    list.add ( "D");
    list.addFirst ( "X");
    list.addLast ( "Z");
    System.out.println (list);
  }
}

5, remove the element

public Object removeFirst ()
public Object removeLast ()
import java.util.LinkedList;


public class MainClass {
  public static void main (String [] a) {


    LinkedList list = new LinkedList ();
    list.add ( "A");
    list.add ( "B");
    list.add ( "C");
    list.add ( "D");
    list.removeFirst ();
    list.removeLast ();
    System.out.println (list);
  }
}

public Object removeFirst ()
public Object removeLast ()
import java.util.LinkedList;


public class MainClass {
  public static void main (String [] a) {


    LinkedList list = new LinkedList ();
    list.add ( "A");
    list.add ( "B");
    list.add ( "C");
    list.add ( "D");
    list.removeFirst ();
    list.removeLast ();
    System.out.println (list);
  }
}

6, using a linked list stack effect

import java.util.LinkedList;
public class MainClass {
  public static void main (String [] args) {
    StackL stack = new StackL ();
    for (int i = 0; i < 10; i ++)
      stack.push (i);
    System.out.println (stack.top ());
    System.out.println (stack.top ());
    System.out.println (stack.pop ());
    System.out.println (stack.pop ());
    System.out.println (stack.pop ());
  }
}
class StackL {
  private LinkedList list = new LinkedList ();
  public void push (Object v) {
    list.addFirst (v);
  }
  public Object top () {
    return list.getFirst ();
  }
  public Object pop () {
    return list.removeFirst ();
  }
}

7, the effect of using a linked list to implement queue

import java.util.LinkedList;
public class MainClass {
  public static void main (String [] args) {
    Queue queue = new Queue ();
    for (int i = 0; i < 10; i ++)
      queue.put (Integer.toString (i));
    while (! queue.isEmpty ())
      System.out.println (queue.get ());
  }
}
class Queue {
  private LinkedList list = new LinkedList ();
  public void put (Object v) {
    list.addFirst (v);
  }
  public Object get () {
    return list.removeLast ();
  }
  public boolean isEmpty () {
    return list.isEmpty ();
  }
}

8, will be converted into ArrayList LinkedList

ArrayList < String> arrayList = new ArrayList < String> (linkedList);
    for (String s: arrayList) {
      System.out.println ( "s =" + s);
    }

ArrayList < String> arrayList = new ArrayList < String> (linkedList);
    for (String s: arrayList) {
      System.out.println ( "s =" + s);
    }

9, delete all the elements: Clear LinkedList
    lList.clear ();
10, delete the first element of the list

import java.util.LinkedList;
public class Main {
  public static void main (String [] args) {
    LinkedList < String> lList = new LinkedList < String> ();
    lList.add ( "1");
    lList.add ( "2");
    lList.add ( "3");
    lList.add ( "4");
    lList.add ( "5");
    System.out.println (lList);
        // Delete the element at the time, you can still get to the element
    Object object = lList.removeFirst ();
    System.out.println (object + "has been removed");
    System.out.println (lList);
    object = lList.removeLast ();
    System.out.println (object + "has been removed");
    System.out.println (lList);
  }
}

11. Clear list elements

import java.util.LinkedList;
public class Main {
  public static void main (String [] args) {
    LinkedList < String> lList = new LinkedList < String> ();
    lList.add ( "1");
    lList.add ( "2");
    lList.add ( "3");
    lList.add ( "4");
    lList.add ( "5");
    System.out.println (lList);
    lList.subList (2, 5) .clear ();
    System.out.println (lList);
  }
}

12, to delete the list of specific elements

import java.util.LinkedList;
public class Main {
  public static void main (String [] args) {
    LinkedList < String> lList = new LinkedList < String> ();
    lList.add ( "1");
    lList.add ( "2");
    lList.add ( "3");
    lList.add ( "4");
    lList.add ( "5");
    System.out.println (lList);
    System.out.println (lList.remove ( "2")); // delete the element value element = 2
    System.out.println (lList);
    Object obj = lList.remove (2); // Delete the second element
    System.out.println (obj + "has been removed from the list");
    System.out.println (lList);
  }
}

13, the LinkedList into an array, the array length 0

import java.util.LinkedList;
import java.util.List;
public class Main {
  public static void main (String [] args) {
    List < String> theList = new LinkedList < String> ();
    theList.add ( "A");
    theList.add ( "B");
    theList.add ( "C");
    theList.add ( "D");
    String [] my = theList.toArray (new String [0]);
    for (int i = 0; i < my.length; i ++) {
      System.out.println (my [i]);
    }
  }
}

14, the LinkedList into an array, the array length is the length of the list

import java.util.LinkedList;
import java.util.List;
public class Main {
  public static void main (String [] args) {
    List < String> theList = new LinkedList < String> ();
    theList.add ( "A");
    theList.add ( "B");
    theList.add ( "C");
    theList.add ( "D");
    String [] my = theList.toArray (new String [theList.size ()]);
    for (int i = 0; i < my.length; i ++) {
      System.out.println (my [i]);
    }
  }
}

15, will be converted into ArrayList LinkedList

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Main {
  public static void main (String [] args) {
    LinkedList < String> myQueue = new LinkedList < String> ();
    myQueue.add ( "A");
    myQueue.add ( "B");
    myQueue.add ( "C");
    myQueue.add ( "D");
    List < String> myList = new ArrayList < String> (myQueue);
    for (Object theFruit: myList)
      System.out.println (theFruit);
  }
}

16, to achieve the stack

import java.util.Collections;
import java.util.LinkedList;
public class Main {
  public static void main (String [] argv) throws Exception {
    LinkedList stack = new LinkedList ();
    Object object = "";
    stack.addFirst (object);
    Object o = stack.getFirst ();
    stack = (LinkedList) Collections.synchronizedList (stack);
  }
}

17, queue

import java.util.LinkedList;
public class Main {
  public static void main (String [] argv) throws Exception {
    LinkedList queue = new LinkedList ();
    Object object = "";
    // Add to end of queue
    queue.add (object);
    // Get head of queue
    Object o = queue.removeFirst ();
  }
}

18. The synchronization method

import java.util.Collections;
import java.util.LinkedList;
public class Main {
  public static void main (String [] argv) throws Exception {
    LinkedList queue = new LinkedList ();
    Object object = "";
    queue.add (object);
    Object o = queue.removeFirst ();
    queue = (LinkedList) Collections.synchronizedList (queue);
  }
}

19, find the element position

import java.util.LinkedList;

public class Main {
  public static void main (String [] args) {
    LinkedList < String> lList = new LinkedList < String> ();
    lList.add ( "1");
    lList.add ( "2");
    lList.add ( "3");
    lList.add ( "4");
    lList.add ( "5");
    lList.add ( "2");
    System.out.println (lList.indexOf ( "2"));
    System.out.println (lList.lastIndexOf ( "2"));
  }
}

20, replaced elements

import java.util.LinkedList;

public class Main {
  public static void main (String [] args) {
    LinkedList < String> lList = new LinkedList < String> ();
    lList.add ( "1");
    lList.add ( "2");
    lList.add ( "3");
    lList.add ( "4");
    lList.add ( "5");
    System.out.println (lList);
    lList.set (3, "Replaced"); // replace the element using the set method, the first parameter is the index of the element method, the latter is the replacement value
    System.out.println (lList);
  }
}

21, add objects list

import java.util.LinkedList;
class Address {
  private String name;
  private String street;
  private String city;
  private String state;
  private String code;
  Address (String n, String s, String c, String st, String cd) {
    name = n;
    street = s;
    city = c;
    state = st;
    code = cd;
  }
  public String toString () {
    return name + "" + street + "" + city + "" + state + "" + code;
  }
}


class MailList {
  public static void main (String args []) {
    LinkedList < Address> ml = new LinkedList < Address> ();
    ml.add (new Address ( "A", "11 Ave", "U", "IL", "11111"));
    ml.add (new Address ( "R", "11 Lane", "M", "IL", "22222"));
    ml.add (new Address ( "T", "8 St", "C", "IL", "33333"));
    for (Address element: ml)
      System.out.println (element + "\ n");
  }
}

22, confirm the existence of a particular list elements

import java.util.LinkedList;


public class Main {
  public static void main (String [] args) {
    LinkedList < String> lList = new LinkedList < String> ();
    lList.add ( "1");
    lList.add ( "2");
    lList.add ( "3");
    lList.add ( "4");
    lList.add ( "5");
    if (lList.contains ( "4")) {
      System.out.println ( "LinkedList contains 4");
    } Else {
      System.out.println ( "LinkedList does not contain 4");
    }
  }
}

23, according to a list element to generate an array of objects

Object [] objArray = lList.toArray ();
for (Object obj: objArray) {
  System.out.println (obj);
}

24, multi-threaded list

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
class PrepareProduction implements Runnable {
  private final List < String> queue;
  PrepareProduction (List < String> q) {
    queue = q;
  }
  public void run () {
    queue.add ( "1");
    queue.add ( "done");
  }
}
class DoProduction implements Runnable {
  private final List < String> queue;
  DoProduction (List < String> q) {
    queue = q;
  }
  public void run () {
    String value = queue.remove (0);
    while (! value.equals ( "*")) {
      System.out.println (value);
      value = queue.remove (0);
    }
  }
}
public class Main {
  public static void main (String [] args) throws Exception {
    List q = Collections.synchronizedList (new LinkedList < String> ());
    Thread p1 = new Thread (new PrepareProduction (q));
    Thread c1 = new Thread (new DoProduction (q));
    p1.start ();
    c1.start ();
    p1.join ();
    c1.join ();
  }
}

25, the priority list (from JBOSS)

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;


public class BasicPriorityLinkedList {


  protected LinkedList [] linkedLists;
  protected int priorities;
  protected int size;


  public BasicPriorityLinkedList (int priorities) {
    this.priorities = priorities;
    initDeques ();
  }
  public void addFirst (Object obj, int priority) {
    linkedLists [priority] .addFirst (obj);
    size ++;
  }
  public void addLast (Object obj, int priority) {
    linkedLists [priority] .addLast (obj);
    size ++;
  }
  public Object removeFirst () {
    Object obj = null;
    for (int i = priorities - 1; i> = 0; i--) {
      LinkedList ll = linkedLists [i];
      if (! ll.isEmpty ()) {
        obj = ll.removeFirst ();
        break;
      }
    }
    if (obj! = null) {
      size--;
    }
    return obj;
  }
  public Object removeLast () {
    Object obj = null;
    for (int i = 0; i < priorities; i ++) {
      LinkedList ll = linkedLists [i];
      if (! ll.isEmpty ()) {
        obj = ll.removeLast ();
      }
      if (obj! = null) {
        break;
      }
    }
    if (obj! = null) {
      size--;
    }
    return obj;
  }


  public Object peekFirst () {
    Object obj = null;
    for (int i = priorities - 1; i> = 0; i--) {
      LinkedList ll = linkedLists [i];
      if (! ll.isEmpty ()) {
        obj = ll.getFirst ();
      }
      if (obj! = null) {
        break;
      }
    }
    return obj;
  }


  public List getAll () {
    List all = new ArrayList ();
    for (int i = priorities - 1; i> = 0; i--) {
      LinkedList deque = linkedLists [i];
      all.addAll (deque);
    }
    return all;
  }


  public void clear () {
    initDeques ();
  }


  public int size () {
    return size;
  }


  public boolean isEmpty () {
    return size == 0;
  }


  public ListIterator iterator () {
    return new PriorityLinkedListIterator (linkedLists);
  }


  protected void initDeques () {
    linkedLists = new LinkedList [priorities];
    for (int i = 0; i < priorities; i ++) {
      linkedLists [i] = new LinkedList ();
    }
    size = 0;
  }


  class PriorityLinkedListIterator implements ListIterator {
    private LinkedList [] lists;
    private int index;
    private ListIterator currentIter;
    PriorityLinkedListIterator (LinkedList [] lists) {
      this.lists = lists;
      index = lists.length - 1;
      currentIter = lists [index] .listIterator ();
    }


    public void add (Object arg0) {
      throw new UnsupportedOperationException ();
    }


    public boolean hasNext () {
      if (currentIter.hasNext ()) {
        return true;
      }
      while (index> = 0) {
        if (index == 0 || currentIter.hasNext ()) {
          break;
        }
        index--;
        currentIter = lists [index] .listIterator ();
      }
      return currentIter.hasNext ();
    }


    public boolean hasPrevious () {
      throw new UnsupportedOperationException ();
    }


    public Object next () {
      if (! hasNext ()) {
        throw new NoSuchElementException ();
      }
      return currentIter.next ();
    }


    public int nextIndex () {
      throw new UnsupportedOperationException ();
    }


    public Object previous () {
      throw new UnsupportedOperationException ();
    }


    public int previousIndex () {
      throw new UnsupportedOperationException ();
    }


    public void remove () {
      currentIter.remove ();
      size--;
    }


    public void set (Object obj) {
      throw new UnsupportedOperationException ();
    }
  }


}

26, with the help of the class generated list (from google)

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class Lists {
  private Lists () {}
  public static < E> ArrayList < E> newArrayList () {
    return new ArrayList < E> ();
  }
  public static < E> ArrayList < E> newArrayListWithCapacity (int initialCapacity) {
    return new ArrayList < E> (initialCapacity);
  }
  public static < E> ArrayList < E> newArrayList (E ... elements) {
    ArrayList < E> set = newArrayList ();
    Collections.addAll (set, elements);
    return set;
  }
  public static < E> ArrayList < E> newArrayList (Iterable < ? extends E> elements) {
    ArrayList < E> list = newArrayList ();
    for (E e: elements) {
      list.add (e);
    }
    return list;
  }
  public static < E> LinkedList < E> newLinkedList () {
    return new LinkedList < E> ();
  }
}
     
         
       
         
  More:      
 
- Linux shared libraries .so file name and Dynamic Link (Linux)
- SecureCRT 7.0 Log Ubuntu 12.04 server via SSH service under Vmware (Server)
- CentOS-based Kickstart automated installation practice (Linux)
- C language keywords Comments (Programming)
- Ubuntu 14.04 compile RT288x_SDK / toolchain / squashfs3.2-r2 Error (Linux)
- Oracle inline view updates problems encountered (Database)
- To install Oracle Database Details and FAQ Summary under CentOS (Database)
- SSH security note (Linux)
- Intruder tools Knark Analysis and Prevention Linux environment (Linux)
- Tmux Getting Start (Linux)
- Steps to build MPICH2 development environment on CentOS 6.4 (Linux)
- Using DBMS_STAT function closes mission (Database)
- How to create an alternative Android / iOS connected wireless hotspot AP in Ubuntu 15.04 (Linux)
- Binary Tree Traversal (Linux)
- C ++ type conversion and RTTI (Programming)
- Zabbix configure DataGuard monitoring (Database)
- C + + secondary pointer memory model (pointer array) (Programming)
- Compare Oracle MySQL (ICP) index condition pushdown is described (Database)
- Ubuntu 14.10 install KDE Plasma 5.2 (Linux)
- GoldenGate update is missing (Database)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.