Question

Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

Part I:

Create a doubly linked circular list class named LinkedItemList that implements the following interface:

/**

* An ordered list of items.

*/

public interface ItemList<E> {

     /**

      * Append an item to the end of the list

      *

      * @param item – item to be appended

      */

public void append(E item);

     /**

      * Insert an item at a specified index position

      *

      * @param item – item to be inserted

       * @param index – index position where to insert the item

      * @throw IndexOutOfBoundsException if index is < 0 or > no of items

      */

public void insert(E item, int index);

/**

      * Return an item at a specified index

      *

      * @param index – index of the item to return

      * @return the item at the specified index

      * @throw IndexOutOfBoundsException if index is < 0 or >= no of items

      */

public E get(int index);

/**

      * Remove an item at a specified index

      *

      * @param index – index of the item to be removed

      * @return the removed item

      * @throw IndexOutOfBoundsException if index is < 0 or >= no of items

      */

public E removeAtIndex (int index);

/**

* Return the number of items currently in the list

*

* @return the number of items in the list

*/

public int size();

/**

* Determine if the list is empty

*

* @return true if the list is empty, otherwise false

*/

public boolean isEmpty();

/**

* Clear the list. The list becomes empty

*

*/

public void clear();

}

You will need the appropriate constructors. In addition, you may also implement other methods that may be useful for the assignment (where possible define code in just one place).

Part II:

Write JUnit test program to thoroughly test all of the methods of your LinkedItemList class

Additional Requirements:

The LinkedItemList must be implemented as a doubly linked circular list with a dummy node in place of a first node and last node reference variables.

Provide a fast implementation for append(), e.g., does not need to walk the list to get to the end.

Define and use a private method appendAfter(Node node, E item) which adds a Node to the linked list for the item. Call appendAfter from the append and insert methods. The append and insert methods must not directly create a node.

Define and use a private method removeNode(Node node) which removes a Node from the linked list. Call removeNode from the removeAtIndex method. The removeAtIndex method must not directly remove a node.

Define and use a private method getNode(int index) which returns a node at a given index position. Use getNode in the get, removeAtIndex, and insert methods

Provide a fast implementation for size(), e.g., does not need to walk the list to count the number of items

0 0
Add a comment Improve this question Transcribed image text
Answer #1

==========>ItemList<E>.java

package doublelnkdlst;

public interface ItemList<E> {

public void clean();

public void insrt(E item, int index);

public bool Empty();

public E clearAtIndex(int index);

public void apnd(E item);

public int size();

public E get(int index);

}

=======>LinkedItemList<E>.java

package doublelnkdlst;

public class LinkedItemList<E> implements ItemList<E> {

private Node head;

private Node tail;

private int size;

private class Node {

E item;

Node next;

Node prev;

public Node(E item, Node next, Node prev) {

this.item = item;

this.next = next;

this.prev = prev;

}

}

@Override

public void apnd(E item) {

Node tmp = new Node(item, head, null);

if (head != null) {

head.prev = tmp;

}

head = tmp;

if (tail == null) {

tail = tmp;

}

size++;

}

@Override

public void insrt(E item, int index) {

if (index < 0 && index >= size)

throw new IndexOutOfBoundsException();

Node current = head;

Node previous = null;

while (index >= 0) {

previous = current;

current = current.next;

index--;

}

Node node = new Node(item, head, null);

node.next = current;

node.prev = previous;

current.prev = node;

if (previous != null) {

previous.next = node;

}

if (index == 1)

head = node;

}

@Override

public E get(int index) {

if (index < 0 && index >= size)

throw new IndexOutOfBoundsException();

Node temp = head;

while (index > 0) {

temp = temp.next;

index--;

}

return temp.item;

}

@Override

public E clearAtIndex(int index) {

if (index < 0 && index >= size)

throw new IndexOutOfBoundsException();

Node current = head;

Node previous = null;

while (index > 0) {

previous = current;

current = current.next;

index--;

}

if (previous != null) {

previous.next = current.next;

if (current.next != null)

current.next.prev = previous;

}

else {

head = current.next;

head.prev = null;

}

return current.item;

}

@Override

public int size() {

return size;

}

@Override

public bool Empty() {

if (size == 0) {

return true;

}

return false;

}

@Override

public void clean() {

head = null;

size = 0;

}

}

============>Junit test case class

package doublelnkdlst;

import org.junit.runners.JUnit4;

import junit.framework.Assert;

import org.junit.Test;

import org.junit.runner.RunWith;

@RunWith(JUnit4.class)

public class TestLinkedItemList {

@Test

public void testInsrtAndGet() {

ItemList<Integer> list = new LinkedItemList<>();

list.apnd(1);

list.apnd(2);

list.apnd(3);

Assert.assertEquals(list.get(0), Integer.valueOf(3));

Assert.assertEquals(list.get(1), Integer.valueOf(2));

Assert.assertEquals(list.get(2), Integer.valueOf(1));

}

@Test

public void testclearAtIndex() {

ItemList<Integer> list = new LinkedItemList<>();

list.apnd(1);

list.apnd(2);

list.apnd(3);

Assert.assertEquals(list.clearAtIndex(0), Integer.valueOf(3));

Assert.assertEquals(list.clearAtIndex(1), Integer.valueOf(1));

}

@Test

public void testCleanAllAndEmpty() {

ItemList<Integer> list = new LinkedItemList<>();

list.apnd(1);

list.apnd(2);

list.apnd(3);

list.clean();

Assert.assertEquals(Bool.valueOf(list.Empty()), Bool.valueOf(true));

}}

Add a comment
Know the answer?
Add Answer to:
Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

    Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)    {        this.size = size;    }    /** Reference to list head. */    private Node<E> head = null;    /** The number of items in the list */    private int size = 0;       /** Add an item to the front of the list.    @param item The item to be added    */    public void addFirst(E...

  • Given the Interface Code write a java class that implements this interface and show the working...

    Given the Interface Code write a java class that implements this interface and show the working functionality in the main method: public interface CustomList<T> { /** * This method should add a new item into the <code>CustomList</code> and should * return <code>true</code> if it was successfully able to insert an item. * @param item the item to be added to the <code>CustomList</code> * @return <code>true</code> if item was successfully added, <code>false</code> if the item was not successfully added (note: it...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

  • Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the...

    Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public...

    JAVA - Circular Doubly Linked List Does anybody could help me with this method below(previous)? public E previous() { // Returns the previous Element return null; } Explanation: We have this class with these two implemented inferfaces: The interfaces are: package edu.ics211.h04; /** * Interface for a List211. * * @author Cam Moore * @param the generic type of the Lists. */ public interface IList211 { /** * Gets the item at the given index. * @param index the index....

  • n JAVA, students will create a linked list structure that will be used to support a...

    n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • I need help with the Implementation of an Ordered List (Template Files) public interface Ordered...

    I need help with the Implementation of an Ordered List (Template Files) public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); } import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT