Answered step by step
Verified Expert Solution
Link Copied!

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

Java/Eclipse/SinglyList
In this exercise, you will add a non-static method swapNodes(int node1Index, int node2Index) to SinglyLinkedList class from week 2 lecture examples. This method should swap the elements of two nodes where node1Index is the index of one node, and node2Index 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 1 implies first node, index 2 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 Node(E e, Node n){
element = e;
next = n;
}
public E getElement(){ return element; }
public Node getNext(){ return next; }
public void setNext(Node n){ next = n; }
}
private Node head = null;
private Node tail = null;
private int size =0;
public SinglyLinkedList(){}
public int size(){ return size; }
public boolean isEmpty(){ return size ==0; }
public E first(){
if (isEmpty()) return null;
return head.getElement();
}
public E last(){
if (isEmpty()) return null;
return tail.getElement();
}
public void addFirst(E e){
head = new Node<>(e, head);
if (size ==0)
tail = head;
size++;
}
public void addLast(E e){
Node newest = new Node<>(e, null);
if (isEmpty())
head = newest;
else
tail.setNext(newest);
tail = newest;
size++;
}
public E removeFirst(){
if (isEmpty()) return null;
E answer = head.getElement();
head = head.getNext();
size--;
if (size ==0)
tail = null;
return answer;
}
@SuppressWarnings({"unchecked"})
public boolean equals(Object o){
if (o == null) return false;
if (getClass()!= o.getClass()) return false;
SinglyLinkedList other =(SinglyLinkedList) o;
if (size != other.size) return false;
Node walkA = head;
Node walkB = other.head;
while (walkA != null){
if (!walkA.getElement().equals(walkB.getElement())) return false;
walkA = walkA.getNext();
walkB = walkB.getNext();
}
return true;
}
@SuppressWarnings({"unchecked"})
public SinglyLinkedList clone() throws CloneNotSupportedException {
SinglyLinkedList other =(SinglyLinkedList) super.clone();
if (size >0){
other.head = new Node<>(head.getElement(), null);
Node walk = head.getNext();
Node otherTail = other.head;
while (walk != null){
Node newest = new Node<>(walk.getElement(), null);
otherTail.setNext(newest);
otherTail = newest;
walk = walk.getNext();
}
other.tail = otherTail;
}
return other;
}
public int hashCode(){
int h =0;
for (Node walk=head; walk != null; walk = walk.getNext()){
h ^= walk.getElement().hashCode();
h =(h <<5)|(h >>>27);
}
return h;
}
public String toString(){
StringBuilder sb = new StringBuilder("(");
Node walk = head;
while (walk != null){
sb.append(walk.getElement());
if (walk != tail)
sb.append(",");
walk = walk.getNext();
}
sb.append(")");
return sb.toString();
}
public void traverse(){
Node walk = head;
while (walk != null){
E e = walk.getElement();
System.out.println( e );
walk = walk.getNext();
}
}
public static void main(String[] args)
throws CloneNotSupportedException
{
SinglyLinkedList list = new SinglyLinkedList();
list.addFirst("MSP");
list.addLast("ATL");
list.addLast("BOS");
list.addLast("LAX");
System.out.println( list );
SinglyLinkedList list2= list.clone();
//System.out.println(list2);
if( list != list2)
System.out.println( "list points to an object that is not pointed to by list2");
System.out.println( list2);
list.traverse();
}
}

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

More Books

Students also viewed these Databases questions