1. Design a Java method to swap the contents of two files.

2. Design a method in the "SinglyLinked" class to swap the first and last nodes of a singly linked list. "SinglyLinked" can be found in the "Course Documents" My professor has already given me The "SinglyLinked" class you just have to design a method inside of it to swap the first and last nodes of a singly linked list. here is the class.

public class SinglyLinkedList
{
private Node head;
//optional
private Node cursor;

public SinglyLinkedList ()
{
head = null;
cursor = null;
}

private void resetCursor ()
{
cursor = head;
}

private Node getCursorPos ()
{
return cursor;
}

private boolean hasNext ()
{
return (cursor.getLink () != null);
}

private Node getNext ()
{

cursor = cursor.getLink ();
return cursor;
}


private boolean empty ()
{
return (head == null);
}

private void stepCursorBack ()
{
Node temp = null;
if (!empty ())
{
if (cursor != head)
{
temp = head;
while (temp.getLink () != cursor)
temp = temp.getLink ();
cursor = temp;
}
}
}

private void moveCursorToRear ()
{
if (!empty ())
{
while (hasNext ())
getNext ();

}
}

public void traverse ()
{
if (!empty ())
{
resetCursor ();
System.out.println (cursor);
while (hasNext ())
{
System.out.println (getNext ());
}
}

}


public void addToFront (Integer newMember)
{
Node newNode = new Node (newMember);

newNode.setLink (head);
head = newNode;
resetCursor ();
}

public void addToRear (Integer newMember)
{
if (empty ())
addToFront (newMember);
else
{
moveCursorToRear ();
Node newNode = new Node (newMember);
cursor.setLink (newNode);
}

}

public Node search (Integer key)
{
Node found = null;
if (!empty ())
{
resetCursor ();
do
{
if (cursor.getData ().equals (key))
{
found = cursor;
break;
}

if (hasNext ())
getNext ();
else
break;
} while (true);
}
return found;

}

public boolean delete (Integer key)
{
Node found = search (key);

if (found == null)
{
System.out.println ("key not found");
return false;
}
else
{
if (found == head)
{
head = found.getLink ();
resetCursor ();
}
else
{
cursor = found;
stepCursorBack ();
cursor.setLink (found.getLink ());
}

System.out.println ("key deleted");
return true;

}
}

public String toString ()
{
String outString = "";
if (!empty ())
{
resetCursor ();
outString += (cursor + " ") ;
while (hasNext ())
outString += (getNext () + " ");

}
return outString;
}



}

3.Design a method in the "SinglyLinked2" class to dynamically expand the array if it runs out of spaces for nodes. "SinglyLinked2" can be found in the "Course Documents" . Same thing we are just working with a class that has already been made, we are just designing a method in it to expand the array if it runs out of space for nodes.

public class SinglyLinkedList
{
private int head;
//optional
private int cursor;

private Node[ ] spaces;
private int capacity;


public SinglyLinkedList ()
{
head = -1;
cursor = -1;
capacity = 10000;

spaces = new Node [capacity];
for (int i = 0; i < capacity; i ++)
spaces[i] = new Node ( );

for (int i = 0; i < capacity; i++)
spaces[i].setLink (-2);


}

private void resetCursor ()
{
cursor = head;
}

private int getCursorPos ()
{
return cursor;
}

private boolean hasNext ()
{
return (spaces[cursor].getLink () != -1 );
}

private Node getNext ()
{

cursor = spaces[cursor].getLink ();
return spaces[cursor];
}


private boolean empty ()
{
return (head == -1);
}

private void stepCursorBack ()
{
int temp = -1;
if (!empty ())
{
if (cursor != head)
{
temp = head;
while (spaces[temp].getLink () != cursor)
temp = spaces[temp].getLink ();
cursor = temp;
}
}
}

private void moveCursorToRear ()
{
if (!empty ())
{
while (hasNext ())
getNext ();

}
}

public void traverse ()
{
if (!empty ())
{
resetCursor ();
System.out.println (spaces[cursor]);
while (hasNext ())
{
System.out.println (getNext ());
}
}

}

private int NEW ( )
{
int indexOfTheNextOpenSpace = -1;
for (int i = 0; i < capacity; i++)
{
if (spaces[i].getLink () == -2)
{
indexOfTheNextOpenSpace = i;
break;
}
}

return indexOfTheNextOpenSpace;

}

public void addToFront (Integer newMember)
{
int newNode = NEW ();
if (newNode == -1)
{
System.out.println ("no space available");
return;
}
else
{
spaces[newNode].setData (newMember);
spaces[newNode].setLink (-1);
spaces[newNode].setLink (head);
head = newNode;
resetCursor ();
}
}

public void addToRear (Integer newMember)
{
if (empty ())
addToFront (newMember);
else
{
moveCursorToRear ();
int newNode = NEW ( );
if (newNode == -1)
{
System.out.println ("no space available");
return;
}
else
{
spaces[newNode].setData (newMember);
spaces[newNode].setLink (-1);
spaces[cursor].setLink (newNode);

}
}

}

public int search (Integer key)
{
int found = -1;
if (!empty ())
{
resetCursor ();
do
{
if (spaces[cursor].getData ().equals (key))
{
found = cursor;
break;
}

if (hasNext ())
getNext ();
else
break;
} while (true);
}
return found;

}

public boolean delete (Integer key)
{
int found = search (key);

if (found == -1)
{
System.out.println ("key not found");
return false;
}
else
{
if (found == head)
{
head = spaces[found].getLink ();
resetCursor ();
}
else
{
cursor = found;
stepCursorBack ();
spaces[cursor].setLink (spaces[found].getLink ());
}

System.out.println ("key deleted");
deAllocate (found);

return true;

}
}

private void deAllocate (int found)
{
spaces[found].setLink (-2);
}


public String toString ()
{
String outString = "";
if (!empty ())
{
resetCursor ();
outString += (spaces[cursor] + " ") ;
while (hasNext ())
outString += (getNext () + " ");

}
return outString;
}



}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.