SlideShare a Scribd company logo
1 of 5
Download to read offline
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

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

Write the Java source code necessary to build a solution for the pro.pdf
Write the Java source code necessary to build a solution for the pro.pdfWrite the Java source code necessary to build a solution for the pro.pdf
Write the Java source code necessary to build a solution for the pro.pdf
fckindswear
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
archgeetsenterprises
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
ezycolours78
 
JAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdfJAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdf
amrishinda
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
aptind
 
Help please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdfHelp please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdf
jyothimuppasani1
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
climatecontrolsv
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
malavshah9013
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
angelsfashion1
 
Step 1 The Pair Class Many times in writing software we come across p.pdf
Step 1 The Pair Class Many times in writing software we come across p.pdfStep 1 The Pair Class Many times in writing software we come across p.pdf
Step 1 The Pair Class Many times in writing software we come across p.pdf
formaxekochi
 
You can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdfYou can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdf
fashionbigchennai
 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
asarudheen07
 
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdfimplement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
FOREVERPRODUCTCHD
 
Can someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdfCan someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdf
ABHISHEKREADYMADESKO
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
mail931892
 
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdfWrite the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
fathimalinks
 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
mail931892
 

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

Write the Java source code necessary to build a solution for the pro.pdf
Write the Java source code necessary to build a solution for the pro.pdfWrite the Java source code necessary to build a solution for the pro.pdf
Write the Java source code necessary to build a solution for the pro.pdf
 
PathOfMostResistance
PathOfMostResistancePathOfMostResistance
PathOfMostResistance
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
 
JAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdfJAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdf
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
 
Help please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdfHelp please!!(Include your modified DList.java source code file in.pdf
Help please!!(Include your modified DList.java source code file in.pdf
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdfImplement the additional 5 methods as indicated in the LinkedList fi.pdf
Implement the additional 5 methods as indicated in the LinkedList fi.pdf
 
singly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malavsingly link list project in dsa.....by rohit malav
singly link list project in dsa.....by rohit malav
 
Step 1 The Pair Class Many times in writing software we come across p.pdf
Step 1 The Pair Class Many times in writing software we come across p.pdfStep 1 The Pair Class Many times in writing software we come across p.pdf
Step 1 The Pair Class Many times in writing software we come across p.pdf
 
You can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdfYou can list anything, it doesnt matter. I just want to see code f.pdf
You can list anything, it doesnt matter. I just want to see code f.pdf
 
import java-util--- public class MyLinkedList{ public static void.pdf
import java-util---  public class MyLinkedList{    public static void.pdfimport java-util---  public class MyLinkedList{    public static void.pdf
import java-util--- public class MyLinkedList{ public static void.pdf
 
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdfimplement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
 
Can someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdfCan someone help me to fix the code please package dlist i.pdf
Can someone help me to fix the code please package dlist i.pdf
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
 
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdfWrite the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
 

More from birajdar2

javaFix in the program belowhandle incomplete data for text fil.pdf
javaFix in the program belowhandle incomplete data for text fil.pdfjavaFix in the program belowhandle incomplete data for text fil.pdf
javaFix in the program belowhandle incomplete data for text fil.pdf
birajdar2
 
How do hydrophobic and hydrophilic hormones differ in their speeds a.pdf
How do hydrophobic and hydrophilic hormones differ in their speeds a.pdfHow do hydrophobic and hydrophilic hormones differ in their speeds a.pdf
How do hydrophobic and hydrophilic hormones differ in their speeds a.pdf
birajdar2
 
Given below is an issue that you have identified as an issue in a ret.pdf
Given below is an issue that you have identified as an issue in a ret.pdfGiven below is an issue that you have identified as an issue in a ret.pdf
Given below is an issue that you have identified as an issue in a ret.pdf
birajdar2
 
21. What is the relationship between the maximum size of aggregates a.pdf
21. What is the relationship between the maximum size of aggregates a.pdf21. What is the relationship between the maximum size of aggregates a.pdf
21. What is the relationship between the maximum size of aggregates a.pdf
birajdar2
 
What are the five stages of team development Describe each stage an.pdf
What are the five stages of team development Describe each stage an.pdfWhat are the five stages of team development Describe each stage an.pdf
What are the five stages of team development Describe each stage an.pdf
birajdar2
 

More from birajdar2 (20)

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

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

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. }