SlideShare a Scribd company logo
1 of 5
Download to read offline
This is a homework assignment that has to be done in JAVA.
Objective:
Write a class Called GenDoubleLinkedList which is a generic double linked list. This link list is
similar to the single linked list that was shown in class except that each node in addition to
having data and nextLink (which was originally called link) now has prevLink.
The class GenDoubleLinkedList needs to have the following:
-Internal class ListNode which has:
- Instance Variables:
- data of type T
- nextLink of type ListNode
- prevLink of type ListNode
- Constructors:
- Default
- Parameterized
Instance Variables:
- head of type ListNode which always points to the beginning of the linked list
- current of type ListNode which moves around pointing to one of the nodes
Constructor:
- A default constructor that initializes head to an empty ListNode and sets current to point at the
head.
Methods:
- goToNext – This moves the current node forward in the list by one node. It doesn’t move
forward if that node is null
- goToPrev – This moves the current node backwards in the list by one node. It doesn’t move
backwards if that node is null.
- getDataAtCurrent – returns the data at the current node as long as the current isn’t null
- setDataAtCurrent – takes in a parameter and sets the data at the current node to that value as
long as current is not null
- insertNodeAfterCurrent – creates a new node based on data that is passed in by a parameter and
puts that node after the current position
- deleteCurrentNode – removes the current node from the list by resetting the links
- showList – prints out the contents of the list line-by-line
- inList – returns a true or false value based on whether or not the data passed in via a parameter
is in the list
Insert Node After Current
Delete Current Node Head Current
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
public class GenDoubleLinkedList> {
private class ListNode{
T data;
ListNode nextLink;
ListNode prevLink;
public ListNode(T data) {
this.data = data;
nextLink = null;
prevLink = null;
}
}
// instance variables
private ListNode head;
private ListNode current;
public GenDoubleLinkedList() {
head = new ListNode(null);
current = head;
}
public void goToNext(){
if(current != null && (current == head && head.nextLink != null))
current = current.nextLink;
else if(current != null && current != head)
current = current.nextLink;
}
public void goToPrev(){
if(current != null && current != head)
current = current.prevLink;
}
public T getDataAtCurrent(){
if(current == null || current == head)
return null;
return current.data;
}
public void setDataAtCurrent(T data){
if(current != null && current != head){
current.data = data;
}
}
public void insertNodeAfterCurrent(T data){
if(current == null)
return;
ListNode newNode = new ListNode(data);
newNode.nextLink = current.nextLink;
if(current.nextLink != null)
current.nextLink.prevLink = newNode;
current.nextLink = newNode;
newNode.prevLink = current;
}
public void deleteCurrentNode(){
if(current == null || current == head)
return;
// if current node is pointing to last node of list
if(current.nextLink == null){
current.prevLink.nextLink = null;
current.prevLink = null;
current = null;
}else{
ListNode temp = current.nextLink;
current.prevLink.nextLink = temp;
temp.prevLink = current.prevLink;
current.nextLink = null;
current.prevLink = null;
current = temp;
}
}
public void showList(){
ListNode temp = head.nextLink;
while(temp != null){
System.out.print(temp.data+" ");
temp = temp.nextLink;
}
System.out.println();
}
public boolean inList(T data){
ListNode temp = head.nextLink;
while(temp != null){
if(temp.data.compareTo(data) == 0)
return true;
temp = temp.nextLink;
}
return false;
}
}
public class TestGenDoubleLinkedList {
public static void main(String[] args) {
GenDoubleLinkedList list = new GenDoubleLinkedList<>();
list.insertNodeAfterCurrent(4);
list.insertNodeAfterCurrent(5);
list.insertNodeAfterCurrent(6);
list.showList();
list.goToNext();
list.insertNodeAfterCurrent(7);
list.showList();
list.goToPrev();
list.insertNodeAfterCurrent(8);
list.showList();
}
}
/*
Sample run:
6 5 4
6 7 5 4
8 6 7 5 4
*/

More Related Content

Similar to This is a homework assignment that has to be done in JAVA.Objectiv.pdf

Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdfin Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
sauravmanwanicp
 
Can somebody solve the TODO parts of the following problem- Thanks D.pdf
Can somebody solve the TODO parts of the following problem- Thanks   D.pdfCan somebody solve the TODO parts of the following problem- Thanks   D.pdf
Can somebody solve the TODO parts of the following problem- Thanks D.pdf
vinaythemodel
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
arjunstores123
 
In this assignment you will implement insert() method for a singly l.pdf
In this assignment you will implement insert() method for a singly l.pdfIn this assignment you will implement insert() method for a singly l.pdf
In this assignment you will implement insert() method for a singly l.pdf
fantasiatheoutofthef
 
Singly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptxSingly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptx
ssusera965f6
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
almaniaeyewear
 
Write a generic method named genericListSort that takes a generic li.pdf
Write a generic method named genericListSort that takes a generic li.pdfWrite a generic method named genericListSort that takes a generic li.pdf
Write a generic method named genericListSort that takes a generic li.pdf
archiesshop48
 
Add delete at a position to the code and display it to the code- class.pdf
Add delete at a position to the code and display it to the code- class.pdfAdd delete at a position to the code and display it to the code- class.pdf
Add delete at a position to the code and display it to the code- class.pdf
yrajjoshi
 

Similar to This is a homework assignment that has to be done in JAVA.Objectiv.pdf (20)

Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
 
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdfin Java (ignore the last line thats hidden) Create a doubly linked l.pdf
in Java (ignore the last line thats hidden) Create a doubly linked l.pdf
 
Linked list
Linked list Linked list
Linked list
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Data Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptxData Structure and Algorithm Lesson 2.pptx
Data Structure and Algorithm Lesson 2.pptx
 
Can somebody solve the TODO parts of the following problem- Thanks D.pdf
Can somebody solve the TODO parts of the following problem- Thanks   D.pdfCan somebody solve the TODO parts of the following problem- Thanks   D.pdf
Can somebody solve the TODO parts of the following problem- Thanks D.pdf
 
In the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdfIn the class we extensively discussed a node class called IntNode in.pdf
In the class we extensively discussed a node class called IntNode in.pdf
 
10-linked-list.ppt
10-linked-list.ppt10-linked-list.ppt
10-linked-list.ppt
 
In this assignment you will implement insert() method for a singly l.pdf
In this assignment you will implement insert() method for a singly l.pdfIn this assignment you will implement insert() method for a singly l.pdf
In this assignment you will implement insert() method for a singly l.pdf
 
Singly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptxSingly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptx
 
I am failing a few tests with my Double Linked List program. Can som.pdf
I am failing a few tests with my Double Linked List program. Can som.pdfI am failing a few tests with my Double Linked List program. Can som.pdf
I am failing a few tests with my Double Linked List program. Can som.pdf
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 Sorted number list implementation with linked listsStep 1 Inspec.pdf Sorted number list implementation with linked listsStep 1 Inspec.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Sorting & Linked Lists
Sorting & Linked ListsSorting & Linked Lists
Sorting & Linked Lists
 
Write a generic method named genericListSort that takes a generic li.pdf
Write a generic method named genericListSort that takes a generic li.pdfWrite a generic method named genericListSort that takes a generic li.pdf
Write a generic method named genericListSort that takes a generic li.pdf
 
Add delete at a position to the code and display it to the code- class.pdf
Add delete at a position to the code and display it to the code- class.pdfAdd delete at a position to the code and display it to the code- class.pdf
Add delete at a position to the code and display it to the code- class.pdf
 
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 The MyLinkedList class used in Listing 24.6 is a one-way directional .docx The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
The MyLinkedList class used in Listing 24.6 is a one-way directional .docx
 

More from feelingcomputors

Identify sources and effects of power on organizational structures a.pdf
Identify sources and effects of power on organizational structures a.pdfIdentify sources and effects of power on organizational structures a.pdf
Identify sources and effects of power on organizational structures a.pdf
feelingcomputors
 
How to do the main method for this programBinaryNode.javapublic.pdf
How to do the main method for this programBinaryNode.javapublic.pdfHow to do the main method for this programBinaryNode.javapublic.pdf
How to do the main method for this programBinaryNode.javapublic.pdf
feelingcomputors
 
Explain the managerial approach to public administration and include.pdf
Explain the managerial approach to public administration and include.pdfExplain the managerial approach to public administration and include.pdf
Explain the managerial approach to public administration and include.pdf
feelingcomputors
 
Exercise 12-4 Chipotle Mexican Grill began with a single location in .pdf
Exercise 12-4 Chipotle Mexican Grill began with a single location in .pdfExercise 12-4 Chipotle Mexican Grill began with a single location in .pdf
Exercise 12-4 Chipotle Mexican Grill began with a single location in .pdf
feelingcomputors
 
describe two recent scientific research studies and it therapies inv.pdf
describe two recent scientific research studies and it therapies inv.pdfdescribe two recent scientific research studies and it therapies inv.pdf
describe two recent scientific research studies and it therapies inv.pdf
feelingcomputors
 
Android Studio App that lets you upload pictures to the app.Hello,.pdf
Android Studio App that lets you upload pictures to the app.Hello,.pdfAndroid Studio App that lets you upload pictures to the app.Hello,.pdf
Android Studio App that lets you upload pictures to the app.Hello,.pdf
feelingcomputors
 
Willy owns a small chocolate factory, located close to a river that o.pdf
Willy owns a small chocolate factory, located close to a river that o.pdfWilly owns a small chocolate factory, located close to a river that o.pdf
Willy owns a small chocolate factory, located close to a river that o.pdf
feelingcomputors
 
what historical trends have affected the social resposibilites of bu.pdf
what historical trends have affected the social resposibilites of bu.pdfwhat historical trends have affected the social resposibilites of bu.pdf
what historical trends have affected the social resposibilites of bu.pdf
feelingcomputors
 

More from feelingcomputors (20)

Identify sources and effects of power on organizational structures a.pdf
Identify sources and effects of power on organizational structures a.pdfIdentify sources and effects of power on organizational structures a.pdf
Identify sources and effects of power on organizational structures a.pdf
 
I have .5ml of a stock 3.0ppm F- solution that Im adding to 2.5 ml.pdf
I have .5ml of a stock 3.0ppm F- solution that Im adding to 2.5 ml.pdfI have .5ml of a stock 3.0ppm F- solution that Im adding to 2.5 ml.pdf
I have .5ml of a stock 3.0ppm F- solution that Im adding to 2.5 ml.pdf
 
How to do the main method for this programBinaryNode.javapublic.pdf
How to do the main method for this programBinaryNode.javapublic.pdfHow to do the main method for this programBinaryNode.javapublic.pdf
How to do the main method for this programBinaryNode.javapublic.pdf
 
How many lexemes does the following Java code containGive the num.pdf
How many lexemes does the following Java code containGive the num.pdfHow many lexemes does the following Java code containGive the num.pdf
How many lexemes does the following Java code containGive the num.pdf
 
How many 5-letter words can be formed from the letters S-W-I-N-G .pdf
How many 5-letter words can be formed from the letters S-W-I-N-G .pdfHow many 5-letter words can be formed from the letters S-W-I-N-G .pdf
How many 5-letter words can be formed from the letters S-W-I-N-G .pdf
 
How can companies and customers become interconnected Recently, the.pdf
How can companies and customers become interconnected Recently, the.pdfHow can companies and customers become interconnected Recently, the.pdf
How can companies and customers become interconnected Recently, the.pdf
 
H0w to Secured Bluetooth DeviceSolutionAnswerBluetooth is be.pdf
H0w to Secured Bluetooth DeviceSolutionAnswerBluetooth is be.pdfH0w to Secured Bluetooth DeviceSolutionAnswerBluetooth is be.pdf
H0w to Secured Bluetooth DeviceSolutionAnswerBluetooth is be.pdf
 
Explain the managerial approach to public administration and include.pdf
Explain the managerial approach to public administration and include.pdfExplain the managerial approach to public administration and include.pdf
Explain the managerial approach to public administration and include.pdf
 
Exercise 12-4 Chipotle Mexican Grill began with a single location in .pdf
Exercise 12-4 Chipotle Mexican Grill began with a single location in .pdfExercise 12-4 Chipotle Mexican Grill began with a single location in .pdf
Exercise 12-4 Chipotle Mexican Grill began with a single location in .pdf
 
describe two recent scientific research studies and it therapies inv.pdf
describe two recent scientific research studies and it therapies inv.pdfdescribe two recent scientific research studies and it therapies inv.pdf
describe two recent scientific research studies and it therapies inv.pdf
 
Differentiating Events that Occur Before and After Ovulation In the O.pdf
Differentiating Events that Occur Before and After Ovulation In the O.pdfDifferentiating Events that Occur Before and After Ovulation In the O.pdf
Differentiating Events that Occur Before and After Ovulation In the O.pdf
 
Android Studio App that lets you upload pictures to the app.Hello,.pdf
Android Studio App that lets you upload pictures to the app.Hello,.pdfAndroid Studio App that lets you upload pictures to the app.Hello,.pdf
Android Studio App that lets you upload pictures to the app.Hello,.pdf
 
A man starts walking north at 3 fts from a point P. Five minutes la.pdf
A man starts walking north at 3 fts from a point P. Five minutes la.pdfA man starts walking north at 3 fts from a point P. Five minutes la.pdf
A man starts walking north at 3 fts from a point P. Five minutes la.pdf
 
11. Briefly describe the function of the following in protein synthe.pdf
11. Briefly describe the function of the following in protein synthe.pdf11. Briefly describe the function of the following in protein synthe.pdf
11. Briefly describe the function of the following in protein synthe.pdf
 
You save exist1 the first day of the month, exist2 the second day, ex.pdf
You save exist1 the first day of the month, exist2 the second day, ex.pdfYou save exist1 the first day of the month, exist2 the second day, ex.pdf
You save exist1 the first day of the month, exist2 the second day, ex.pdf
 
Willy owns a small chocolate factory, located close to a river that o.pdf
Willy owns a small chocolate factory, located close to a river that o.pdfWilly owns a small chocolate factory, located close to a river that o.pdf
Willy owns a small chocolate factory, located close to a river that o.pdf
 
Which of the following are used as cloning vectors Select all that a.pdf
Which of the following are used as cloning vectors Select all that a.pdfWhich of the following are used as cloning vectors Select all that a.pdf
Which of the following are used as cloning vectors Select all that a.pdf
 
When are bank deposits createdWhen businesses pay wages and salar.pdf
When are bank deposits createdWhen businesses pay wages and salar.pdfWhen are bank deposits createdWhen businesses pay wages and salar.pdf
When are bank deposits createdWhen businesses pay wages and salar.pdf
 
what historical trends have affected the social resposibilites of bu.pdf
what historical trends have affected the social resposibilites of bu.pdfwhat historical trends have affected the social resposibilites of bu.pdf
what historical trends have affected the social resposibilites of bu.pdf
 
What are some alternate perspectives that HRM professionals may purs.pdf
What are some alternate perspectives that HRM professionals may purs.pdfWhat are some alternate perspectives that HRM professionals may purs.pdf
What are some alternate perspectives that HRM professionals may purs.pdf
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
EADTU
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
Elizabeth Walsh
 

Recently uploaded (20)

VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
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
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdfDiuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.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
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
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
 

This is a homework assignment that has to be done in JAVA.Objectiv.pdf

  • 1. This is a homework assignment that has to be done in JAVA. Objective: Write a class Called GenDoubleLinkedList which is a generic double linked list. This link list is similar to the single linked list that was shown in class except that each node in addition to having data and nextLink (which was originally called link) now has prevLink. The class GenDoubleLinkedList needs to have the following: -Internal class ListNode which has: - Instance Variables: - data of type T - nextLink of type ListNode - prevLink of type ListNode - Constructors: - Default - Parameterized Instance Variables: - head of type ListNode which always points to the beginning of the linked list - current of type ListNode which moves around pointing to one of the nodes Constructor: - A default constructor that initializes head to an empty ListNode and sets current to point at the head. Methods: - goToNext – This moves the current node forward in the list by one node. It doesn’t move forward if that node is null - goToPrev – This moves the current node backwards in the list by one node. It doesn’t move backwards if that node is null. - getDataAtCurrent – returns the data at the current node as long as the current isn’t null - setDataAtCurrent – takes in a parameter and sets the data at the current node to that value as long as current is not null - insertNodeAfterCurrent – creates a new node based on data that is passed in by a parameter and puts that node after the current position - deleteCurrentNode – removes the current node from the list by resetting the links - showList – prints out the contents of the list line-by-line - inList – returns a true or false value based on whether or not the data passed in via a parameter is in the list Insert Node After Current
  • 2. Delete Current Node Head Current Solution Hi, Please find my implementation. Please let me know in case of any issue. public class GenDoubleLinkedList> { private class ListNode{ T data; ListNode nextLink; ListNode prevLink; public ListNode(T data) { this.data = data; nextLink = null; prevLink = null; } } // instance variables private ListNode head; private ListNode current; public GenDoubleLinkedList() { head = new ListNode(null); current = head; } public void goToNext(){ if(current != null && (current == head && head.nextLink != null)) current = current.nextLink; else if(current != null && current != head) current = current.nextLink; }
  • 3. public void goToPrev(){ if(current != null && current != head) current = current.prevLink; } public T getDataAtCurrent(){ if(current == null || current == head) return null; return current.data; } public void setDataAtCurrent(T data){ if(current != null && current != head){ current.data = data; } } public void insertNodeAfterCurrent(T data){ if(current == null) return; ListNode newNode = new ListNode(data); newNode.nextLink = current.nextLink; if(current.nextLink != null) current.nextLink.prevLink = newNode; current.nextLink = newNode; newNode.prevLink = current; } public void deleteCurrentNode(){ if(current == null || current == head) return;
  • 4. // if current node is pointing to last node of list if(current.nextLink == null){ current.prevLink.nextLink = null; current.prevLink = null; current = null; }else{ ListNode temp = current.nextLink; current.prevLink.nextLink = temp; temp.prevLink = current.prevLink; current.nextLink = null; current.prevLink = null; current = temp; } } public void showList(){ ListNode temp = head.nextLink; while(temp != null){ System.out.print(temp.data+" "); temp = temp.nextLink; } System.out.println(); } public boolean inList(T data){ ListNode temp = head.nextLink; while(temp != null){ if(temp.data.compareTo(data) == 0) return true; temp = temp.nextLink; } return false;
  • 5. } } public class TestGenDoubleLinkedList { public static void main(String[] args) { GenDoubleLinkedList list = new GenDoubleLinkedList<>(); list.insertNodeAfterCurrent(4); list.insertNodeAfterCurrent(5); list.insertNodeAfterCurrent(6); list.showList(); list.goToNext(); list.insertNodeAfterCurrent(7); list.showList(); list.goToPrev(); list.insertNodeAfterCurrent(8); list.showList(); } } /* Sample run: 6 5 4 6 7 5 4 8 6 7 5 4 */