Write some code that will use an iterator to interchange the items

Write some code that will use an iterator to interchange the items in every pair of items in an instance of StringLinkedListWithIterator in Listing 12.9. For example, if the list contains “a”, “b”, “c”, “d”, “e”, and “f”, after the code runs, it will contain “b”, “a”, “d”, “c”, “f”, and “e”. You can assume that the list contains an even number of strings.

Listing 12.9

/**
Linked list with an iterator. One node is the “current node.”
Initially, the current node is the first node. It can be changed
to the next node until the iteration has moved beyond the end
of the list.
*/
public class StringLinkedListWithIterator
{
private ListNode head;
private ListNode current;
private ListNode previous;
public StringLinkedListWithIterator()
{
head = null;
current = null;
previous = null;
}
public void addANodeToStart(String addData)
{
head = new ListNode(addData, head);
if ((current == head.link) && (current != null))
//if current is at old start node
previous = head;
}
/**
Sets iterator to beginning of list.
*/
public void resetIteration()
{
current = head;
previous = null;
}
/**
Returns true if iteration is not finished.
*/
public boolean moreToIterate()
{
return current != null;
}
/**
Advances iterator to next node.
*/
public void goToNext()
{
if (current != null)
{
previous = current;
current = current.link;
}
else if (head != null)
{
System.out.println(
“Iterated too many times or uninitialized
iteration.”);
System.exit(0);
}
else
{
System.out.println(“Iterating with an empty list.”);
System.exit(0);
}
}
/**
Returns the data at the current node.
*/
public String getDataAtCurrent()
{
String result = null;
if (current != null)
result = current.data;
else
{
System.out.println(
“Getting data when current is not at any node.”);
System.exit(0);
}
return result;
}
/**
Replaces the data at the current node.
/*
public void setDataAtCurrent(String newData)
{
if (current != null)
{
current.data = newData;
}
else
{
System.out.println(
“Setting data when current is not at any node.”);
System.exit(0);
}
}
/**
Inserts a new node containing newData after the current node.
The current node is the same after invocation as it is before.
Precondition: List is not empty; current node is not
beyond the entire list.
*/
public void insertNodeAfterCurrent(String newData)
{
ListNode newNode = new LisNode();
newNode.data = newData;
if (current != null)
{
newNode.link = current.link;
current.link = newNode;
}
else if (head != null)
{
System.out.println(
“Inserting when iterator is past all ” +
“nodes or is not initialized.”);
System.exit(0);
}
else
{
System.out.println(
“Using insertNodeAfterCurrent with empty list.”;
System.exit(0);
}
}
/**
Deletes the current node. After the invocation,
the current node is either the node after the
deleted node or null if there is no next node.
*/
public void deleteCurrentNode()
{
if ((current != null) && (previous == null))
{
previous.link = current.link;
current = current.link;
}
else if ((current != null) && (previous == null))
{//At head node
head = current.link;
current = head;
}
else //current==null
{
System.out.println(
“Deleting with uninitialized current or an empty ” +
“list.”);
System.exit(0);
}
}
private inner class ListNode are the same as in Listing 12.7.>

}

 

Stressed over that homework?

Essay deadline breathing down your neck?

Let’s cut to the chase: Why struggle when you can ace it with zero hassle?

Whether it’s essays, research papers, or assignments — we’ve got you covered.

✅ Expert writers
✅ 100% original work
✅ No AI tools, just real pros

Stressed about your essay or homework? Get a top-quality custom essay NOW!!! Stop worrying. Start succeeding.

GradeEssays.com
We are GradeEssays.com, the best college essay writing service. We offer educational and research assistance to assist our customers in managing their academic work. At GradeEssays.com, we promise quality and 100% original essays written from scratch.
Contact Us

Enjoy 24/7 customer support for any queries or concerns you have.

Phone: +1 213 3772458

Email: support@gradeessays.com

© 2024 - GradeEssays.com. All rights reserved.

WE HAVE A GIFT FOR YOU!

15% OFF 🎁

Get 15% OFF on your order with us

Scroll to Top