SlideShare a Scribd company logo
In the class we extensively discussed a generic singly linked list implemented as a node class.
Implement a generic node class called DoublyNode in which each node contains an element of
type E and each node is linked not only forward to the next element in the list but also backward
to the previous element in the list. Your implementation should adapt and implement all the
methods of generic Node including the following methods:
Constructor for DoublyNode
addNodeAfter, removeNodeAfter
getData, setData
getForeLink, setForeLink
getBackLink, setBackLink
listCopy
listCopyWithTail
listLength
listPart
listPosition
listSearch
Solution
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.Random;
public class DoublyNode {
private Node head;
private Node tail;
private int size;
public DoublyNode() {
size = 0;
}
private class Node {
E element;
Node getForeNode;
Node getBackNode;
public Node(E element, Node next, Node prev) {
this.element = element;
this.getForeNode = next;
this.getBackNode = prev;
}
}
public int listLength() { return size; }
public boolean isEmpty() { return size == 0; }
public void addFirst(E element) {
Node tmp = new Node(element, head, null);
if(head != null ) {head.getBackNode = tmp;}
head = tmp;
if(tail == null) { tail = tmp;}
size++;
System.out.println("adding: "+element);
}
public void addNodeAfter(E element) {
Node tmp = new Node(element, null, tail);
if(tail != null) {tail.getForeNode = tmp;}
tail = tmp;
if(head == null) { head = tmp;}
size++;
System.out.println("adding: "+element);
}
public void addNodeAfterSpecifiedNode(E element, E tobeAddedAfter) {
Node prevTobeAddedAfter = listSearch(element);
Node afterNode = prevTobeAddedAfter.getForeNode;
Node tmpNode = new Node(element,prevTobeAddedAfter,afterNode);
tmpNode = prevTobeAddedAfter.getForeNode;
afterNode = tmpNode.getForeNode;
}
public void listCopy(){
System.out.println("iterating forward..");
Node tmp = head;
while(tmp != null){
System.out.println(tmp.element);
tmp = tmp.getForeNode;
}
}
public Node listSearch(E item){
System.out.println("Searching Item "+item);
Node tmp = head;
while(tmp != null){
System.out.println(tmp.element);
if(tmp.element == item) {
System.out.println("Item found "+item);
return tmp;
}
tmp = tmp.getForeNode;
}
return null;
}
public void iterateBackward(){
System.out.println("iterating backword..");
Node tmp = tail;
while(tmp != null){
System.out.println(tmp.element);
tmp = tmp.getBackNode;
}
}
public E removeFirst() {
if (size == 0) throw new NoSuchElementException();
Node tmp = head;
head = head.getForeNode;
head.getBackNode = null;
size--;
System.out.println("deleted: "+tmp.element);
return tmp.element;
}
public E removeNodeAfter() {
if (size == 0) throw new NoSuchElementException();
Node tmp = tail;
tail = tail.getBackNode;
tail.getForeNode = null;
size--;
System.out.println("deleted: "+tmp.element);
return tmp.element;
}
}

More Related Content

PDF
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
DOCX
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
PDF
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
DOCX
Please complete all the code as per instructions in Java programming.docx
PDF
I need help implementing a Stack with this java programming assignme.pdf
PDF
-JAVA-provide a test class that do the required -you may add met.pdf
PDF
practice of using and manipulating a doubly linked list and (2) prac.pdf
DOCX
Note- Can someone help me with the private E get(int index- int curren (1).docx
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Please complete all the code as per instructions in Java programming.docx
I need help implementing a Stack with this java programming assignme.pdf
-JAVA-provide a test class that do the required -you may add met.pdf
practice of using and manipulating a doubly linked list and (2) prac.pdf
Note- Can someone help me with the private E get(int index- int curren (1).docx

Similar to In the class we extensively discussed a generic singly linked list i.pdf (20)

PDF
public class CircularDoublyLinkedList-E- implements List-E- { privat.pdf
PDF
Note- Can someone help me with the Public boolean add(E value) method.pdf
PDF
Help please!!(Include your modified DList.java source code file in.pdf
PDF
import java-util--- public class MyLinkedList{ public static void.pdf
PDF
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
PDF
Fix my codeCode.pdf
PDF
STAGE 2 The Methods 65 points Implement all the methods t.pdf
PDF
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
PDF
public class MyLinkedListltE extends ComparableltEgtg.pdf
PDF
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
PDF
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
PDF
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PDF
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PDF
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
PDF
hi i have to write a java program involving link lists. i have a pro.pdf
PDF
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
PDF
Describe an algorithm for concatenating two singly linked lists L and.pdf
PDF
Using visual studio 2022- a C# windows form application- and your Doub.pdf
PDF
Rewrite this code so it can use a generic type instead of integers. .pdf
PDF
Use the singly linked list class introduced in the lab to implement .pdf
public class CircularDoublyLinkedList-E- implements List-E- { privat.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
Help please!!(Include your modified DList.java source code file in.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
Fix my codeCode.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
public class MyLinkedListltE extends ComparableltEgtg.pdf
Copy your completed LinkedList class from Lab 3 into the LinkedList..pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
Implement the interface you wrote for Lab B (EntryWayListInterface)..pdf
Describe an algorithm for concatenating two singly linked lists L and.pdf
Using visual studio 2022- a C# windows form application- and your Doub.pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
Use the singly linked list class introduced in the lab to implement .pdf
Ad

More from birajdar2 (20)

PDF
public static ArrayListInteger doArrayListSearchSmallest(int nu.pdf
PDF
Project selection methods and the project portfolio play an importan.pdf
PDF
javaFix in the program belowhandle incomplete data for text fil.pdf
PDF
James can row 14 km downstream in a river in 2 hours. He takes 7 hou.pdf
PDF
How do hydrophobic and hydrophilic hormones differ in their speeds a.pdf
PDF
Given below is an issue that you have identified as an issue in a ret.pdf
PDF
Explain Aquinas 5 proofs of God, being sure to identify the Aris.pdf
PDF
Exercise 7. Show that if C(0) (the punctured plane) and U C are conf.pdf
PDF
Consider the following segment table What are the physical addresse.pdf
PDF
Can you explain the movement of ions and ion channel activity during.pdf
PDF
Calculate the implied stock price assuming an EBITDA multiple of 11..pdf
PDF
Below is the graph of a polynomial function f with real coefficients.pdf
PDF
Are higher than average sea surface temperatures associated with a g.pdf
PDF
A table of values of an increasing function F is shown. Use the table.pdf
PDF
A polygenic trait is determined by a single gene with many different.pdf
PDF
4. Phil is conducting a seed germination experiment. He places 3 gro.pdf
PDF
21. What is the relationship between the maximum size of aggregates a.pdf
PDF
Which of the following are organizer molecules in the avian PMZ is a.pdf
PDF
What are the five stages of team development Describe each stage an.pdf
PDF
What kinds of molecules can be used as metabolic fuel to produce ATP.pdf
public static ArrayListInteger doArrayListSearchSmallest(int nu.pdf
Project selection methods and the project portfolio play an importan.pdf
javaFix in the program belowhandle incomplete data for text fil.pdf
James can row 14 km downstream in a river in 2 hours. He takes 7 hou.pdf
How do hydrophobic and hydrophilic hormones differ in their speeds a.pdf
Given below is an issue that you have identified as an issue in a ret.pdf
Explain Aquinas 5 proofs of God, being sure to identify the Aris.pdf
Exercise 7. Show that if C(0) (the punctured plane) and U C are conf.pdf
Consider the following segment table What are the physical addresse.pdf
Can you explain the movement of ions and ion channel activity during.pdf
Calculate the implied stock price assuming an EBITDA multiple of 11..pdf
Below is the graph of a polynomial function f with real coefficients.pdf
Are higher than average sea surface temperatures associated with a g.pdf
A table of values of an increasing function F is shown. Use the table.pdf
A polygenic trait is determined by a single gene with many different.pdf
4. Phil is conducting a seed germination experiment. He places 3 gro.pdf
21. What is the relationship between the maximum size of aggregates a.pdf
Which of the following are organizer molecules in the avian PMZ is a.pdf
What are the five stages of team development Describe each stage an.pdf
What kinds of molecules can be used as metabolic fuel to produce ATP.pdf
Ad

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Cell Structure & Organelles in detailed.
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Pharma ospi slides which help in ospi learning
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
master seminar digital applications in india
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Anesthesia in Laparoscopic Surgery in India
Cell Structure & Organelles in detailed.
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Pharma ospi slides which help in ospi learning
Supply Chain Operations Speaking Notes -ICLT Program
Complications of Minimal Access Surgery at WLH
Module 4: Burden of Disease Tutorial Slides S2 2025
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
master seminar digital applications in india
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
102 student loan defaulters named and shamed – Is someone you know on the list?
VCE English Exam - Section C Student Revision Booklet
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
A systematic review of self-coping strategies used by university students to ...
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Computing-Curriculum for Schools in Ghana
Pharmacology of Heart Failure /Pharmacotherapy of CHF
FourierSeries-QuestionsWithAnswers(Part-A).pdf

In the class we extensively discussed a generic singly linked list i.pdf

  • 1. In the class we extensively discussed a generic singly linked list implemented as a node class. Implement a generic node class called DoublyNode in which each node contains an element of type E and each node is linked not only forward to the next element in the list but also backward to the previous element in the list. Your implementation should adapt and implement all the methods of generic Node including the following methods: Constructor for DoublyNode addNodeAfter, removeNodeAfter getData, setData getForeLink, setForeLink getBackLink, setBackLink listCopy listCopyWithTail listLength listPart listPosition listSearch Solution import java.util.ListIterator; import java.util.NoSuchElementException; import java.util.Random; public class DoublyNode { private Node head; private Node tail; private int size; public DoublyNode() { size = 0; } private class Node { E element;
  • 2. Node getForeNode; Node getBackNode; public Node(E element, Node next, Node prev) { this.element = element; this.getForeNode = next; this.getBackNode = prev; } } public int listLength() { return size; } public boolean isEmpty() { return size == 0; } public void addFirst(E element) { Node tmp = new Node(element, head, null); if(head != null ) {head.getBackNode = tmp;} head = tmp; if(tail == null) { tail = tmp;} size++; System.out.println("adding: "+element); } public void addNodeAfter(E element) { Node tmp = new Node(element, null, tail); if(tail != null) {tail.getForeNode = tmp;} tail = tmp; if(head == null) { head = tmp;} size++; System.out.println("adding: "+element); }
  • 3. public void addNodeAfterSpecifiedNode(E element, E tobeAddedAfter) { Node prevTobeAddedAfter = listSearch(element); Node afterNode = prevTobeAddedAfter.getForeNode; Node tmpNode = new Node(element,prevTobeAddedAfter,afterNode); tmpNode = prevTobeAddedAfter.getForeNode; afterNode = tmpNode.getForeNode; } public void listCopy(){ System.out.println("iterating forward.."); Node tmp = head; while(tmp != null){ System.out.println(tmp.element); tmp = tmp.getForeNode; } } public Node listSearch(E item){ System.out.println("Searching Item "+item); Node tmp = head; while(tmp != null){ System.out.println(tmp.element); if(tmp.element == item) { System.out.println("Item found "+item); return tmp; } tmp = tmp.getForeNode;
  • 4. } return null; } public void iterateBackward(){ System.out.println("iterating backword.."); Node tmp = tail; while(tmp != null){ System.out.println(tmp.element); tmp = tmp.getBackNode; } } public E removeFirst() { if (size == 0) throw new NoSuchElementException(); Node tmp = head; head = head.getForeNode; head.getBackNode = null; size--; System.out.println("deleted: "+tmp.element); return tmp.element; } public E removeNodeAfter() { if (size == 0) throw new NoSuchElementException(); Node tmp = tail; tail = tail.getBackNode; tail.getForeNode = null; size--; System.out.println("deleted: "+tmp.element); return tmp.element; }
  • 5. }