Question: import java.util.Scanner; public class LinkedList > { / / - - - - - - - - - - - - - - - -
import java.util.Scanner;
public class LinkedList
nested Node class
private static class Node
private E data; reference to the data stored at this node
private Node next; reference to the subsequent node in the list
public NodeE e Node n
data e;
next n;
public E getData
return data;
public Node getNext
return next;
public void setNextNode n
next n;
instance variables of the LinkedList
private Node head null; head node of the list or null if empty
private Node tail null; last node of the list or null if empty
private int size ; number of nodes in the list
public LinkedList constructs an initially empty list
access methods
public int size
return size;
public boolean isEmpty
return size ;
public E first returns but does not remove the first data
if isEmpty
return null;
return head.getData;
public E last returns but does not remove the last data
if isEmpty
return null;
return tail.getData;
update methods
public void addFirstE e adds data e to the front of the list
head new Nodee head; create and link a new node
if size
tail head; special case: new node becomes tail also
size;
public void addLastE e adds data e to the end of the list
Node newest new Nodee null; node will eventually be the tail
if isEmpty
head newest; special case: previously empty list
else
tail.setNextnewest; new node after existing tail
tail newest; new node becomes the tail
size;
public E removeFirst removes and returns the first data
if isEmpty return null; nothing to remove
E answer head.getData;
head head.getNext; will become null if list had only one node
size;
if size
tail null; special case as list is now empty
return answer;
public E removeLast removes and returns the last data
if isEmpty return null; nothing to remove
E answer tail.getData;
if head tail check for only one item on the list
head null;
tail null;
size ;
return answer;
Node p head; find the next to last item
Node prev;
do
prev p;
p pgetNext;
while p tail;
tail prev; make the next to last item the last item
prev.next null;
size;
return answer;
public int indexOfE e return the position of a value in the list
if isEmpty return ;
Node p head;
for int i ; i;
@Override
public boolean equalsObject o
if o null return false;
if o instanceof LinkedList return false;
LinkedList listLinkedList o;
if size listsize return false;
Node p head;
Node q listhead;
while p null && q null
if pgetDataequalsqgetData return false;
p pgetNext;
q qgetNext;
return true;
@Override
public String toString
String result ;
Node p head;
while p null
result result pgetData;
p pgetNext;
result result.substring;
return result;
public static void mainString args
Scanner s new ScannerSystemin;
LinkedList list new LinkedList;
int n snextInt;
for int i; i list new LinkedList;
n snextInt;
for int i; i
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
