Linked List: Fruit 1. You are to create a class Fruit that has the following var
ID: 3581099 • Letter: L
Question
Linked List: Fruit
1. You are to create a class Fruit that has the following variables:
Fruit next
Fruit previous
String name
2. You are to write a program (name it LinkedListFruitDemo.jav) in which you will:
Create a doubly linked list of Fruits with the following values stored in the variable space name of each node (note that Apple will be the first element in the linked List and Mango the last). Hint: using an array holding the names and a for loop would make this easier.
Apple
Banana
Cantaloupe
Date
Elderberry
Fig
Grape
Honeyberry
Ice Cream Bean
Jostaberry
Kiwi
Lime
Mango
Develop methods that will:
Iterate through (from front to back) the linked list elements printing the values stored in name.
Iterate through (from back to front) the linked list elements printing the values stored in name.
Ask the user for the name of a new fruit and add a new element (with that fruit stored as name) to the end of the linked list.
Ask the user to enter the name of a fruit and delete the element in the linked list that has the same name (if there is such an element)
Present to the user a menu that will allow them to select one of the options (methods) above.
Explanation / Answer
import java.util.Scanner;
/**
* @author zubaeyr
*
*/
public class LinkedListFruitDemo {
public static String[] fruitNames = new String[] {"Apple", "Banana", "Cantaloupe", "Date",
"Elderberry", "Fig", "Grape", "Honeyberry", "Ice Cream Bean", "Jostaberry", "Kiwi", "Lime", "Mango" };
private Fruit head;
public LinkedListFruitDemo() {
/* Add all the given fruits to the linked list */
for (int i=0; i<fruitNames.length; i++) {
Fruit fruit = new Fruit(null, null, fruitNames[i]);
addFruitAtEnd(fruit);
}
int choice = -1;
String fruitName;
Scanner choiceScanner = new Scanner(System.in);
Scanner stringScanner = new Scanner(System.in);
while (true) {
System.out.print(" "
+ "1. Print (Forward Order) "
+ "2. Print (Reverse Order) "
+ "3. Add Fruit "
+ "4. Delete Fruit "
+ "5. Exit "
+ "Enter your option : ");
choice = choiceScanner.nextInt();
switch(choice) {
case 1 :
printElementsForward();
break;
case 2 :
printElementsReverse();
break;
case 3 :
System.out.print("Enter fruit name : ");
fruitName = stringScanner.nextLine();
addFruitAtEnd(new Fruit(null, null, fruitName));
break;
case 4 :
System.out.print("Enter fruit name : ");
fruitName = stringScanner.nextLine();
deleteFruit(fruitName);
break;
case 5 :
choiceScanner.close();
stringScanner.close();
System.exit(0);
break;
default :
System.out.println("Please enter a correct option ");
}
}
}
/**
* Adds the given fruit to the end of the list
*
* @param fruit
*/
public void addFruitAtEnd(Fruit fruit) {
/* If the list is empty, then the given element is the first element
* So make head point to it
* */
if (head == null) {
head = fruit;
return;
}
/* Make another reference of head
* Iterate over it to get to the last element of the list
* Attach the given element at the end
* */
Fruit current = head;
while(current.getNext() != null) {
current = current.getNext();
}
fruit.setPrevious(current);
current.setNext(fruit);
}
/**
* Deletes the Fruit with the given name in the list
*
* @param fruitName
*/
public void deleteFruit(String fruitName) {
Fruit current = head;
while(current != null) {
if (current.getName().equals(fruitName)) {
Fruit prev = current.getPrevious();
Fruit next = current.getNext();
/* If it is the first node, then make the head pointer point to second node */
if (prev == null) {
head = next;
head.setPrevious(null);
}
else {
/* If it is previous node is not null,
* then make the next node point to previous node and vice versa
* */
prev.setNext(next);
if (next != null) {
next.setPrevious(prev);
}
}
/* Free references from current node */
current.setNext(null);
current.setPrevious(null);
System.out.println("Deleted the fruit : "+fruitName +" from the list ");
break;
}
current = current.getNext();
}
}
/**
* Prints the elements in the forward order
*/
public void printElementsForward() {
Fruit tempCopy = head;
System.out.println("The fruits in the list are [forward order] : ");
while(tempCopy != null) {
System.out.printf(tempCopy.getName()+", ");
tempCopy = tempCopy.getNext();
}
System.out.println(" ");
}
/**
* Prints the elements in the reverse order
*/
public void printElementsReverse() {
/*
* Make a temporary reference of the head
* Traverse to the end of the list
* */
Fruit temp = head;
while(temp.getNext() != null) {
temp = temp.getNext();
}
System.out.println("The fruits in the list are [reverse order] : ");
while(temp != null) {
System.out.printf(temp.getName()+", ");
temp = temp.getPrevious();
}
System.out.println(" ");
}
public static void main(String args[]) {
new LinkedListFruitDemo();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.