/* Author : Michael Robinson Program : linkedListSample.java Purpose : Here we learn how to create link lists A data structure that is very efficient to add nodes in a list. The drawback of link lists is the search time to find a node is very expensive in time. Updated : October 15, 2099 */ import java.util.*; public class linkedListSample { public static void createLinkedList() { System.out.println( "\nCreating food Linked List" ); //this is a one dimensional array of String data type String foodsArray[] = { "Chicken_soup", "Wild_Rice", "Bread", "Lasagna" }; //this is a linked list of String data type List foodsList = new LinkedList(); //this for loop reads one item at the time for( String foods: foodsArray ) { //adding the array item foods to the linked list foodsList.add(foods); System.out.printf( "adding %s\n", foods ); } System.out.println( "\nCreating people Linked List" ); //creating another single dimensional array of String type String peopleArray[] = { "Joe", "Maria", "Bobby", "Tony" }; //this is a linked list of String data type List peopleList = new LinkedList(); //this for loop reads one item at the time for( String people: peopleArray ) { //adding the array item people to the linked list peopleList.add(people); System.out.printf( "adding %s\n", people); } System.out.println( "\nMerging Linked Lists" ); //merge Linked List peopleList into foodsList foodsList.addAll(peopleList); for( String temp : foodsList ) { System.out.printf( "%s\n", temp ); } printListFoward( foodsList ); printListBackward( foodsList ); //remove nodes from location 4 to 6, included removeItems( foodsList, 4, 6 ); printListFoward( foodsList ); printListBackward( foodsList ); }//end public static void createLinkedList() public static void printListFoward( List foodsList ) { System.out.print( "\nPrinting nodes Forward : " ); for( String temp : foodsList ) { System.out.printf( "[%s] ", temp); } }//end public static void printListFoward(List foodsList) public static void printListBackward( List foodsList ) { System.out.print( "\nPrinting nodes Backwards : " ); ListIterator itr = foodsList.listIterator( foodsList.size() ); while( itr.hasPrevious() ) { System.out.printf( "[%s] ", itr.previous()); } }//end public static void //printListBackward(List foodsList) public static void removeItems( List foodsList, int from, int to ) { foodsList.subList(from, to).clear(); System.out.printf( "\n\nRemoving items %d to %d\n", from, to ); }//end public static void //removeItems(List foodsList,int from, int to) public static void main( String arg[] ) { createLinkedList(); }//end public static void main( String arg[] ) }//end public class linkList