Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Java / Eclipse / SinglyList In this exercise, you will add a non - static method swapNodes ( int node 1 Index, int node 2
JavaEclipseSinglyList
In this exercise, you will add a nonstatic method swapNodesint nodeIndex, int nodeIndex to SinglyLinkedList class from week lecture examples. This method should swap the elements of two nodes where nodeIndex is the index of one node, and nodeIndex is the index of the other node. Here, we assume, the front node of the list is the first node, the node next to the first node is the second node, and so on Thus, the index implies first node, index implies the second node, and so on Write the main method to test the swapNodes method. Hint: You may need to traverse the list.
Code:
package linkedlists;
public class SinglyLinkedList implements Cloneable
private static class Node
private E element;
private Node next;
public NodeE e Node n
element e;
next n;
public E getElement return element;
public Node getNext return next;
public void setNextNode n next n;
private Node head null;
private Node tail null;
private int size ;
public SinglyLinkedList
public int size return size;
public boolean isEmpty return size ;
public E first
if isEmpty return null;
return head.getElement;
public E last
if isEmpty return null;
return tail.getElement;
public void addFirstE e
head new Nodee head;
if size
tail head;
size;
public void addLastE e
Node newest new Nodee null;
if isEmpty
head newest;
else
tail.setNextnewest;
tail newest;
size;
public E removeFirst
if isEmpty return null;
E answer head.getElement;
head head.getNext;
size;
if size
tail null;
return answer;
@SuppressWarningsunchecked
public boolean equalsObject o
if o null return false;
if getClass ogetClass return false;
SinglyLinkedList other SinglyLinkedList o;
if size other.size return false;
Node walkA head;
Node walkB other.head;
while walkA null
if walkA.getElementequalswalkBgetElement return false;
walkA walkA.getNext;
walkB walkB.getNext;
return true;
@SuppressWarningsunchecked
public SinglyLinkedList clone throws CloneNotSupportedException
SinglyLinkedList other SinglyLinkedList super.clone;
if size
other.head new NodeheadgetElement null;
Node walk head.getNext;
Node otherTail other.head;
while walk null
Node newest new NodewalkgetElement null;
otherTail.setNextnewest;
otherTail newest;
walk walk.getNext;
other.tail otherTail;
return other;
public int hashCode
int h ;
for Node walkhead; walk null; walk walk.getNext
h walk.getElementhashCode;
h h h ;
return h;
public String toString
StringBuilder sb new StringBuilder;
Node walk head;
while walk null
sbappendwalkgetElement;
if walk tail
sbappend;
walk walk.getNext;
sbappend;
return sbtoString;
public void traverse
Node walk head;
while walk null
E e walk.getElement;
System.out.println e ;
walk walk.getNext;
public static void mainString args
throws CloneNotSupportedException
SinglyLinkedList list new SinglyLinkedList;
list.addFirstMSP;
list.addLastATL;
list.addLastBOS;
list.addLastLAX;
System.out.println list ;
SinglyLinkedList list list.clone;
Systemout.printlnlist;
if list list
System.out.println "list points to an object that is not pointed to by list;
System.out.println list;
list.traverse;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started