Write a testing class that is similar to linkedListApplication class defined in
ID: 3752806 • Letter: W
Question
Write a testing class that is similar to linkedListApplication class defined in p2-1 to test program p2-2 DoubleLinkedList. Make sure that you understand each operation in the program.
import java.util.*;
public final class linkedListApplication
{
public static void main(String args[])
{
int Key, preKey;
int NewItem;
/* create an empty linked list */
LinkedList LL = new LinkedList();
/* create a linked list of 5 nodes */
Random rnd = new Random();
for (int i = 0; i < 5; i++)
{
NewItem = rnd.nextInt(101) ;
LL.Append(NewItem);
}
/* print the list */
System.out.println("The following are the items/integers in the current linked list from the header:");
// LL.PrintList();
LL.ShowLinkedList();
System.out.println("Enter 1 for search, 2 for insertion, 3 for deletion, 4 for append, 5 for remove, 6 for exit");
int s = Integer.parseInt(new Scanner(System.in).nextLine());
while (s == 1 || s == 2 || s == 3 || s == 4 || s == 5)
{
if (s == 1)
{
System.out.println("Enter an key/integer that you want to search:");
Key = Integer.parseInt(new Scanner(System.in).nextLine());
Node n = LL.Search(Key);
if (n != null)
{
System.out.printf("The item/integer is found: %1$s" + " ", n.item);
}
else
{
System.out.println("there is no such key!");
}
};
if (s == 2)
{
System.out.println("Enter a new item/integer that you want to insert:");
NewItem = Integer.parseInt(new Scanner(System.in).nextLine());
System.out.println("Enter the preKey/integer that the new item will follow:");
preKey = Integer.parseInt(new Scanner(System.in).nextLine());
LL.Insert(NewItem, preKey);
System.out.println("The items/integers of the current linked list from the header:");
LL.ShowLinkedList();
};
if (s == 3)
{
System.out.println("Enter the key/integer of the item that you want to delete:");
Key = Integer.parseInt(new Scanner(System.in).nextLine());
LL.Delete(Key);
System.out.println("The items/integers in the current linked list from the header.");
LL.ShowLinkedList();
};
if (s == 4)
{
System.out.println("Enter the item/integer that you want to append:");
NewItem = Integer.parseInt(new Scanner(System.in).nextLine());
LL.Append(NewItem);
System.out.println("The items/integers in the current linked list from the header");
LL.ShowLinkedList();
};
if (s == 5)
{
Node RemoveNode = LL.Remove();
if (RemoveNode != null)
{
System.out.printf("The removed item is: %1$s" + " ", RemoveNode.item);
System.out.println("The items/integers in the current linked list from the header");
LL.ShowLinkedList();
}
else
{
System.out.println("The linked list is empty!");
}
};
System.out.println(" ");
System.out.println("Enter 1 for search, 2 for insertion, 3 for deletion, 4 for append, 5 for remove");
s = Integer.parseInt(new Scanner(System.in).nextLine());
}
System.out.println("Goodbye!");
}
}
Explanation / Answer
please give thumbs up, given design has some weakness, i design class as you needed with some good modification.
CODE:
import java.util.*;
/**
*
* @author VISHAL
*/
public class DoubleLinkedList
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of linkedList */
DoubleLinkedList list = new DoubleLinkedList();
char ch;
Random rnd = new Random();
for (int i = 0; i < 5; i++)
{
list.insertAtStart(rnd.nextInt(101)) ;
}
System.out.println("The following are the items/integers in the current linked list from the header:");
list.display();
do
{
System.out.println("1. insert at begining");
System.out.println("2. insert at end");
System.out.println("3. insert at position");
System.out.println("4. delete at position");
System.out.println("5. check empty");
System.out.println("6. get size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
list.insertAtStart( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer element to insert");
list.insertAtEnd( scan.nextInt() );
break;
case 3 :
System.out.println("Enter integer element to insert");
int num = scan.nextInt() ;
System.out.println("Enter position");
int pos = scan.nextInt() ;
if (pos < 1 || pos > list.getSize() )
System.out.println("Invalid position ");
else
list.insertAtPos(num, pos);
break;
case 4 :
System.out.println("Enter position");
int p = scan.nextInt() ;
if (p < 1 || p > list.getSize() )
System.out.println("Invalid position ");
else
list.deleteAtPos(p);
break;
case 5 :
System.out.println("Empty status = "+ list.isEmpty());
break;
case 6 :
System.out.println("Size = "+ list.getSize() +" ");
break;
default :
System.out.println("Wrong Entry ");
break;
}
/* Display List */
list.display();
} while (choice == 1 || choice==2 ||choice == 3 || choice==4 ||choice == 5 || choice==6);
System.out.println("Goodbye!");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.