In: Computer Science
Create the following java program with class list that outputs: //output List Empty List Empty List Empty Item not found Item not found Item not found Original list Do or do not. There is no try. Sorted Original List Do There do is no not. or try. Front is Do Rear is try. Count is 8 Is There present? true Is Dog present? false List with junk junk Do or moremorejunk do not. There is no try. morejunk Count is 11 List with junk removed Do or do not. There is no try. Count is 8 List Full List Full List Full List Full List Full List Full After filling List dummy dummy dummy dummy dummy dummy dummy Do or do not. There is no try. Count is 15 After removing dummy Do or do not. There is no try. Count is 8
// main
public class AssignmentThree
{
public static void main(String[] args)
{
List myList = new List(15);
// Cause List Empty Message
myList.removeFront();
myList.removeRear();
myList.removeItem("a");
// Cause Not found message
myList.addToFront("x");
myList.removeItem("y");
myList.removeItem("x");
myList.addAfterItem("x",
"z");
myList.addBeforeItem("x",
"z");
// Normal behavior
myList.addToFront("not.");
myList.addToFront("or");
myList.addToRear("is");
myList.addToRear("try.");
myList.addAfterItem("is",
"no");
myList.addBeforeItem("is",
"There");
myList.addToFront("Do");
myList.addAfterItem("or",
"do");
myList.print("Original
list");
myList.printSorted("Sorted Original
List");
sop("\nFront is " +
myList.getFront());
sop("Rear is " +
myList.getRear());
sop("Count is " +
myList.askCount());
sop("Is There present? " +
myList.isPresent("There"));
sop("Is Dog present? " +
myList.isPresent("Dog"));
myList.addToFront("junk");
myList.addToRear("morejunk");
myList.addAfterItem("or",
"moremorejunk");
myList.print("List with
junk");
sop("Count is " +
myList.askCount());
myList.removeFront();
myList.removeRear();
myList.removeItem("moremorejunk");
myList.print("List with junk
removed");
sop("Count is " +
myList.askCount());
sop("");
// Cause List Full message
for(int ii = 0; ii < 10;
++ii)
{
myList.addToFront(DUMMY);
}
myList.addToRear(DUMMY);
myList.addBeforeItem("no",
DUMMY);
myList.addAfterItem("There",
DUMMY);
myList.print("After filling
List");
sop("Count is " +
myList.askCount());
while(myList.isPresent(DUMMY))
myList.removeItem(DUMMY);
myList.print("After removing " +
DUMMY );
sop("Count is " +
myList.askCount());
}
private static void sop(String s)
{
System.out.println(s);
}
private static final String DUMMY = "dummy";
}
Following is MyList implementation for your output as:
Java Code:
import java.util.Comparator;
public class MyList {
private Node start;
private Node end;
private int size;
private int capacity;
public MyList(int capacity) {
start = null;
end = null;
size = 0;
this.capacity = capacity;
}
/* Function to check if list is empty */
public boolean isEmpty() {
return start == null;
}
public void removeFront() {
if (isEmpty()) {
System.out.println("List Empty");
return;
} else {
start =
start.next;
size--;
return;
}
}
public void removeRear() {
if (size == 0) {
System.out.println("List Empty");
return;
}
if (start.next == null) {
start=null;
end=null;
size=0;
}
Node node = start.next;
while (node.next != null) {
node =
node.next;
}
node.next = null;
end = node;
size--;
}
public void removeItem(String key) {
if (size == 0) {
System.out.println("List Empty");
return;
}
if (start.next == null) {
if
(start.key.equals(key)) {
start=null;
end=null;
size=0;
} else {
System.out.println("");
}
return;
}
Node node = start;
Node nodeNext = node.next;
while (nodeNext.next != null)
{
if
(nodeNext.key.equals(key)) {
node.next=nodeNext.next;
} else {
node = node.next;
nodeNext=nodeNext.next;
}
}
if (nodeNext.key.equals(key))
{
node.next=null;
end =
node;
}
size--;
}
/**
* addToFront
* @param ele
*/
public void addToFront(String ele) {
if (size == capacity) {
System.out.println("List Full");
return;
} else {
Node node = new
Node(ele, null);
size++;
if (start ==
null) {
start = node;
end = start;
} else {
node.next = start;
start = node;
}
}
}
/**
* addAfterItem
* @param key1
* @param key2
*/
public void addAfterItem(String key1, String key2)
{
Node tmp = start;
Node refNode = null;
/**
* Traverse till given element
*/
while(true){
if(tmp == null){
break;
}
if(tmp.key.equals(key1)){
//found the key1 node, add after add k2 node
refNode = tmp;
break;
}
tmp = tmp.next;
}
if(refNode != null){
//add element after the target node
Node newNode = new Node(key2,tmp.next);
if(tmp == end){
end = newNode;
}
tmp.next=newNode;
size++;
} else {
System.out.println("");
}
}
/**
* addBeforeItem
* @param key1
* @param key2
*/
public void addBeforeItem(String key1, String key2)
{
Node current = start;
//check here
Node prev = null;
if (start != null) {
while (current != null) {
if (current.key.equals(key1)) {
Node newNode = new Node(key2,current);
//check here
if (prev != null) {
prev.next = newNode;
size++;
}
return;
}
//check here
prev = current;
current = current.next;
}
}
}
public void addToRear(String ele) {
if (size == capacity) {
System.out.println("List Full");
} else {
Node node = new
Node(ele, null);
size++;
if (start ==
null) {
start = node;
end = start;
} else {
end.next = (node);
end = node;
}
}
}
public String getFront() throws Exception {
if (isEmpty()) {
System.out.println("List Empty");
return
null;
} else {
return
start.key;
}
}
public String getRear() {
if (isEmpty()) {
System.out.println("List Empty");
return
null;
} else {
return
end.key;
}
}
public int askCount() {
return size;
}
public boolean isPresent(String key) {
if (size == 0) {
System.out.println("List Empty");
return
false;
}
if (start.next == null) {
System.out.println();
if
(start.key.equals(key)) {
return true;
} else {
return false;
}
}
Node node = start;
node = start.next;
while (node.next != null) {
if
(node.key.equals(key)) {
return true;
} else {
node = node.next;
}
}
if (node.key.equals(key)) {
return
true;
} else {
return
false;
}
}
public void print(String msg) {
System.out.println(msg);
if (size == 0) {
System.out.println("List Empty");
return;
}
if (start.next == null) {
System.out.println(start.key);
return;
}
Node node = start;
System.out.print(start.key + "
");
node = start.next;
while (node.next != null) {
System.out.print(node.key + "");
node =
node.next;
}
System.out.println(node.key +
"\n");
}
public void printSorted(String string) {
}
/**
* Node as nested class
*/
private static class Node implements
Comparator<String> {
/*-----------------------*
* Variable Declarations *
*-----------------------*/
private String key; // The data in
the node
private Node next; // The next
node
/**
* Constructs a new TreeNode
*
* @param key
*/
private Node(String key) {
this(key,
null);
}
/**
* Constructs a new TreeNode
*
* @param key
* @param next
*/
private Node(String key, Node next)
{
this.key =
key;
this.next =
next;
}
@Override
public int compare(String key1,
String key2) {
if (key1 ==
key2) {
return 0;
}
if (key1 == null) {
return -1;
}
if (key2 == null) {
return 1;
}
return key1.compareTo(key2);
}
}
}
public class AssignmentThree {
public static void main(String[] args) throws
Exception {
MyList myList = new
MyList(15);
// Cause List Empty Message
myList.removeFront();
myList.removeRear();
myList.removeItem("a");
// Cause Not found message
myList.addToFront("x");
myList.removeItem("y");
myList.removeItem("x");
myList.addAfterItem("x",
"z");
myList.addBeforeItem("x",
"z");
// Normal behavior
myList.addToFront("not.");
myList.addToFront("or");
myList.addToRear("is");
myList.addToRear("try.");
myList.addAfterItem("is",
"no");
myList.addBeforeItem("is",
"There");
myList.addToFront("Do");
myList.addAfterItem("or",
"do");
myList.print("Original
list");
myList.printSorted("Sorted Original
List");
sop("\nFront is " +
myList.getFront());
sop("Rear is " +
myList.getRear());
sop("Count is " +
myList.askCount());
sop("Is There present? " +
myList.isPresent("There"));
sop("Is Dog present? " +
myList.isPresent("Dog"));
myList.addToFront("junk");
myList.addToRear("morejunk");
myList.addAfterItem("or",
"moremorejunk");
myList.print("List with
junk");
sop("Count is " +
myList.askCount());
myList.removeFront();
myList.removeRear();
myList.removeItem("moremorejunk");
myList.print("List with junk
removed");
sop("Count is " +
myList.askCount());
sop("");
// Cause List Full message
for (int ii = 0; ii < 10; ++ii)
{
myList.addToFront(DUMMY);
}
myList.addToRear(DUMMY);
myList.addBeforeItem("no",
DUMMY);
myList.addAfterItem("There",
DUMMY);
myList.print("After filling
List");
sop("Count is " +
myList.askCount());
while
(myList.isPresent(DUMMY))
myList.removeItem(DUMMY);
myList.print("After removing " +
DUMMY);
sop("Count is " +
myList.askCount());
}
private static void sop(String s) {
System.out.println(s);
}
private static final String DUMMY = "dummy";
}
Output:
List Empty
List Empty
List Empty
Original list
Do ordonot.Thereisnotry.
Front is Do
Rear is try.
Count is 8
Is There present? true
Is Dog present? false
List with junk
junk Doormoremorejunkdonot.Thereisnotry.morejunk
Count is 11
If you have any query please comment. Thanks!