SlideShare a Scribd company logo
1 of 4
Download to read offline
Can someone help me to fix the code please?
package dlist;
import java.util.Iterator;
public class DList implements Iterable<String> {
private static class DListNode {
public String data;
public DListNode next;
public DListNode previous;
}
private DListNode nil;
private int size;
public DList() {
nil = new DListNode();
nil.previous = nil;
nil.next = nil;
nil.data = null;
size = 0;
}
public void addFirst(String elem) {
DListNode newNode = new DListNode();
newNode.data = elem;
newNode.next = nil.next;
newNode.previous = nil;
nil.next.previous = newNode;
nil.next = newNode;
size++;
}
public void addLast(String elem) {
DListNode newNode = new DListNode();
newNode.data = elem;
newNode.next = nil;
newNode.previous = nil.previous;
nil.previous.next = newNode;
nil.previous = newNode;
size++;
}
public String getFirst() {
if (size == 0) {
throw new RuntimeException("DList is empty");
}
return nil.next.data;
}
public String getLast() {
if (size == 0) {
throw new RuntimeException("DList is empty");
}
return nil.previous.data;
}
public String removeFirst() {
if (size == 0) {
throw new RuntimeException("DList is empty");
}
DListNode first = nil.next;
first.next.previous = nil;
nil.next = first.next;
size--;
return first.data;
}
public String removeLast() {
if (size == 0) {
throw new RuntimeException("DList is empty");
}
DListNode last = nil.previous;
last.previous.next = nil;
nil.previous = last.previous;
size--;
return last.data;
}
public String get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
DListNode node = nil.next;
for (int i = 0; i < index; i++) {
node = node.next;
}
return node.data;
}
public String set(int index, String value) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
DListNode node = nil.next;
for (int i = 0; i < index; i++) {
node = node.next;
}
String oldValue = node.data;
node.data = value;
return oldValue;
}
public boolean contains(Object obj) {
DListNode node = nil.next;
while (node != nil) {
if (node.data.equals(obj)) {
return true;
}
node = node.next;
}
return false;
}
public int size() {
return size;
}
public int indexOf(Object obj)
{
int index = 0;
DListNode node = nil.next;
while (node != nil)
{
if (node.data.equals(obj))
{
return index;
}
index++;
node = node.next;
}
return -1;
}
@Override
public Iterator<String> iterator()
{
return new DListIterator();
}
private class DListIterator implements Iterator<String>
{
private DListNode pointer;
public DListIterator()
{
if(nil.next==nil)
{
pointer = nil;
}
else
pointer=nil.next;
}
public boolean hasNext()
{
return pointer != nil;
}
}
}

More Related Content

Similar to Can someone help me to fix the code please package dlist i.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
mail931892
 
In the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdfIn the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdf
birajdar2
 
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfDoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
aathiauto
 
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
arkmuzikllc
 
computer notes - Data Structures - 3
computer notes - Data Structures - 3computer notes - Data Structures - 3
computer notes - Data Structures - 3
ecomputernotes
 
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdfSOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
arccreation001
 
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
 
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
 
Create a new java class called ListNode. Implement ListNode as a gen.pdf
Create a new java class called ListNode. Implement ListNode as a gen.pdfCreate a new java class called ListNode. Implement ListNode as a gen.pdf
Create a new java class called ListNode. Implement ListNode as a gen.pdf
mohamednihalshahru
 
double linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdfdouble linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdf
facevenky
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdf
sauravmanwanicp
 
For this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docxFor this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docx
mckellarhastings
 
output and explain There is Mylist There is MyArrayList pa.pdf
output and explain There is Mylist There is MyArrayList pa.pdfoutput and explain There is Mylist There is MyArrayList pa.pdf
output and explain There is Mylist There is MyArrayList pa.pdf
access2future1
 
public class Deque {private class Node {public int data;public.pdf
public class Deque {private class Node {public int data;public.pdfpublic class Deque {private class Node {public int data;public.pdf
public class Deque {private class Node {public int data;public.pdf
annaipowerelectronic
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
alphaagenciesindia
 
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
 

Similar to Can someone help me to fix the code please package dlist i.pdf (20)

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
 
In the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdfIn the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdf
 
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdfDoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
DoublyList-cpp- #include -DoublyList-h- using namespace std- void Doub.pdf
 
Lowering in C#: What really happens with your code?, from NDC Oslo 2019
Lowering in C#: What really happens with your code?, from NDC Oslo 2019Lowering in C#: What really happens with your code?, from NDC Oslo 2019
Lowering in C#: What really happens with your code?, from NDC Oslo 2019
 
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
 
computer notes - Data Structures - 3
computer notes - Data Structures - 3computer notes - Data Structures - 3
computer notes - Data Structures - 3
 
i am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdfi am looking for help on the method AddSorted and the method Copy only.pdf
i am looking for help on the method AddSorted and the method Copy only.pdf
 
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdfSOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
SOURCE CODEimport java.util.Iterator;public class CircularLinke.pdf
 
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
 
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
 
Create a new java class called ListNode. Implement ListNode as a gen.pdf
Create a new java class called ListNode. Implement ListNode as a gen.pdfCreate a new java class called ListNode. Implement ListNode as a gen.pdf
Create a new java class called ListNode. Implement ListNode as a gen.pdf
 
double linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdfdouble linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdf
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdf
 
For this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docxFor this micro assignment, you must implement two Linked List functi.docx
For this micro assignment, you must implement two Linked List functi.docx
 
output and explain There is Mylist There is MyArrayList pa.pdf
output and explain There is Mylist There is MyArrayList pa.pdfoutput and explain There is Mylist There is MyArrayList pa.pdf
output and explain There is Mylist There is MyArrayList pa.pdf
 
fix the error - class Node{ int data- Node next-.pdf
fix the error -   class Node{           int data-           Node next-.pdffix the error -   class Node{           int data-           Node next-.pdf
fix the error - class Node{ int data- Node next-.pdf
 
public class Deque {private class Node {public int data;public.pdf
public class Deque {private class Node {public int data;public.pdfpublic class Deque {private class Node {public int data;public.pdf
public class Deque {private class Node {public int data;public.pdf
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
 
Droidcon Poland - From Kotlin to Machine Code
Droidcon Poland - From Kotlin to Machine CodeDroidcon Poland - From Kotlin to Machine Code
Droidcon Poland - From Kotlin to Machine Code
 
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
 

More from ABHISHEKREADYMADESKO

6 points Cat frlends Dr H shares hls home with two cats .pdf
6 points Cat frlends Dr H shares hls home with two cats .pdf6 points Cat frlends Dr H shares hls home with two cats .pdf
6 points Cat frlends Dr H shares hls home with two cats .pdf
ABHISHEKREADYMADESKO
 
Ynetim Liderlik Vaka almas Maria ailelere destek salay.pdf
Ynetim Liderlik Vaka almas  Maria ailelere destek salay.pdfYnetim Liderlik Vaka almas  Maria ailelere destek salay.pdf
Ynetim Liderlik Vaka almas Maria ailelere destek salay.pdf
ABHISHEKREADYMADESKO
 

More from ABHISHEKREADYMADESKO (20)

As a final year Information Technology student your supervi.pdf
As a final year Information Technology student your supervi.pdfAs a final year Information Technology student your supervi.pdf
As a final year Information Technology student your supervi.pdf
 
A To calculate and display the number of records in Random.pdf
A To calculate and display the number of records in Random.pdfA To calculate and display the number of records in Random.pdf
A To calculate and display the number of records in Random.pdf
 
Alice is taking Artificial Intelligence from Professor Bob .pdf
Alice is taking Artificial Intelligence from Professor Bob .pdfAlice is taking Artificial Intelligence from Professor Bob .pdf
Alice is taking Artificial Intelligence from Professor Bob .pdf
 
Assume a Poisson distribution Find the following probabilit.pdf
Assume a Poisson distribution Find the following probabilit.pdfAssume a Poisson distribution Find the following probabilit.pdf
Assume a Poisson distribution Find the following probabilit.pdf
 
6 points Cat frlends Dr H shares hls home with two cats .pdf
6 points Cat frlends Dr H shares hls home with two cats .pdf6 points Cat frlends Dr H shares hls home with two cats .pdf
6 points Cat frlends Dr H shares hls home with two cats .pdf
 
8 If the random vector XY has the joint density fxy.pdf
8 If the random vector XY has the joint density fxy.pdf8 If the random vector XY has the joint density fxy.pdf
8 If the random vector XY has the joint density fxy.pdf
 
cussthon 1 potrilly oovebude to theue fintings GuEsnesi GuE.pdf
cussthon 1 potrilly oovebude to theue fintings GuEsnesi GuE.pdfcussthon 1 potrilly oovebude to theue fintings GuEsnesi GuE.pdf
cussthon 1 potrilly oovebude to theue fintings GuEsnesi GuE.pdf
 
Cules de los siguientes son proyectos y cules son operaci.pdf
Cules de los siguientes son proyectos y cules son operaci.pdfCules de los siguientes son proyectos y cules son operaci.pdf
Cules de los siguientes son proyectos y cules son operaci.pdf
 
Anatomy amp Physiology II Dr Irene Matejio Lymphatic and .pdf
Anatomy amp Physiology II Dr Irene Matejio Lymphatic and .pdfAnatomy amp Physiology II Dr Irene Matejio Lymphatic and .pdf
Anatomy amp Physiology II Dr Irene Matejio Lymphatic and .pdf
 
Aadaki ifadeleri gz nnde bulundurun FCFS lk gelen alr .pdf
Aadaki ifadeleri gz nnde bulundurun FCFS lk gelen alr .pdfAadaki ifadeleri gz nnde bulundurun FCFS lk gelen alr .pdf
Aadaki ifadeleri gz nnde bulundurun FCFS lk gelen alr .pdf
 
Which of the following options must hold simultaneously for .pdf
Which of the following options must hold simultaneously for .pdfWhich of the following options must hold simultaneously for .pdf
Which of the following options must hold simultaneously for .pdf
 
Why would a Grams stain be completed by a laboratory techni.pdf
Why would a Grams stain be completed by a laboratory techni.pdfWhy would a Grams stain be completed by a laboratory techni.pdf
Why would a Grams stain be completed by a laboratory techni.pdf
 
420 If as in Exercise 416 Y has density function fy.pdf
420 If as in Exercise 416 Y has density function fy.pdf420 If as in Exercise 416 Y has density function fy.pdf
420 If as in Exercise 416 Y has density function fy.pdf
 
Ynetim Liderlik Vaka almas Maria ailelere destek salay.pdf
Ynetim Liderlik Vaka almas  Maria ailelere destek salay.pdfYnetim Liderlik Vaka almas  Maria ailelere destek salay.pdf
Ynetim Liderlik Vaka almas Maria ailelere destek salay.pdf
 
Verileri sunmann alternatif yollar yardmc olabilir ancak ha.pdf
Verileri sunmann alternatif yollar yardmc olabilir ancak ha.pdfVerileri sunmann alternatif yollar yardmc olabilir ancak ha.pdf
Verileri sunmann alternatif yollar yardmc olabilir ancak ha.pdf
 
What hindered industrialization in the South a The South i.pdf
What hindered industrialization in the South a The South i.pdfWhat hindered industrialization in the South a The South i.pdf
What hindered industrialization in the South a The South i.pdf
 
Watch What are Nestl doing about plastic pollution plast.pdf
Watch  What are Nestl doing about plastic pollution plast.pdfWatch  What are Nestl doing about plastic pollution plast.pdf
Watch What are Nestl doing about plastic pollution plast.pdf
 
Which of the following declares a public class named Book th.pdf
Which of the following declares a public class named Book th.pdfWhich of the following declares a public class named Book th.pdf
Which of the following declares a public class named Book th.pdf
 
The nurse is assessing a toddler Which of the following com.pdf
The nurse is assessing a toddler Which of the following com.pdfThe nurse is assessing a toddler Which of the following com.pdf
The nurse is assessing a toddler Which of the following com.pdf
 
The random variables X and Y have the following joint densit.pdf
The random variables X and Y have the following joint densit.pdfThe random variables X and Y have the following joint densit.pdf
The random variables X and Y have the following joint densit.pdf
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
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)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
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.
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
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
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
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
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

Can someone help me to fix the code please package dlist i.pdf

  • 1. Can someone help me to fix the code please? package dlist; import java.util.Iterator; public class DList implements Iterable<String> { private static class DListNode { public String data; public DListNode next; public DListNode previous; } private DListNode nil; private int size; public DList() { nil = new DListNode(); nil.previous = nil; nil.next = nil; nil.data = null; size = 0; } public void addFirst(String elem) { DListNode newNode = new DListNode(); newNode.data = elem; newNode.next = nil.next; newNode.previous = nil; nil.next.previous = newNode; nil.next = newNode; size++; } public void addLast(String elem) { DListNode newNode = new DListNode(); newNode.data = elem; newNode.next = nil; newNode.previous = nil.previous; nil.previous.next = newNode; nil.previous = newNode; size++; } public String getFirst() { if (size == 0) { throw new RuntimeException("DList is empty"); } return nil.next.data; }
  • 2. public String getLast() { if (size == 0) { throw new RuntimeException("DList is empty"); } return nil.previous.data; } public String removeFirst() { if (size == 0) { throw new RuntimeException("DList is empty"); } DListNode first = nil.next; first.next.previous = nil; nil.next = first.next; size--; return first.data; } public String removeLast() { if (size == 0) { throw new RuntimeException("DList is empty"); } DListNode last = nil.previous; last.previous.next = nil; nil.previous = last.previous; size--; return last.data; } public String get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); } DListNode node = nil.next; for (int i = 0; i < index; i++) { node = node.next; } return node.data; } public String set(int index, String value) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(); } DListNode node = nil.next; for (int i = 0; i < index; i++) {
  • 3. node = node.next; } String oldValue = node.data; node.data = value; return oldValue; } public boolean contains(Object obj) { DListNode node = nil.next; while (node != nil) { if (node.data.equals(obj)) { return true; } node = node.next; } return false; } public int size() { return size; } public int indexOf(Object obj) { int index = 0; DListNode node = nil.next; while (node != nil) { if (node.data.equals(obj)) { return index; } index++; node = node.next; } return -1; } @Override public Iterator<String> iterator() { return new DListIterator(); } private class DListIterator implements Iterator<String> { private DListNode pointer;
  • 4. public DListIterator() { if(nil.next==nil) { pointer = nil; } else pointer=nil.next; } public boolean hasNext() { return pointer != nil; } } }