Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Finish this code by Implementing a NodeList First piece of code is a blueprint t

ID: 3566897 • Letter: F

Question

Finish this code by Implementing a NodeList

First piece of code is a blueprint to what you have to do.

Second piece of code is started, and now needs to have various methods implemented. Keep in mind that the methods need to be implemented as a linked node implementation.

__________________________________________________________________________________

int length(); // returns the number of data items in the list

Object first(); // returns the data item at the start of the list

Object last(); // returns the data item at the end of the list

// Public Methods

boolean lookup(String);
/* moves pointer to the first data that contains String (based on toString() of data);
returns true/false if found or not;
if not found pointer advances to start of list */

Object find(int n);
/* returns the nth object from the current location and advances pointer but does not wrap;
n can be positive or negative find(0) returns current data. */

boolean delete(); // deletes current object

void add(Object);
/* insert this Object into the list (either alphabetically or at current location depending on sort flag;
pointer points to the new item */

Object getNext();
/* returns data after current pointer;
pointer advances and wraps if appropriate */

Object getPrevious(); // same as getNext() but moves back in list

void reset(); // moves pointer to start of list

}

___________________________________________________________________________

package Project3;

public class MyNodeList implements MyList{

  
   public class N {
  
   // constructors
  
   public N() {}
  
   public N(Object o, N link) {
   data = o;
   next = link;
   }
  
   // selectors
  
   public Object getData() {
   return data;
   }
  
   public void setData(Object o) {
   data = o;
   }
  
   public N getNext() {
   return next;
  
   }
  
   public void setNext(N link) {
   next = link;
   }
  
   // instance variables
  
   private Object data;
  
   private N next;
  
   }

      
}

Explanation / Answer

package Project3;

public class MyNodeList implements MyList{

  
public class N {

// constructors

public N() {}

public N(Object o, N link) {
data = o;
next = link;
}

// selectors

public Object getData() {
return data;
}

public void setData(Object o) {
data = o;
}

public N getNext() {
return next;

}

public void setNext(N link) {
next = link;
}

// instance variables

private Object data;

private N next;
  
}

private N head;
private N tail;
private N current;
private int size;
  
public MyNodeList()
{
head = null;
tail = null;
current = null;
size = 0;
}

public int length()
{
return size;
}


public Object first()
{
if (head != null)
return head.getData();
return null;
}


public Object last()
{
if (tail != null)
return tail.getData();
return null;
}


public boolean lookup(String str)
{
N p = head;
while (p != null)
{
if (p.getData().toString().equals(str))
return true;
p = p.getNext();
}
return false;
}


public Object find(int n)
{
N p = head;
for (int i = 0; i < n && p != null; i++)
{
p = p.getNext();
}
if (p != null)
return p.getData();
return null;
}


public boolean delete()
{
if (current == null)
return false;
  
N prev = null;
if (current != head)
{
prev = head;
while (prev.getNext() != current)
prev = prev.getNext();
}
  
if (prev == null)
{
head = current.getNext();
}
else
{
prev.setNext(current.getNext());
}
  
current = current.getNext();
size--;
  
if (size == 0)
{
head = null;
tail = null;
}
  
  
return true;
}


public void add(Object obj)
{
N newNode = new N(obj, null);
if (head == null)
head = newNode;
else
tail.setNext(newNode);
tail = newNode;
size++;
}


public Object getNext()
{
Object result = null;
if (current != null)
{
result = current.getData();
current = current.getNext();
}

return result;
}


public Object getPrevious()
{
Object result = null;
if (current != null)
{
result = current.getData();
  
if (current == head)
current = null;
else
{
N previous = head;
while (previous.getNext() != current)
previous = previous.getNext();
current = previous;
}
}

return result;
}


public void reset()
{
current = head;
  
}
  
  


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote