import java.util.LinkedList; import java.util.*; import java.io.*; import java.lang.*;
public class LinkedListv4 {
char question; Scanner console = new Scanner(System.in); LinkedList listA = new LinkedList();
public void Switchers(){
System.out.println("| a. Append a specified elements to the list"); System.out.println("| b. Add an element at specified index"); System.out.println("| c. Add a new elements in front of the list"); System.out.println("| d. Add new elements with a specified value into the list alternately"); System.out.println("| e. Remove the first element in the list"); System.out.println("| f. Find an element at a specified position in the list"); System.out.println("| g. Replace the elements at a specified index"); System.out.println("| h. Print the list"); System.out.println("| i. Print the list from a specified index"); System.out.println("| j. Print the list in reverse order"); System.out.println("| k. Remove all elements in the list"); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit"); question = console.next().charAt(0); System.out.println(); switch (question) { case 'a': AppendElement(); case 'b': AddElementSpecific(); case 'c': AddElementFront(); case 'd': EditExistingList(); case 'e': RemoveFirstElement(); case 'f': ViewElements(); case 'g': ReplaceElement(); case 'h': PrintList(); case 'i': PrintListSpecific(); case 'j': PrintReverseOrder(); case 'k': ClearAll(); case 'l': CheckEmptyList(); case 'q': Quit(); case 'Q': Quit(); break; default: System.out.println("Invalid Input"); System.out.println(); Switchers(); break; }
}
//1 --- DONE public void AppendElement() { System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Create/Store objects in an LinkedList container. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); System.out.println("Please set maximum elements required"); System.out.println(); int Max = console.nextInt(); for (int i = 0; i < Max; i++) { System.out.println("Please insert number for element "+ i); double element = console.nextDouble(); System.out.println(" - Storing Number(" + element + ")"); listA.add(new Double(element)); } System.out.println(); System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Retrieve objects in an LinkedList container using an Iterator. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); Iterator Ite = listA.iterator(); while (Ite.hasNext()) { System.out.println(Ite.next()); } Switchers(); } //2 --- DONE FIXED - ??? public void AddElementSpecific() { System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Create/Store objects in specified LinkedList container. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); System.out.println("Please insert specified index :"); int index = console.nextInt(); System.out.println("Please insert number for element :"); double element = console.nextDouble(); System.out.println(" - Storing Number(" + element + ")"); listA.add(index,new Double(element)); System.out.println(); System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Retrieve objects in an LinkedList container using an Iterator. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); Iterator Ite = listA.iterator(); while (Ite.hasNext()) { System.out.println(Ite.next()); } Switchers(); } //3 --- DONE public void AddElementFront() { System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Create/Store objects in front of LinkedList container. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); int i= 0; System.out.println("System will store element to the first index, Insert new element please :"); double element = console.nextDouble(); System.out.println(" - Storing Number(" + element + ")"); listA.add(0,new Double(element)); System.out.println(); System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Retrieve objects in an LinkedList container using an Iterator. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); Iterator Ite = listA.iterator(); while (Ite.hasNext()) { System.out.println(Ite.next()); } Switchers(); } //4 --- DONE - ??? public void EditExistingList() { System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Add new elements with a specified value into the list alternately |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); System.out.println(); System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Retrieve objects in an LinkedList container using an Iterator. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); Iterator IteInitial = listA.iterator(); while (IteInitial.hasNext()) { System.out.println(IteInitial.next()); } System.out.println("Please choose desirable index to modify elements"); System.out.println(); int SpecIndex = console.nextInt(); listA.remove(SpecIndex); System.out.println("Please insert new number for element "+ SpecIndex); double element = console.nextDouble(); System.out.println(" - Storing Number(" + element + ")"); listA.add(SpecIndex,new Double(element)); System.out.println(); System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Retrieve objects in an LinkedList container using an Iterator. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); Iterator Ite = listA.iterator(); while (Ite.hasNext()) { System.out.println(Ite.next()); } Switchers(); } //5 --- DONE public void RemoveFirstElement() { System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Remove fisrt element in the LinkedList container. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); Object object = listA.removeFirst(); System.out.println(object + " has been removed from the first index of LinkedList"); System.out.println(); System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Retrieve objects in an LinkedList container using an Iterator. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); Iterator Ite = listA.iterator(); while (Ite.hasNext()) { System.out.println(Ite.next()); } Switchers(); } //6 --- DONE public void ViewElements() { System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Find an element at a specified position in the list |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); System.out.println(); System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Retrieve objects in an LinkedList container using an Iterator. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); Iterator Ite = listA.iterator(); while (Ite.hasNext()) { System.out.println(Ite.next()); } System.out.println("Select any element in the list to review index position :"); double element = console.nextDouble(); System.out.println(); int NotNegOne = listA.indexOf(element); if (NotNegOne != -1) { System.out.println("element "+element+" is at index "+listA.indexOf(element)); } else { System.out.println("LinkedList does not contain "+element); } Switchers(); } //7 --- DONE public void ReplaceElement() { System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Replace the elements at a specified index |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); System.out.println(); System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Retrieve objects in an LinkedList container using an Iterator. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); Iterator IteInitial = listA.iterator(); while (IteInitial.hasNext()) { System.out.println(IteInitial.next()); } System.out.println(); System.out.println("Please choose desirable index to modify elements"); System.out.println(); int SpecIndex = console.nextInt(); listA.remove(SpecIndex); System.out.println("Please insert new number for index "+ SpecIndex); double element = console.nextDouble(); System.out.println(" - Storing Number(" + element + ")"); listA.add(SpecIndex,new Double(element)); System.out.println(); System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Retrieve objects in an LinkedList container using an Iterator. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); Iterator Ite = listA.iterator(); while (Ite.hasNext()) { System.out.println(Ite.next()); } Switchers(); } //8 --- DONE public void PrintList() { System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Print List in the LinkedList container. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); if (listA.isEmpty()){ System.out.println("Linked list is empty"); } else{ System.out.println (listA); } Switchers(); } //9 --- DONE public void PrintListSpecific() { System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Print List from specified index in the LinkedList container. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); try { int indexSize = listA.size(); int realIndexSize = indexSize - 1; System.out.println("Total Index in LinkedList (counting from O) "+realIndexSize); System.out.println(); System.out.println(); System.out.println("Please choose desirable index to view its element"); System.out.println(); int SpecIndex = console.nextInt(); System.out.println(); System.out.println("The element is "+listA.get(SpecIndex)+" at index "+SpecIndex); } catch ( Exception e ) { System.out.println(); System.out.println("LinkedList does not contain index requested"); } Switchers(); } //10 --- DONE public void PrintReverseOrder() { System.out.println(); System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Reverse the list in the LinkedList container. |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); ListIterator itr = listA.listIterator(); System.out.println("Iterating through elements of Java LinkedList using ListIterator in forward direction..."); while(itr.hasNext()) { System.out.println(itr.next()); } System.out.println("Iterating through elements of Java LinkedList using ListIterator in reverse direction..."); while(itr.hasPrevious()) { System.out.println(itr.previous()); } Switchers(); } //11 --- DONE public void ClearAll() { System.out.println("+---------------------------------------------------------------------+"); System.out.println("| Clear entire LinkedList |"); System.out.println("+---------------------------------------------------------------------+"); System.out.println(); listA.clear(); System.out.println("All element has been removed"); System.out.println(); System.out.println(); if (listA.isEmpty()){ System.out.println("Linked list is empty"); } else{ System.out.println (listA); } Switchers(); } //12 --- DONE public void CheckEmptyList() { if (listA.isEmpty()){ System.out.println(); System.out.println("Linked list is empty"); System.out.println(); } else{ System.out.println (listA); } Switchers(); } //13 ---DONE public void Quit() { System.exit(0); } public static void main (String args[]) { LinkedListv4 MYlinkList = new LinkedListv4(); MYlinkList.Switchers(); }
}
Do modifications in Switchers() method:
public void Switchers(){ boolean exit=false; do{ System.out.println("| a. Append a specified elements to the list"); System.out.println("| b. Add an element at specified index"); System.out.println("| c. Add a new elements in front of the list"); System.out.println("| d. Add new elements with a specified value into the list alternately"); System.out.println("| e. Remove the first element in the list"); System.out.println("| f. Find an element at a specified position in the list"); System.out.println("| g. Replace the elements at a specified index"); System.out.println("| h. Print the list"); System.out.println("| i. Print the list from a specified index"); System.out.println("| j. Print the list in reverse order"); System.out.println("| k. Remove all elements in the list"); System.out.println("| l. Check for empty list"); System.out.println("| q. Quit"); System.out.print("Enter your choice: "); question = console.next().charAt(0); System.out.println(); switch (question){ case 'a': AppendElement(); break; case 'b': AddElementSpecific(); break; case 'c': AddElementFront(); break; case 'd': EditExistingList(); break; case 'e': RemoveFirstElement(); break; case 'f': ViewElements(); break; case 'g': ReplaceElement(); break; case 'h': PrintList(); break; case 'i': PrintListSpecific(); break; case 'j': PrintReverseOrder(); break; case 'k': ClearAll(); break; case 'l': CheckEmptyList(); break; case 'q': Quit(); exit=true; break; case 'Q': Quit(); exit=true; break; default: System.out.println("Invalid Input"); System.out.println(); break; } } while(!exit); }
Ads