/ // Returns the element at the current position on this list. // If the current position is the last element, then it advances the value // of the current position to the first element; otherwise, it advances // the value of the current position to the next element. { T next = currentPos.getInfo(); if (currentPos.getLink() == null) currentPos = list;

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

package Linked_List;


public class RefUnsortedList<T> implements ListInterface<T>
{
protected int numElements; // number of elements in this list
protected LLNode<T> currentPos; // current position for iteration
// set by find method
protected boolean found; // true if element found, else false
protected LLNode<T> location; // node containing element, if found
protected LLNode<T> previous; // node preceeding location
protected LLNode<T> list; // first node on the list
public RefUnsortedList()
{
numElements = 0;
list = null;
currentPos = null;
}
public void add(T element)
// Adds element to this list.
{
LLNode<T> newNode = new LLNode<T>(element);
newNode.setLink(list);
list = newNode;
numElements++;
}
protected void find(T target)
// Searches list for an occurence of an element e such that
// e.equals(target). If successful, sets instance variables
// found to true, location to node containing e, and previous
// to the node that links to location. If not successful, sets
// found to false.
{
location = list;
found = false;
while (location != null)
{
if (location.getInfo().equals(target)) // if they match
{
found = true;
return;
}
else
{
previous = location;
location = location.getLink();
}
}
}
public int size()
// Returns the number of elements on this list.
{
return numElements;
}
public boolean contains (T element)
// Returns true if this list contains an element e such that
// e.equals(element); otherwise, returns false.
{
find(element);
return found;
}
public boolean remove (T element)
// Removes an element e from this list such that e.equals(element)
// and returns true; if no such element exists, returns false.
{
find(element);
if (found)
{
if (list == location)
list = list.getLink(); // remove first node
else
previous.setLink(location.getLink()); // remove node at location
numElements--;
}
return found;
}
public T get(T element)
// Returns an element e from this list such that e.equals(element);
// if no such element exists, returns null.
{
find(element);
if (found)
return location.getInfo();
else
return null;
}
public String toString()
// Returns a nicely formatted string that represents this list.
{
LLNode<T> currNode = list;
String listString = "List:\n";
while (currNode != null)
{
listString = listString + " " + currNode.getInfo() + "\t";
currNode = currNode.getLink();
}
return listString;
}
public void reset()
// Initializes current position for an iteration through this list,
// to the first element on this list.
{
currentPos = list;
}
public T getNext()
// Preconditions: The list is not empty
// The list has been reset
// The list has not been modified since most recent reset
//
// Returns the element at the current position on this list.
// If the current position is the last element, then it advances the value
// of the current position to the first element; otherwise, it advances
// the value of the current position to the next element.
{
T next = currentPos.getInfo();
if (currentPos.getLink() == null)
currentPos = list;
else
currentPos = currentPos.getLink();
return next;
}
}

Using the RefUnsortedList class (linked list), write an instance method called AddNext that will change the info by
adding the info of the next list element to the current element. The last element will be incremented by 0 without
calling any other member function
Example: For the object List1 that contains the following items [8, -3, 4, 9, 5, 8], List1 will contain the following items [5,
1, 13, 14, 13, 8] after the AddNext method call.
Transcribed Image Text:Using the RefUnsortedList class (linked list), write an instance method called AddNext that will change the info by adding the info of the next list element to the current element. The last element will be incremented by 0 without calling any other member function Example: For the object List1 that contains the following items [8, -3, 4, 9, 5, 8], List1 will contain the following items [5, 1, 13, 14, 13, 8] after the AddNext method call.
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY