Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i need the code where it says //code goes here// Complete the method removeLast that returns and removes the last element of a linked list.

i need the code where it says //code goes here//
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Complete the method removeLast that returns and removes the last element of a linked list. Update the links directly. Do not use an iterator 1. 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. Linkedt ist class. 9 public class LinkedList 10f 11 private Node first; 12 14 Constructs an empty linked list. Adds an element to the front of the 2 inked list. Adds on element to the front of the linked list. eparam element the element to add public void addFirst(Object element) \{ Node newlode = new Node( ; newNode. data = element; newNode. next = first; first = newNode; \} Returns an iterotor for iterating through this list. ereturn on iterator for iterating through this list Returns an iterator for iterating through this list. ereturn an iterator for iterating through this l ist public ListIterator listIterator() class Node \{ public Object data; public Node next; class LinkedListIterator implements ListIterator 8586878889909192939495classLinkedistiteratorimplementsListiteratoriprivateNodeposition;privateNodeprevious;privatebooleanisAfterNext;Constructsaniteratorthatpointstothefrontofthelinkedlist.publicLinkedListiteratorCfposition=null;previous=null;isAfterNext=false; Moves the iterator past the next element.. ereturn the traversed element public Object next() if (IhasNext()) { throw new NoSuchElementException(); } previous = position; /I Remembe { for remove isAfterNext = true; if (position = null) \{ position = first; Adds an element before the iterator position. and moves the iterator past the inserted element. eparam element the element to add Adds an element before the iterator position and moves the iterator past the inserted element. gporam element the element to odd 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; 3. 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()

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

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

Recommended Textbook for

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions