Kim J.

asked • 11/26/20

I need help with the two empty methods in the code below. You need to loop though head to tail before returning removefirstoccurance(Object o) or removeLastoccurance(Object o) ??

public class LinkedListDeque<E> extends AbstractDeque<E> {


private Deque<Integer>list;

private Node<E> head;

private Node<E> tail;

private int size;



@Override

public boolean offerFirst(E e) {

Node<E> temp = new Node<>(e);

if(isEmpty()) {

tail = head = temp;

}else {

head.prev=temp;

temp.next=head;

head=temp;

}

size++;

return true;

}


@Override

public boolean offerLast(E e) {

Node<E> temp = new Node<>(e);

if (isEmpty()) {

tail = head = temp;

}else {

temp.prev=tail;

tail.next=temp;

tail=temp;

}

size++;

return true;

}

@Override

public E pollFirst() {

Node<E>temp;

if (isEmpty()) {

return null;

}

temp = head;

head = head.next;

if(head!=null) {

head.prev = null;

}else {

tail = null;

}

size--;

return temp.value;

}


@Override

public E pollLast() {

Node<E>temp;

if (isEmpty()) {

return null;

}

temp = tail;

tail = tail.prev;

if(tail != null) {

tail.next=null;

}else {

head=null;

}

size--;

return temp.value;

}

@Override

public E peekFirst() {

Node<E>temp;

if (isEmpty()) {

return null;

}

if(head !=null) {

temp=head;

}else {

temp=tail;

}

return temp.value;

}

@Override

public E peekLast() {

Node<E>temp;

if (isEmpty()) {

return null;

}

if(tail != null) {

temp=tail;

}else {

temp=head;

}

return temp.value;

}



public boolean removeFirstOccurrence(Object o) {

}


public boolean removeLastOccurrence(Object o) {

}

@Override

public boolean contains(Object o) {

while(head != null) {

if(head.value.equals(o)) {

return true;

}

}

return false;

}

@Override

public int size() {

return size;

}

@Override

public boolean isEmpty() {

return size == 0;

}

@Override

public void clear() {

head = null;

tail = null;

size = 0;

}

private boolean remove(Node<E> n) {

if(isEmpty()) {

return false;

}

if(head.equals(n)) {

head = head.next;

return true;

}

return false;

}



public class Node<R>{

private R value;

private Node<R> next;

private Node<R> prev;

private Node(R value) {

this(value,null,null);

}

private Node(Node<R> next,Node<R> prev) {

this(null,next,prev);

}

private Node(R value,Node<R> next, Node<R> prev) {

this.value = value;

this.next = next;

this.prev = prev;

}

public String toString() {

return null;

}

}

}




1 Expert Answer

By:

Connor K. answered • 02/12/21

Tutor
5 (2)

Computer Science Tutor for Debugging and Project Help

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.