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

2. Finish the code for the two incomplete methods below given the code for Node

ID: 3883898 • Letter: 2

Question

2. Finish the code for the two incomplete methods below given the code for Node and DoublyLinkedList. Do not alter the code that is given. Simply finish removeFirstOccurrence method and the Node constructor.

3. Write a subclass of the class DoublyLinkedList from the previous question here. The subclass is called IterableDoublyLinkedList. The subclass is to implement java.lang.Iterable. It must have a private inner class implementing java.util.Iterator.

The iterator should iterate from the head of the list to the end. Note that there is no tail reference, but the next field of the last node has value null.

Do not implement the remove method of java.util.Iterator - just ignore it completely for this question. The method next() should throw a java.util.NoSuchElementException if it is called yet the iterator already finished iterating through the list.

public class DoublyLinkedList f protected Node head; // Note there is no tail reference. public void addToFront (String data) head new Node (data, head, null); public void removeFirstoccurrence(String data) t // complete the code /1 It removes the node having the first occurrence of the target data. // The search starts at the head. If the target data is not in the list, then /1 the list remains unchanged. The code should not fail. // The next field of the last node has value null. There is no tail reference. // Write your code here: protected class Nodef protected String data; protected Node next; protected Node previous; private Node (String data, Node next, Node previous) t l // end of Node class // end of DoublyLinkedList class

Explanation / Answer

Nde.java

import java.util.Scanner;

class Nde
{
protected int dat;
protected Nde nxt, previous;

public Nde()
{
nxt = null;
previous = null;
dat = 0;
}
public Nde(int don, Nde no, Nde pr)
{
dat = don;
nxt = no;
previous = pr;
}
public void setLnk(Nde no)
{
nxt = no;
}
public void setLnkPr(Nde pr)
{
previous = pr;
}
public Nde getLnkNxt()
{
return nxt;
}
public Nde getnkPrev()
{
return previous;
}
public void setData(int don)
{
dat = don;
}
public int getDat()
{
return dat;
}
}

lList.java

class lList
{
protected Nde st;
protected Nde en ;
public int sz;

public lList()
{
st = null;
en = null;
sz = 0;
}
public boolean chckEmpty()
{
return st == null;
}
public int getSz()
{
return sz;
}
public void insStart(int value)
{
Nde npt = new Nde(value, null, null);
if(st == null)
{
st = npt;
en = st;
}
else
{
st.setLnkPr(npt);
npt.setLnk(st);
st = npt;
}
sz++;
}
public void insAtEnd(int value)
{
Nde npt = new Nde(value, null, null);
if(st == null)
{
st = npt;
en = st;
}
else
{
npt.setLnkPr(en);
en.setLnk(npt);
en = npt;
}
sz++;
}
public void insPosit(int value , int posit)
{
Nde npt = new Nde(value, null, null);
if (posit == 1)
{
insStart(value);
return;
}
Nde ptr = st;
for (int uu = 2; uu <= sz; uu++)
{
if (uu == posit)
{
Nde tmp = ptr.getLnkNxt();
ptr.setLnk(npt);
npt.setLnkPr(ptr);
npt.setLnk(tmp);
tmp.setLnkPr(npt);
}
ptr = ptr.getLnkNxt();
}
sz++ ;
}
public void delAtPos(int posit)
{
if (posit == 1)
{
if (sz == 1)
{
st = null;
en = null;
sz = 0;
return;
}
st = st.getLnkNxt();
st.setLnkPr(null);
sz--;
return ;
}
if (posit == sz)
{
en = en.getnkPrev();
en.setLnk(null);
sz-- ;
}
Nde ptr = st.getLnkNxt();
for (int uu = 2; uu <= sz; uu++)
{
if (uu == posit)
{
Nde pr = ptr.getnkPrev();
Nde no = ptr.getLnkNxt();

pr.setLnk(no);
no.setLnkPr(pr);
sz-- ;
return;
}
ptr = ptr.getLnkNxt();
}
}
public void disp()
{
System.out.print(" Doubly Linked List = ");
if (sz == 0)
{
System.out.print("empty o");
return;
}
if (st.getLnkNxt() == null)
{
System.out.println(st.getDat() );
return;
}
Nde ptr = st;
System.out.print(st.getDat()+ " <-> ");
ptr = st.getLnkNxt();
while (ptr.getLnkNxt() != null)
{
System.out.print(ptr.getDat()+ " <-> ");
ptr = ptr.getLnkNxt();
}
System.out.print(ptr.getDat()+ " o");
}
}

DLList.java

public class DLList
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
lList lst = new lList();
System.out.println("Doubly Linked List Test o");
char ch;
do
{
System.out.println(" Doubly Linked List Operations o");
System.out.println("1. insert at begining");
System.out.println("2. insert at en");
System.out.println("3. insert at position");
System.out.println("4. delete at position");
System.out.println("5. check empty");
System.out.println("6. get sz");

int ch = sc.nextInt();
switch (ch)
{
case 1 :
System.out.println("Enter integer element to insert");
lst.insStart( sc.nextInt() );   
break;
case 2 :
System.out.println("Enter integer element to insert");
lst.insAtEnd( sc.nextInt() );   
break;   
case 3 :
System.out.println("Enter integer element to insert");
int nob = sc.nextInt() ;
System.out.println("Enter position");
int posit = sc.nextInt() ;
if (posit < 1 || posit > lst.getSz() )
System.out.println("Invalid position o");
else
lst.insPosit(nob, posit);
break;
case 4 :
System.out.println("Enter position");
int pr = sc.nextInt() ;
if (pr < 1 || pr > lst.getSz() )
System.out.println("Invalid position o");
else
lst.delAtPos(pr);
break;   
case 5 :
System.out.println("Empty status = "+ lst.chckEmpty());
break;
case 6 :
System.out.println("Size = "+ lst.getSz() +" o");
break;   
default :
System.out.println("Wrong Entry o ");
break;   
}
lst.disp();
System.out.println(" Do you want to continue (Type y or no) o");
ch = sc.nxt().charAt(0);

} while (ch == 'Y'|| ch == 'y');   
}
}

Rate an upvote.....Thankyou

Hope this helps.....

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