Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code must only be written in IntegerLinkedList.java. 9 Methods need to be implemented so that all the tests in the tester file passes. IntegerLinkedList.java implements

Code must only be written in IntegerLinkedList.java. 9 Methods need to be implemented so that all the tests in the tester file passes. IntegerLinkedList.java implements IntegerNode.java

Your task is to implement the interface described in the file IntegerList.java using a doubly linked list in the file IntegerLinkedList.java.

Your implementation must be a doubly-linked list that maintains both a head and a tail reference. You've also been provided with a node class (IntegerNode.java) that includes a prev and next references.

//IntegerList.java interface//

public interface IntegerList { /* * PURPOSE: * Add the element x to the front of the list. * * PRECONDITIONS: * None. * * Examples: * * If l is {1,2,3} and l.addFront(9) returns, then l is {9,1,2,3}. * If l is {} and l.addFront(3) returns, then l is {3}. */ public void addFront (int x); /* * PURPOSE: * Add the element x to the back of the list. * * PRECONDITIONS: * None. * * Examples: * * If l is {1,2,3} and l.addBack(9) returns, then l is {1,2,3,9}. * If l is {} and l.addBack(9) returns, then l is {9}. */ public void addBack (int x); /* * PURPOSE: * Add the element x at position pos in the list. * * Note: * In a list with 3 elements, the valid positions for addAt are * 0, 1, 2, 3. * * PRECONDITIONS: * pos >= 0 and pos <= l.size() * * Examples: * * If l is {} and l.addAt(9,0) returns, then l is {9}. * If l is {1} and l.addAt(9,0) returns, then l is {9,1}. * If l is {1,2} and l.addAt(9,1) returns, then l is {1,9,2} * If l is {1,2} and l.addAt(9,2) returns, then l is {1,2,9} */ public void addAt (int x, int pos); /* * PURPOSE: * Return the number of elements in the list * * PRECONDITIONS: * None. * * Examples: * If l is {7,13,22} l.size() returns 3 * If l is {} l.size() returns 0 */ public int size(); /* * PURPOSE: * Return the element at position pos in the list. * * PRECONDITIONS: * pos >= 0 and pos < l.size() * * Examples: * If l is {67,12,13} then l.get(0) returns 67 * If l is {67,12,13} then l.get(2) returns 13 * If l is {92} then the result of l.get(2) is undefined. * */ public int get (int pos); /* * PURPOSE: * Remove all elements from the list. After calling this * method on a list l, l.size() will return 0 * * PRECONDITIONS: * None. * * Examples: * If l is {67,12,13} then after l.clear(), l is {} * If l is {} then after l.clear(), l is {} * */ public void clear(); /* * PURPOSE: * Remove all instances of value from the list. * * PRECONDITIONS: * None. * * Examples: * If l is {67,12,13,12} then after l.remove(12), l is {67,13} * If l is {1,2,3} then after l.remove(2), l is {1,3} * If l is {1,2,3} then after l.remove(99), l is {1,2,3} */ public void remove (int value); /* * PURPOSE: * Remove the element at position pos in the list. * * Note: * In a list with 3 elements, the valid positions for removeAt are * 0, 1, 2. * * PRECONDITIONS: * pos >= 0 and pos < l.size() * * Examples: * * If l is {1} and l.removeAt(0) returns, then l is {}. * If l is {1,2,3} and l.removeAt(1) returns, then l is {1,3} * If l is {1,2,3} and l.removeAt(2) returns, then l is {1,2} */ public void removeAt (int pos); /* * PURPOSE: * Return a string representation of the list * * PRECONDITIONS: * None. * * Examples: * If l is {1,2,3,4} then l.toString() returns "{1,2,3,4}" * If l is {} then l.toString() returns "{}" * */ public String toString(); }

//IntegerLinkedList.java// TO DO

public class IntegerLinkedList implements IntegerList { public IntegerLinkedList() { } /* * PURPOSE: * Add the element x to the front of the list. * * PRECONDITIONS: * None. * * Examples: * * If l is {1,2,3} and l.addFront(9) returns, then l is {9,1,2,3}. * If l is {} and l.addFront(3) returns, then l is {3}. */ public void addFront (int x) { } /* * PURPOSE: * Add the element x to the back of the list. * * PRECONDITIONS: * None. * * Examples: * * If l is {1,2,3} and l.addBack(9) returns, then l is {1,2,3,9}. * If l is {} and l.addBack(9) returns, then l is {9}. */ public void addBack (int x) { } /* * PURPOSE: * Add the element x at position pos in the list. * * Note: * In a list with 3 elements, the valid positions for addAt are * 0, 1, 2, 3. * * PRECONDITIONS: * pos >= 0 and pos <= l.size() * * Examples: * * If l is {} and l.addAt(9,0) returns, then l is {9}. * If l is {1} and l.addAt(9,0) returns, then l is {9,1}. * If l is {1,2} and l.addAt(9,1) returns, then l is {1,9,2} * If l is {1,2} and l.addAt(9,2) returns, then l is {1,2,9} */ public void addAt (int x, int pos) { } /* * PURPOSE: * Return the number of elements in the list * * PRECONDITIONS: * None. * * Examples: * If l is {7,13,22} l.size() returns 3 * If l is {} l.size() returns 0 */ public int size() { return -1; } /* * PURPOSE: * Return the element at position pos in the list. * * PRECONDITIONS: * pos >= 0 and pos < l.size() * * Examples: * If l is {67,12,13} then l.get(0) returns 67 * If l is {67,12,13} then l.get(2) returns 13 * If l is {92} then the result of l.get(2) is undefined. * */ public int get (int pos) { return -1; } /* * PURPOSE: * Remove all elements from the list. After calling this * method on a list l, l.size() will return 0 * * PRECONDITIONS: * None. * * Examples: * If l is {67,12,13} then after l.clear(), l is {} * If l is {} then after l.clear(), l is {} * */ public void clear() { } /* * PURPOSE: * Remove all instances of value from the list. * * PRECONDITIONS: * None. * * Examples: * If l is {67,12,13,12} then after l.remove(12), l is {67,13} * If l is {1,2,3} then after l.remove(2), l is {1,3} * If l is {1,2,3} then after l.remove(99), l is {1,2,3} */ public void remove (int value) { } /* * PURPOSE: * Remove the element at position pos in the list. * * Note: * In a list with 3 elements, the valid positions for removeAt are * 0, 1, 2. * * PRECONDITIONS: * pos >= 0 and pos < l.size() * * Examples: * * If l is {1} and l.removeAt(0) returns, then l is {}. * If l is {1,2,3} and l.removeAt(1) returns, then l is {1,3} * If l is {1,2,3} and l.removeAt(2) returns, then l is {1,2} */ public void removeAt (int pos) { } /* * PURPOSE: * Return a string representation of the list * * PRECONDITIONS: * None. * * Examples: * If l is {1,2,3,4} then l.toString() returns "{1,2,3,4}" * If l is {} then l.toString() returns "{}" * */ public String toString() { return "Not implemented."; } } 

//IntegerNode.java//

 public class IntegerNode { IntegerNode next; IntegerNode prev; int value; public IntegerNode() { next = null; value = 0; } public IntegerNode (int val) { value = val; next = null; } public IntegerNode (int val, IntegerNode nxt) { value = val; next = nxt; } public IntegerNode getNext() { return next; } public IntegerNode getPrev() { return prev; } public int getValue() { return value; } public void setNext (IntegerNode nxt) { next = nxt; } public void setPrev (IntegerNode prv) { prev = prv; } public void setValue (int val) { value = val; } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Science For Dummies

Authors: Lillian Pierson ,Jake Porway

2nd Edition

1119327636, 978-1119327639

More Books

Students also viewed these Databases questions