P1610add Method Boolean Contains Object Obj Checks Whether Linkedlist Implementation Conta Q37169181

•• P16.10Add a method boolean contains(Object obj) that checkswhether our LinkedList implementation contains a given object.Implement this method by directly traversing the links, not byusing an iterator.

need Test Program

import java.util.NoSuchElementException;

public class LinkedList

{

private Node first;

public LinkedList()

{

first = null;

}

public Object getFirst()

{

if (first == null) { throw new NoSuchElementException(); }

return first.data;

}

public Object removeFirst()

{

if (first == null) { throw new NoSuchElementException(); }

Object element = first.data;

first = first.next;

return element;

}

public void addFirst(Object element)

{

Node newNode = new Node();

newNode.data = element;

newNode.next = first;

first = newNode;

}

public ListIterator listIterator()

{

return new LinkedListIterator();

}

class Node

{

public Object data;

public Node next;

}

class LinkedListIterator implements ListIterator

{

private Node position;

private Node previous;

private boolean isAfterNext;

public LinkedListIterator()

{

position = null;

previous = null;

isAfterNext

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.