Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Complete the method removeLast that returns and removes the last element of a linked list. Update the links directly. Do not use an iterator. import

Complete the method removeLast that returns and removes the last element of a linked list. Update the links directly. Do not use an iterator.

import java.util.NoSuchElementException;

/** A linked list is a sequence of nodes with efficient element insertion and removal. This class contains a subset of the methods of the standard java.util.LinkedList class. */ public class LinkedList { private Node first; /** Constructs an empty linked list. */ public LinkedList() { first = null; }

public Object removeLast() { if (first == null) return null; Node pre = null; Node pos = first; while (pos.next != null) { pre = pos; pos = pos.next; } } /** Returns the first element in the linked list. @return the first element in the linked list */ public Object getFirst() { if (first == null) { throw new NoSuchElementException(); } return first.data; }

/** Removes the first element in the linked list. @return the removed element */ public Object removeFirst() { if (first == null) { throw new NoSuchElementException(); } Object element = first.data; first = first.next; return element; }

/** Adds an element to the front of the linked list. @param element the element to add */ public void addFirst(Object element) { Node newNode = new Node(); newNode.data = element; newNode.next = first; first = newNode; } /** Returns an iterator for iterating through this list. @return an iterator for iterating through this list */ 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;

/** Constructs an iterator that points to the front of the linked list. */ public LinkedListIterator() { position = null; previous = null; isAfterNext = false; } /** Moves the iterator past the next element. @return the traversed element */ public Object next() { if (!hasNext()) { throw new NoSuchElementException(); } previous = position; // Remember for remove isAfterNext = true;

if (position == null) { position = first; } else { position = position.next; }

return position.data; } /** Tests if there is an element after the iterator position. @return true if there is an element after the iterator position */ public boolean hasNext() { if (position == null) { return first != null; } else { return position.next != null; } } /** Adds an element before the iterator position and moves the iterator past the inserted element. @param element the element to add */ public void add(Object element) { if (position == null) { addFirst(element); position = first; } else { Node newNode = new Node(); newNode.data = element; newNode.next = position.next; position.next = newNode; position = newNode; }

isAfterNext = false; } /** Removes the last traversed element. This method may only be called after a call to the next() method. This method is not implemented for this exercise. */ public void remove() { throw new UnsupportedOperationException(); }

/** Sets the last traversed element to a different value. @param element the element to set */ public void set(Object element) { if (!isAfterNext) { throw new IllegalStateException(); } position.data = element; } } }

The method should be

while (pos.next != null)

{

pre = pos;

pos = pos.next;

}

/* Your code goes here */

pre.next = null;

return pos.data;/* Your code goes here */

}

but the tests are coming back wrongimage text in transcribed

C Expected: C ABCAB Expected: ABCAB B Expected: B ABCA Expected: ABCA A Expected: A ABC Expected: ABC C Expected: C AB Expected: AB B Expected: B A Expected: A null Expected: A true Expected: false

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions