SlideShare a Scribd company logo
1 of 4
Download to read offline
In java.
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 hasInstance 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
Solution
package com.quad.generic;
public class GenDoubleLinkedList {
private ListNode head;
private ListNode tail;
private int size;
public int getSize() {
return size;
}
/**
*
*/
public GenDoubleLinkedList() {
this.head = null;
this.size = 0;
}
@SuppressWarnings("hiding")
class ListNode{
private T data;
private ListNode nextLink;
private ListNode prevLink;
/**
*
*/
public ListNode() {
nextLink=null;
prevLink=null;
data=null;
}
/**
* @param data
* @param nextLink
* @param prevLink
*/
public ListNode(T data, ListNode nextLink,
ListNode prevLink) {
super();
this.data = data;
this.nextLink = nextLink;
this.prevLink = prevLink;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public ListNode getNextLink() {
return nextLink;
}
public void setNextLink(ListNode nextLink) {
this.nextLink = nextLink;
}
public ListNode getPrevLink() {
return prevLink;
}
public void setPrevLink(ListNode prevLink) {
this.prevLink = prevLink;
}
}
public void goToNext(){
ListNode temp = head;
while(temp != null){
temp = temp.getNextLink();
}
}
public void goToPrev() {
ListNode temporary = tail;
while(temporary != null){
temporary = temporary.getPrevLink();
}
}
public T getDataAtCurrent (int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
} else {
ListNode current = head;
for (int i = 0; i < index; i++) {
current = current.nextLink;
}
return current.data;
}
}
public ListNode deleteCurrentNode(){
ListNode temp = head;
if(head.nextLink == null){
tail = null;
}
else{
head.nextLink.prevLink = null;
head = head.nextLink;
}
return temp;
}
}

More Related Content

Similar to In java.Write a class called GenDoubleLinkedList which is a generi.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
arjunstores123
 
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
 
could you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdfcould you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdf
feroz544
 
Write a method expand that could be added to the LinkedlntList class f.docx
Write a method expand that could be added to the LinkedlntList class f.docxWrite a method expand that could be added to the LinkedlntList class f.docx
Write a method expand that could be added to the LinkedlntList class f.docx
noreendchesterton753
 
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdfDesign, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
trishacolsyn25353
 
Draw memory map of this link list code and add insert at last ( java)-.pdf
Draw memory map of this link list code and add insert at last ( java)-.pdfDraw memory map of this link list code and add insert at last ( java)-.pdf
Draw memory map of this link list code and add insert at last ( java)-.pdf
Andrew8IBPatersonk
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
FaheemMahmood2
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docx
gilliandunce53776
 
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdfDividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
tesmondday29076
 
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
 
There are a couple of new methods that you will be writing for this pr.pdf
There are a couple of new methods that you will be writing for this pr.pdfThere are a couple of new methods that you will be writing for this pr.pdf
There are a couple of new methods that you will be writing for this pr.pdf
aamousnowov
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
EricvtJFraserr
 
Singly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptxSingly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptx
ssusera965f6
 

Similar to In java.Write a class called GenDoubleLinkedList which is a generi.pdf (20)

Data Structure
Data StructureData Structure
Data Structure
 
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
 
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
 
could you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdfcould you implement this function please, im having issues with it..pdf
could you implement this function please, im having issues with it..pdf
 
Write a method expand that could be added to the LinkedlntList class f.docx
Write a method expand that could be added to the LinkedlntList class f.docxWrite a method expand that could be added to the LinkedlntList class f.docx
Write a method expand that could be added to the LinkedlntList class f.docx
 
Write a method expand that could be added to the LinkedlntList class .docx
 Write a method expand that could be added to the LinkedlntList class .docx Write a method expand that could be added to the LinkedlntList class .docx
Write a method expand that could be added to the LinkedlntList class .docx
 
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdfDesign, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
 
Draw memory map of this link list code and add insert at last ( java)-.pdf
Draw memory map of this link list code and add insert at last ( java)-.pdfDraw memory map of this link list code and add insert at last ( java)-.pdf
Draw memory map of this link list code and add insert at last ( java)-.pdf
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docx
 
Linked List Basics
Linked List BasicsLinked List Basics
Linked List Basics
 
Linked list
Linked list Linked list
Linked list
 
Given is the IntNode class that represents an element in a linked list (1).docx
Given is the IntNode class that represents an element in a linked list (1).docxGiven is the IntNode class that represents an element in a linked list (1).docx
Given is the IntNode class that represents an element in a linked list (1).docx
 
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdfDividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
 
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
 
Sorting & Linked Lists
Sorting & Linked ListsSorting & Linked Lists
Sorting & Linked Lists
 
There are a couple of new methods that you will be writing for this pr.pdf
There are a couple of new methods that you will be writing for this pr.pdfThere are a couple of new methods that you will be writing for this pr.pdf
There are a couple of new methods that you will be writing for this pr.pdf
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
Singly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptxSingly Linked List_Operations-Traversal.pptx
Singly Linked List_Operations-Traversal.pptx
 

More from SALES97

When acting as a neurotransmitter NO binds to the iron atom in vario.pdf
When acting as a neurotransmitter NO binds to the iron atom in vario.pdfWhen acting as a neurotransmitter NO binds to the iron atom in vario.pdf
When acting as a neurotransmitter NO binds to the iron atom in vario.pdf
SALES97
 
TrueFalse A virtual private network is a way to use the Internet to.pdf
TrueFalse  A virtual private network is a way to use the Internet to.pdfTrueFalse  A virtual private network is a way to use the Internet to.pdf
TrueFalse A virtual private network is a way to use the Internet to.pdf
SALES97
 
The problem can be found below. I would like someone to run this. I .pdf
The problem can be found below. I would like someone to run this. I .pdfThe problem can be found below. I would like someone to run this. I .pdf
The problem can be found below. I would like someone to run this. I .pdf
SALES97
 
The Orlando MedicalOrlando Medical Corporation financial statements.pdf
The Orlando MedicalOrlando Medical Corporation financial statements.pdfThe Orlando MedicalOrlando Medical Corporation financial statements.pdf
The Orlando MedicalOrlando Medical Corporation financial statements.pdf
SALES97
 
The IP network is a virtual network and must rely on a link layer ne.pdf
The IP network is a virtual network and must rely on a link layer ne.pdfThe IP network is a virtual network and must rely on a link layer ne.pdf
The IP network is a virtual network and must rely on a link layer ne.pdf
SALES97
 
State if you agree or disagree with the statement made and whyLes.pdf
State if you agree or disagree with the statement made and whyLes.pdfState if you agree or disagree with the statement made and whyLes.pdf
State if you agree or disagree with the statement made and whyLes.pdf
SALES97
 
subject.....project management.Tools and Processes Based on the pr.pdf
subject.....project management.Tools and Processes Based on the pr.pdfsubject.....project management.Tools and Processes Based on the pr.pdf
subject.....project management.Tools and Processes Based on the pr.pdf
SALES97
 

More from SALES97 (20)

Blossom Corporation had 120,000 common shares outstanding on Decembe.pdf
Blossom Corporation had 120,000 common shares outstanding on Decembe.pdfBlossom Corporation had 120,000 common shares outstanding on Decembe.pdf
Blossom Corporation had 120,000 common shares outstanding on Decembe.pdf
 
A system experiences a phase change from liquid to solid. Is this exo.pdf
A system experiences a phase change from liquid to solid. Is this exo.pdfA system experiences a phase change from liquid to solid. Is this exo.pdf
A system experiences a phase change from liquid to solid. Is this exo.pdf
 
Write an equation of a tangent function with the following characters.pdf
Write an equation of a tangent function with the following characters.pdfWrite an equation of a tangent function with the following characters.pdf
Write an equation of a tangent function with the following characters.pdf
 
Why is a mixture of alcohol and water used, rather than simply water.pdf
Why is a mixture of alcohol and water used, rather than simply water.pdfWhy is a mixture of alcohol and water used, rather than simply water.pdf
Why is a mixture of alcohol and water used, rather than simply water.pdf
 
Why are the 1960s relatively turbulentSolutionAnswer1960s .pdf
Why are the 1960s relatively turbulentSolutionAnswer1960s .pdfWhy are the 1960s relatively turbulentSolutionAnswer1960s .pdf
Why are the 1960s relatively turbulentSolutionAnswer1960s .pdf
 
When acting as a neurotransmitter NO binds to the iron atom in vario.pdf
When acting as a neurotransmitter NO binds to the iron atom in vario.pdfWhen acting as a neurotransmitter NO binds to the iron atom in vario.pdf
When acting as a neurotransmitter NO binds to the iron atom in vario.pdf
 
Which of the following are reasons that Drosophila are an ideal exper.pdf
Which of the following are reasons that Drosophila are an ideal exper.pdfWhich of the following are reasons that Drosophila are an ideal exper.pdf
Which of the following are reasons that Drosophila are an ideal exper.pdf
 
What Michael acceptor is needed for the conjugate addition Beta - .pdf
What Michael acceptor is needed for the conjugate addition  Beta - .pdfWhat Michael acceptor is needed for the conjugate addition  Beta - .pdf
What Michael acceptor is needed for the conjugate addition Beta - .pdf
 
What is the role of intuition in decision-makingSolutionIntui.pdf
What is the role of intuition in decision-makingSolutionIntui.pdfWhat is the role of intuition in decision-makingSolutionIntui.pdf
What is the role of intuition in decision-makingSolutionIntui.pdf
 
What is the difference between a HTML element’s id attribute and nam.pdf
What is the difference between a HTML element’s id attribute and nam.pdfWhat is the difference between a HTML element’s id attribute and nam.pdf
What is the difference between a HTML element’s id attribute and nam.pdf
 
What happens when youre Running Scripts without a console in Linux.pdf
What happens when youre Running Scripts without a console in Linux.pdfWhat happens when youre Running Scripts without a console in Linux.pdf
What happens when youre Running Scripts without a console in Linux.pdf
 
TrueFalse A virtual private network is a way to use the Internet to.pdf
TrueFalse  A virtual private network is a way to use the Internet to.pdfTrueFalse  A virtual private network is a way to use the Internet to.pdf
TrueFalse A virtual private network is a way to use the Internet to.pdf
 
The total physical units accounted for is the sum of the units co.pdf
The total physical units accounted for is the sum of the units co.pdfThe total physical units accounted for is the sum of the units co.pdf
The total physical units accounted for is the sum of the units co.pdf
 
The problem can be found below. I would like someone to run this. I .pdf
The problem can be found below. I would like someone to run this. I .pdfThe problem can be found below. I would like someone to run this. I .pdf
The problem can be found below. I would like someone to run this. I .pdf
 
The probability that a teacher will give an unannounced test duri.pdf
The probability that a teacher will give an unannounced test duri.pdfThe probability that a teacher will give an unannounced test duri.pdf
The probability that a teacher will give an unannounced test duri.pdf
 
The Orlando MedicalOrlando Medical Corporation financial statements.pdf
The Orlando MedicalOrlando Medical Corporation financial statements.pdfThe Orlando MedicalOrlando Medical Corporation financial statements.pdf
The Orlando MedicalOrlando Medical Corporation financial statements.pdf
 
The IP network is a virtual network and must rely on a link layer ne.pdf
The IP network is a virtual network and must rely on a link layer ne.pdfThe IP network is a virtual network and must rely on a link layer ne.pdf
The IP network is a virtual network and must rely on a link layer ne.pdf
 
Table 1 Endowment of Labor and Capital US 100 20 Canada 10 Workers Ma.pdf
Table 1 Endowment of Labor and Capital US 100 20 Canada 10 Workers Ma.pdfTable 1 Endowment of Labor and Capital US 100 20 Canada 10 Workers Ma.pdf
Table 1 Endowment of Labor and Capital US 100 20 Canada 10 Workers Ma.pdf
 
State if you agree or disagree with the statement made and whyLes.pdf
State if you agree or disagree with the statement made and whyLes.pdfState if you agree or disagree with the statement made and whyLes.pdf
State if you agree or disagree with the statement made and whyLes.pdf
 
subject.....project management.Tools and Processes Based on the pr.pdf
subject.....project management.Tools and Processes Based on the pr.pdfsubject.....project management.Tools and Processes Based on the pr.pdf
subject.....project management.Tools and Processes Based on the pr.pdf
 

Recently uploaded

會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
中 央社
 

Recently uploaded (20)

Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....diagnosting testing bsc 2nd sem.pptx....
diagnosting testing bsc 2nd sem.pptx....
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"Mattingly "AI & Prompt Design: Named Entity Recognition"
Mattingly "AI & Prompt Design: Named Entity Recognition"
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
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
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 

In java.Write a class called GenDoubleLinkedList which is a generi.pdf

  • 1. In java. 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 hasInstance 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 Solution
  • 2. package com.quad.generic; public class GenDoubleLinkedList { private ListNode head; private ListNode tail; private int size; public int getSize() { return size; } /** * */ public GenDoubleLinkedList() { this.head = null; this.size = 0; } @SuppressWarnings("hiding") class ListNode{ private T data; private ListNode nextLink; private ListNode prevLink; /** * */ public ListNode() { nextLink=null; prevLink=null; data=null; } /** * @param data * @param nextLink * @param prevLink */
  • 3. public ListNode(T data, ListNode nextLink, ListNode prevLink) { super(); this.data = data; this.nextLink = nextLink; this.prevLink = prevLink; } public T getData() { return data; } public void setData(T data) { this.data = data; } public ListNode getNextLink() { return nextLink; } public void setNextLink(ListNode nextLink) { this.nextLink = nextLink; } public ListNode getPrevLink() { return prevLink; } public void setPrevLink(ListNode prevLink) { this.prevLink = prevLink; } } public void goToNext(){ ListNode temp = head; while(temp != null){ temp = temp.getNextLink(); }
  • 4. } public void goToPrev() { ListNode temporary = tail; while(temporary != null){ temporary = temporary.getPrevLink(); } } public T getDataAtCurrent (int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); } else { ListNode current = head; for (int i = 0; i < index; i++) { current = current.nextLink; } return current.data; } } public ListNode deleteCurrentNode(){ ListNode temp = head; if(head.nextLink == null){ tail = null; } else{ head.nextLink.prevLink = null; head = head.nextLink; } return temp; } }