Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sequences As we have already seen, sequences are an abstract datatype representi

ID: 3591400 • Letter: S

Question

Sequences

As we have already seen, sequences are an abstract datatype representing the notion of ordered collections of objects of the same type. This tells us what the objects which inhabit this datatype look like, but not the operations that we can reasonably perform on them. To help with this, we can turn to the Sequence<T> interface given below in the Java programming language:

public interface Sequence<T> {

    /**

     * Adds the specified object to the end of the sequence.

     *

     * @param obj object to be appended to this sequence

     */

    void add(T obj);

    /**

     * Adds the specified object at the given position in the sequence.

     *

     * @param idx index at which the specified object is to be inserted

     * @param obj object to be appended to this sequence

     * @throws IndexOutOfBoundsException if the index is out of range

     *         (index < 0 || index > size())

     */

    void add(int idx, T obj) throws IndexOutOfBoundsException;

    /**

     * Removes all of the elements from the sequence.

     */

    void clear();

    /**

     * Returns the object at the specified position in the sequence.

     *

     * @param idx index of the element to return

     * @return the object at the specified position in the sequence

     * @throws IndexOutOfBoundsException if the index is out of range

     *         (index < 0 || index > size())

     */

    T get(int idx) throws IndexOutOfBoundsException;

    /**

     * Returns {@code true} if the sequence contains the specified object and

     * {@code false} otherwise.

     *

     * @param obj the object to find in the sequence

     * @return {@code true} if the sequence contains the specified object and

     *         {@code false} otherwise

     */

    boolean contains(T obj);

    /**

     * Returns the index of the first occurrence of the specified object in

     * this sequence, or -1 if object is not present.

     *

     * @param obj the object to find in the sequence

     * @return the index of the first occurrence of the specified object in

     *         this sequence, or -1 if object is not present

     */

    int indexOf(T obj);

    /**

     * Returns {@code true} if the sequence is empty and {@code false}

     * otherwise.

     *

     * @return {@code true} if the sequence is empty and {@code false}

     *         otherwise

     */

    boolean isEmpty();

    /**

     * Removes the object at the specified position in the sequence.

     *

     * @param idx index of the element to remove

     * @return the object previously at the specified position

     * @throws IndexOutOfBoundsException if the index is out of range

     *         (index < 0 || index > size())

     */

    T remove(int idx) throws IndexOutOfBoundsException;

    /**

     * Remove the first occurrence of the specified object from the sequence,

     * if it is present.

     *

     * @param obj the object to remove

     * @return {@code true} if the sequence contained the specified object and

     *         {@code false} otherwise

     */

    boolean remove(T obj);

    /**

     * Returns the number of elements in the sequence.

     *

     * @return the number of elements in the sequence

     */

    int size();

    /**

     * Returns an array containing all of the elements in the sequence in the

     * proper order (from first to last).

     *

     * @return an array containing the elements of the sequence

     */

    T[] toArray();

}

The first part of your project is to define a LinkedList<T> class which implements this interface. You will notice that there is no requirement to provide a definition for a toString() member function, this is left to your own discretion. In addition, pay attention to the specifications of the individual member functions: some of them may be of use in implementing others. Lastly, the interface only defines the minimal set of functions/methods that your implementation must provide (i.e. the public API through which objects of the LinkedList<T> type may be manipulated). This does not mean that you ought not define additional functions to help in implementing these.

Explanation / Answer

SimpleList implementation

publlic interface list<L> {

    /**

     * Adds the specified node to the end of the list.

     *

     * @param L node to be appended to the list

     */

public abstract add(L node);

/**

     * Adds the specified node at the given position in the list.

     *

     * @param posn position at which the specified node is to be inserted

     * @param L node to be appended to this list

     * @throws IllegalPositionArgException if the position is out of range

     *         (position< 0 || position> size())

     */

public abstract add(int posn,L node) throws IllegalPositionArgException ;

/**

     * Removes all of the nodes from the list.

     */

public abstract clear();

/**

     * Returns the node at the specified position in the list.

     *

     * @param posn position of the node to return

     * @return the node at the specified position in the list

     * @throws IllegalPositionArgException  if the position is out of range

     *         (position < 0 || position> size())

     */

public abstract L get(int posn) throws IllegalPositionArgException

/**

     * Returns {@code true} if the list contains the specified node and

     * {@code false} otherwise.

     *

     * @param L the node to find in the list

     * @return {@code true} if the list contains the specified node and

     *         {@code false} otherwise

     */

public abstract boolean contains( L node);

/**

     * Returns the position of the first occurrence of the specified node in

     * this list, or -1 if node is not present.

     *

     * @param  L the node to find in the list

     * @return the positionof the first occurrence of the specified node in

     *         this list, or -1 if node is not present

     */

int positionOf(L node);

/**

     * Returns {@code true} if the list is empty and {@code false}

     * otherwise.

     *

     * @return {@code true} if the list is empty and {@code false}

     *         otherwise

     */

public abstract boolean isEmpty();

/**

     * Removes the node at the specified position in the list.

     *

     * @param posn position of the element to remove

     * @return the node previously at the specified position

     * @throws IllegalPositionArgException if the position is out of range

     *         (position< 0 || position> size())

     */

public abstract remove(int posn) throws IllegalPositionArgException;

/**

     * Remove the first occurrence of the specified node from the list,

     * if it is present.

     *

     * @param  L the node to remove

     * @return {@code true} if the list contained the specified node and

     *         {@code false} otherwise

public abstract boolean remove( L node);

/**

     * Returns the number of elements in the list.

     *

     * @return the number of elements in the list

     */

public abstract int size();

    /**

     * Returns an array containing all of the elements in the list in the

     * proper order (from first to last).

     *

     * @return an array containing the elements of the list

     */

L[] toArray();

Linked List <T>Class implementation:

public abstract class linkedlist<T> implements list<L>{

protected list<L> rootele;

protected int size;

protected static class node<T>{

protected T nodeele;

protected node<T> postnode;

protected node<T> prenode;

public node(T nodeele, node<T> prenode, node<T>postnode){

this.nodeele=nodeele;

this.prenode=prenode;

this.postnode=postnode;

}

public int hashcode()

{

return((nodeele==null)?=0:nodeele.hashcode());

}

public boolean equals(object ob){

boolean isEqual=false;

if(ob instance of node){

if(((node<?>)ob).nodeele==nodeele){

isEqual=true;

}

else

{

if(nodeele!=null){

if(nodeele.equals(((node<?>)on).node)){

isEqual=true;

}

}

}

}

return isEqual;

}

The above code is the linked list implementation. Here nodeele represents the node element, post node refers the next node and prenode refers the previous node.

If node element is equal to null it returns 0 and if it is not equal to null it returns some hash code.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote