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

please fix the following issues with my code. Thank you in general, delete() log

ID: 3837087 • Letter: P

Question

please fix the following issues with my code. Thank you

in general, delete() logic must update a back link as well as a next link delete() logic must check to see if you are removing the last element in the list and update the end appropriately getPrevious() must reset backCurrent to end when backCurrent == dlList, not when backCurrent.getBack() == dlList

Test.java

package project4;

//The driver program

public class Test

{

public static void main(String args[])

{

DLLAdt<Integer> dlList= new DLLAdt<Integer>();

for(int itr=100;itr>1;itr-=10)

dlList.add(itr);

System.out.println(dlList.toString());

dlList.reset();

dlList.backReset();

System.out.println(" Testing the returnNext() function");

System.out.print(" "+dlList.returnNext().intValue());

System.out.print(" "+dlList.returnNext().intValue());

System.out.print(" "+dlList.returnNext().intValue());

System.out.println(" Testing the getPrevious() function");

System.out.print(" "+dlList.getPrevious().intValue());

System.out.print(" "+dlList.getPrevious().intValue());

System.out.print(" "+dlList.getPrevious().intValue());

}

}

remove.java

package project4;

public class remove
{

static Node head = null;

class Node
{

String data;
Node next, prev;

Node(String d) {
data = d;
next = prev = null;
}
}

void AddNodeInDoublylinkedList(Node head_ref, String new_data) {
  
/* here allocate node */
Node new_node = new Node(new_data);

new_node.prev = null;

new_node.next = (head);

/* here we are changing prev of head node to new node */
if ((head) != null)
{
(head).prev = new_node;
}

(head) = new_node;
}

  
  
void RemoveNode(Node head_ref, Node remove) {

  
if (head == null || remove == null) {
return;
}

/* If node to be deleted is head node */
if (head == remove)
{
head = remove.next;
}

  
if (remove.next != null) {
remove.next.prev = remove.prev;
}


if (remove.prev != null) {
remove.prev.next = remove.next;
}

return;
}
  

void DisplayList(Node node)
{
while (node != null)
{
System.out.print(node.data + " ");
node = node.next;
}
}

public static void main(String[] args)
{
  
remove list = new remove ();

/* create a doubly linked list */
list.AddNodeInDoublylinkedList(head, "david");
list.AddNodeInDoublylinkedList(head, "Joshusa");
list.AddNodeInDoublylinkedList(head, "Miraim");   

System.out.println(" --------Original Linked list---------------------");
//call DisplayList method
list.DisplayList(head);

  
/*remove middle node of the list*/
list.RemoveNode(head, head.next);

System.out.println(" -----------Modified Linked List------------------");

//call DisplayList method   
list.DisplayList(head);
}
}

DLLNode.java

package project4;

public class DLLNode<T>

{

private DLLNode<T> next,back;

private T info;

public DLLNode(T info)

{

this.info = info;

next = null;

}

public void setInfo(T info)

{

this.info = info;

}

public T getInfo()

{

return info;

}

public void setNext(DLLNode<T> next)

{

this.next = next;

}

public DLLNode<T> returnNext()

// Returns next link of this DLLNode.

{

return next;

}

public void setBack(DLLNode<T> back)

{

this.back = back;

}

public DLLNode<T> getBack()

{

return back;

}

}

DLLAdt.java

package project4;

public class DLLAdt<T>

{

protected int elemNum;

protected DLLNode<T> current;

protected DLLNode<T> backCurrent;

protected boolean status;

protected DLLNode<T> pos;

protected DLLNode<T> backe;

protected DLLNode<T> dlList;

protected DLLNode<T> end;

//class constructor

public DLLAdt()

{

elemNum = 0;

dlList = null;

end = null;

current = null;

backCurrent = null;

}

//to insert to dd list

public void add(T element)

{

DLLNode<T> newNode = new DLLNode<T>(element);

if(dlList==null)

{

dlList = newNode;

end = dlList;

}

else

{

dlList.setBack(newNode);

newNode.setNext(dlList);

dlList = newNode;

}

elemNum++;

}

protected void find(T target)

{

pos = dlList;

status = false;

while (pos != null)

{

if (pos.getInfo().equals(target))

{

status = true;

return;

}

else

{

backe = pos;

pos = pos.returnNext();

}

}

}

//to get the size

public int size()

{

return elemNum;

}

//to check the presence of an entry

public boolean contains (T element)

{

find(element);

return status;

}

//to delete entries from the list

public boolean delete (T element)

{

find(element);

if (status)

{

if (dlList == pos)

dlList = dlList.returnNext();

else

backe.setNext(pos.returnNext());

elemNum--;

}

return status;

}

public T get(T element)

{

find(element);

if (status)

return pos.getInfo();

else

return null;

}

public String toString()

{

DLLNode<T> currNode = dlList;

String listString = "List: ";

while (currNode != null)

{

listString = listString + " " + currNode.getInfo() + " ";

currNode = currNode.returnNext();

}

return listString;

}

//reset teh list

public void reset()

{

current = dlList;

}

public void backReset()

{

backCurrent = end;

}

public T returnNext()

{

T next = current.getInfo();

if (current.returnNext() == null)

current = dlList;

else

current = current.returnNext();

return next;

}

//get previous

public T getPrevious()

{

T pre = backCurrent.getInfo();

if (backCurrent.getBack() == dlList)

backCurrent = end;

else

backCurrent = backCurrent.getBack();

return pre;

}

}

Explanation / Answer

Modified code is highlighted in bold

public class Test
{
public static void main(String args[])
{
DLLAdt<Integer> dlList= new DLLAdt<Integer>();
for(int itr=100;itr>1;itr-=10)
dlList.add(itr);
System.out.println(dlList.toString());
dlList.reset();
dlList.backReset();
System.out.println(" Testing the returnNext() function");
System.out.print(" "+dlList.returnNext().intValue());
System.out.print(" "+dlList.returnNext().intValue());
System.out.print(" "+dlList.returnNext().intValue());
System.out.println(" Testing the getPrevious() function");
System.out.print(" "+dlList.getPrevious().intValue());
System.out.print(" "+dlList.getPrevious().intValue());
System.out.print(" "+dlList.getPrevious().intValue());
dlList.delete(50);

dlList.delete(10);

  dlList.delete(100);

System.out.println(" "+dlList.toString());
}
}
public class remove
{
static Node head = null;

class Node
{

String data;
Node next, prev;

Node(String d) {
data = d;
next = prev = null;
}
}

void AddNodeInDoublylinkedList(Node head_ref, String new_data) {
  
/* here allocate node */
Node new_node = new Node(new_data);

new_node.prev = null;

new_node.next = (head);

/* here we are changing prev of head node to new node */
if ((head) != null)
{
(head).prev = new_node;
}

(head) = new_node;
}

  
  
void RemoveNode(Node head_ref, Node remove) {

  
if (head == null || remove == null) {
return;
}

/* If node to be deleted is head node */
if (head == remove)
{
head = remove.next;
}

  
if (remove.next != null) {
remove.next.prev = remove.prev;
}


if (remove.prev != null) {
remove.prev.next = remove.next;
}

return;
}
  

void DisplayList(Node node)
{
while (node != null)
{
System.out.print(node.data + " ");
node = node.next;
}
}

public static void main(String[] args)
{
  
remove list = new remove ();

/* create a doubly linked list */
list.AddNodeInDoublylinkedList(head, "david");
list.AddNodeInDoublylinkedList(head, "Joshusa");
list.AddNodeInDoublylinkedList(head, "Miraim");   

System.out.println(" --------Original Linked list---------------------");
//call DisplayList method
list.DisplayList(head);

  
/*remove middle node of the list*/
list.RemoveNode(head, head.next);

System.out.println(" -----------Modified Linked List------------------");

//call DisplayList method   
list.DisplayList(head);
}
}

public class DLLNode<T>
{
private DLLNode<T> next,back;
private T info;
public DLLNode(T info)
{
this.info = info;
next = null;
}
public void setInfo(T info)
{
this.info = info;
}
public T getInfo()
{
return info;
}
public void setNext(DLLNode<T> next)
{
this.next = next;
}
public DLLNode<T> returnNext()
// Returns next link of this DLLNode.
{
return next;
}
public void setBack(DLLNode<T> back)
{
this.back = back;
}
public DLLNode<T> getBack()
{
return back;
}
}
public class DLLAdt<T>
{
protected int elemNum;
protected DLLNode<T> current;
protected DLLNode<T> backCurrent;
protected boolean status;
protected DLLNode<T> pos;
protected DLLNode<T> backe;
protected DLLNode<T> dlList;
protected DLLNode<T> end;
//class constructor
public DLLAdt()
{
elemNum = 0;
dlList = null;
end = null;
current = null;
backCurrent = null;
}
//to insert to dd list
public void add(T element)
{
DLLNode<T> newNode = new DLLNode<T>(element);
if(dlList==null)
{
dlList = newNode;
end = dlList;
}
else
{
dlList.setBack(newNode);
newNode.setNext(dlList);
dlList = newNode;
}
elemNum++;
}
protected void find(T target)
{
pos = dlList;
status = false;
while (pos != null)
{
if (pos.getInfo().equals(target))
{
status = true;
return;
}
else
{
backe = pos;
pos = pos.returnNext();
}
}
}
//to get the size
public int size()
{
return elemNum;
}
//to check the presence of an entry
public boolean contains (T element)
{
find(element);
return status;
}
//to delete entries from the list
public boolean delete (T element)
{
find(element);
if (status)
{
//if it is first element
if (dlList == pos)
{

dlList = dlList.returnNext();
dlList.setBack(null);
//delete pos;
}
else if(end == pos) //if it is last node
{
backe.setNext(null); // reset the next link to null
//delete pos;
}
else //if it is in between
{
backe.setNext(pos.returnNext());
pos.returnNext().setBack(backe);
// delete pos;
}
elemNum--;
}
return status;
}

public T get(T element)
{
find(element);
if (status)
return pos.getInfo();
else
return null;
}
public String toString()
{
DLLNode<T> currNode = dlList;
String listString = "List: ";
while (currNode != null)
{
listString = listString + " " + currNode.getInfo() + " ";
currNode = currNode.returnNext();
}
return listString;
}
//reset teh list
public void reset()
{
current = dlList;
}
public void backReset()
{
backCurrent = end;
}
public T returnNext()
{
T next = current.getInfo();
if (current.returnNext() == null)
current = dlList;
else
current = current.returnNext();
return next;
}
//get previous
public T getPrevious()
{
T pre = backCurrent.getInfo();
if (backCurrent == dlList)
backCurrent = end;
else
backCurrent = backCurrent.getBack();
return pre;
}


}