SlideShare a Scribd company logo
1 of 8
Download to read offline
package com.java2novice.ds.linkedlist;
import java.util.NoSuchElementException;
public class DoublyLinkedListImpl {
private Node head;
private Node tail;
private int size;
public DoublyLinkedListImpl() {
size = 0;
}
/**
* this class keeps track of each element information
* @author java2novice
*
*/
private class Node {
E element;
Node next;
Node prev;
public Node(E element, Node next, Node prev) {
this.element = element;
this.next = next;
this.prev = prev;
}
}
/**
* returns the size of the linked list
* @return
*/
public int size() { return size; }
/**
* return whether the list is empty or not
* @return
*/
public boolean isEmpty() { return size == 0; }
/**
* adds element at the starting of the linked list
* @param element
*/
public void addFirst(E element) {
Node tmp = new Node(element, head, null);
if(head != null ) {head.prev = tmp;}
head = tmp;
if(tail == null) { tail = tmp;}
size++;
System.out.println("adding: "+element);
}
/**
* adds element at the end of the linked list
* @param element
*/
public void addLast(E element) {
Node tmp = new Node(element, null, tail);
if(tail != null) {tail.next = tmp;}
tail = tmp;
if(head == null) { head = tmp;}
size++;
System.out.println("adding: "+element);
}
/**
* this method walks forward through the linked list
*/
public void iterateForward(){
System.out.println("iterating forward..");
Node tmp = head;
while(tmp != null){
System.out.println(tmp.element);
tmp = tmp.next;
}
}
/**
* this method walks backward through the linked list
*/
public void iterateBackward(){
System.out.println("iterating backword..");
Node tmp = tail;
while(tmp != null){
System.out.println(tmp.element);
tmp = tmp.prev;
}
}
/**
* this method removes element from the start of the linked list
* @return
*/
public E removeFirst() {
if (size == 0) throw new NoSuchElementException();
Node tmp = head;
head = head.next;
head.prev = null;
size--;
System.out.println("deleted: "+tmp.element);
return tmp.element;
}
/**
* this method removes element from the end of the linked list
* @return
*/
public E removeLast() {
if (size == 0) throw new NoSuchElementException();
Node tmp = tail;
tail = tail.prev;
tail.next = null;
size--;
System.out.println("deleted: "+tmp.element);
return tmp.element;
}
public static void main(String a[]){
DoublyLinkedListImpl dll = new DoublyLinkedListImpl();
dll.addFirst(10);
dll.addFirst(34);
dll.addLast(56);
dll.addLast(364);
dll.iterateForward();
dll.removeFirst();
dll.removeLast();
dll.iterateBackward();
}
}
Solution
package com.java2novice.ds.linkedlist;
import java.util.NoSuchElementException;
public class DoublyLinkedListImpl {
private Node head;
private Node tail;
private int size;
public DoublyLinkedListImpl() {
size = 0;
}
/**
* this class keeps track of each element information
* @author java2novice
*
*/
private class Node {
E element;
Node next;
Node prev;
public Node(E element, Node next, Node prev) {
this.element = element;
this.next = next;
this.prev = prev;
}
}
/**
* returns the size of the linked list
* @return
*/
public int size() { return size; }
/**
* return whether the list is empty or not
* @return
*/
public boolean isEmpty() { return size == 0; }
/**
* adds element at the starting of the linked list
* @param element
*/
public void addFirst(E element) {
Node tmp = new Node(element, head, null);
if(head != null ) {head.prev = tmp;}
head = tmp;
if(tail == null) { tail = tmp;}
size++;
System.out.println("adding: "+element);
}
/**
* adds element at the end of the linked list
* @param element
*/
public void addLast(E element) {
Node tmp = new Node(element, null, tail);
if(tail != null) {tail.next = tmp;}
tail = tmp;
if(head == null) { head = tmp;}
size++;
System.out.println("adding: "+element);
}
/**
* this method walks forward through the linked list
*/
public void iterateForward(){
System.out.println("iterating forward..");
Node tmp = head;
while(tmp != null){
System.out.println(tmp.element);
tmp = tmp.next;
}
}
/**
* this method walks backward through the linked list
*/
public void iterateBackward(){
System.out.println("iterating backword..");
Node tmp = tail;
while(tmp != null){
System.out.println(tmp.element);
tmp = tmp.prev;
}
}
/**
* this method removes element from the start of the linked list
* @return
*/
public E removeFirst() {
if (size == 0) throw new NoSuchElementException();
Node tmp = head;
head = head.next;
head.prev = null;
size--;
System.out.println("deleted: "+tmp.element);
return tmp.element;
}
/**
* this method removes element from the end of the linked list
* @return
*/
public E removeLast() {
if (size == 0) throw new NoSuchElementException();
Node tmp = tail;
tail = tail.prev;
tail.next = null;
size--;
System.out.println("deleted: "+tmp.element);
return tmp.element;
}
public static void main(String a[]){
DoublyLinkedListImpl dll = new DoublyLinkedListImpl();
dll.addFirst(10);
dll.addFirst(34);
dll.addLast(56);
dll.addLast(364);
dll.iterateForward();
dll.removeFirst();
dll.removeLast();
dll.iterateBackward();
}
}

More Related Content

Similar to package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf

Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfannaelctronics
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfarrowmobile
 
practice of using and manipulating a doubly linked list and (2) prac.pdf
practice of using and manipulating a doubly linked list and (2) prac.pdfpractice of using and manipulating a doubly linked list and (2) prac.pdf
practice of using and manipulating a doubly linked list and (2) prac.pdfrd1742
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfStewart29UReesa
 
Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxcgraciela1
 
How do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdfHow do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdffmac5
 
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.pdfmalavshah9013
 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfmail931892
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxVictorzH8Bondx
 
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdfLabProgram.javaimport java.util.NoSuchElementException;public .pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdffantasiatheoutofthef
 
A linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdfA linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdfkisgstin23
 
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.pdfarchgeetsenterprises
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfseoagam1
 
Fix my codeCode.pdf
Fix my codeCode.pdfFix my codeCode.pdf
Fix my codeCode.pdfConint29
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docxVictorXUQGloverl
 
we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdfgudduraza28
 
package linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdfpackage linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdfarcellzone
 
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 .docxKomlin1
 
Please do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdfPlease do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdfaioils
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdffcaindore
 

Similar to package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf (20)

Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdf
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
practice of using and manipulating a doubly linked list and (2) prac.pdf
practice of using and manipulating a doubly linked list and (2) prac.pdfpractice of using and manipulating a doubly linked list and (2) prac.pdf
practice of using and manipulating a doubly linked list and (2) prac.pdf
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
 
Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
 
How do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.pdfHow do I fix it in javaLinkedList.java Defines a doubl.pdf
How do I fix it in javaLinkedList.java Defines a doubl.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
 
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdfHow do I fix it in LinkedList.javaLinkedList.java Define.pdf
How do I fix it in LinkedList.javaLinkedList.java Define.pdf
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
 
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdfLabProgram.javaimport java.util.NoSuchElementException;public .pdf
LabProgram.javaimport java.util.NoSuchElementException;public .pdf
 
A linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.pdfA linked stack is implemented using a standard Node class as follows.pdf
A linked stack is implemented using a standard Node class as follows.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
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdf
 
Fix my codeCode.pdf
Fix my codeCode.pdfFix my codeCode.pdf
Fix my codeCode.pdf
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
 
we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdf
 
package linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.pdfpackage linkedLists- import java-util-Iterator- --- A class representi.pdf
package linkedLists- import java-util-Iterator- --- A class representi.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
 
Please do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdfPlease do parts labeled TODO LinkedList.java Replace.pdf
Please do parts labeled TODO LinkedList.java Replace.pdf
 
This is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdfThis is problem is same problem which i submitted on 22017, I just.pdf
This is problem is same problem which i submitted on 22017, I just.pdf
 

More from aptind

ssian chemist, Dmitri Mendeleev is often consider.pdf
                     ssian chemist, Dmitri Mendeleev is often consider.pdf                     ssian chemist, Dmitri Mendeleev is often consider.pdf
ssian chemist, Dmitri Mendeleev is often consider.pdfaptind
 
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
                     moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf                     moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdfaptind
 
               CLOUD COMPUTING -----------------------------------.pdf
               CLOUD COMPUTING -----------------------------------.pdf               CLOUD COMPUTING -----------------------------------.pdf
               CLOUD COMPUTING -----------------------------------.pdfaptind
 
You cannot.SolutionYou cannot..pdf
You cannot.SolutionYou cannot..pdfYou cannot.SolutionYou cannot..pdf
You cannot.SolutionYou cannot..pdfaptind
 
ViVi is universally available on Unix systems. It has been around.pdf
ViVi is universally available on Unix systems. It has been around.pdfViVi is universally available on Unix systems. It has been around.pdf
ViVi is universally available on Unix systems. It has been around.pdfaptind
 
Waterfall methodThe model consists of various phases based on the.pdf
Waterfall methodThe model consists of various phases based on the.pdfWaterfall methodThe model consists of various phases based on the.pdf
Waterfall methodThe model consists of various phases based on the.pdfaptind
 
Hi, I am unable to understand the terminology in .pdf
                     Hi, I am unable to understand the terminology in .pdf                     Hi, I am unable to understand the terminology in .pdf
Hi, I am unable to understand the terminology in .pdfaptind
 
The main function of cerebellum is to control the motor movements. H.pdf
The main function of cerebellum is to control the motor movements. H.pdfThe main function of cerebellum is to control the motor movements. H.pdf
The main function of cerebellum is to control the motor movements. H.pdfaptind
 
Starting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdfStarting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdfaptind
 
solution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdfsolution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdfaptind
 
Sexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdfSexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdfaptind
 
And is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdfAnd is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdfaptind
 
import java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdfimport java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdfaptind
 
Hi please find my code.import java.util.HashMap;import java.util.pdf
Hi please find my code.import java.util.HashMap;import java.util.pdfHi please find my code.import java.util.HashMap;import java.util.pdf
Hi please find my code.import java.util.HashMap;import java.util.pdfaptind
 
Given below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdfGiven below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdfaptind
 
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdfCisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdfaptind
 
As we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdfAs we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdfaptind
 
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdfAmount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdfaptind
 
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdfaptind
 
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdfaptind
 

More from aptind (20)

ssian chemist, Dmitri Mendeleev is often consider.pdf
                     ssian chemist, Dmitri Mendeleev is often consider.pdf                     ssian chemist, Dmitri Mendeleev is often consider.pdf
ssian chemist, Dmitri Mendeleev is often consider.pdf
 
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
                     moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf                     moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
 
               CLOUD COMPUTING -----------------------------------.pdf
               CLOUD COMPUTING -----------------------------------.pdf               CLOUD COMPUTING -----------------------------------.pdf
               CLOUD COMPUTING -----------------------------------.pdf
 
You cannot.SolutionYou cannot..pdf
You cannot.SolutionYou cannot..pdfYou cannot.SolutionYou cannot..pdf
You cannot.SolutionYou cannot..pdf
 
ViVi is universally available on Unix systems. It has been around.pdf
ViVi is universally available on Unix systems. It has been around.pdfViVi is universally available on Unix systems. It has been around.pdf
ViVi is universally available on Unix systems. It has been around.pdf
 
Waterfall methodThe model consists of various phases based on the.pdf
Waterfall methodThe model consists of various phases based on the.pdfWaterfall methodThe model consists of various phases based on the.pdf
Waterfall methodThe model consists of various phases based on the.pdf
 
Hi, I am unable to understand the terminology in .pdf
                     Hi, I am unable to understand the terminology in .pdf                     Hi, I am unable to understand the terminology in .pdf
Hi, I am unable to understand the terminology in .pdf
 
The main function of cerebellum is to control the motor movements. H.pdf
The main function of cerebellum is to control the motor movements. H.pdfThe main function of cerebellum is to control the motor movements. H.pdf
The main function of cerebellum is to control the motor movements. H.pdf
 
Starting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdfStarting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdf
 
solution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdfsolution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdf
 
Sexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdfSexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdf
 
And is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdfAnd is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdf
 
import java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdfimport java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdf
 
Hi please find my code.import java.util.HashMap;import java.util.pdf
Hi please find my code.import java.util.HashMap;import java.util.pdfHi please find my code.import java.util.HashMap;import java.util.pdf
Hi please find my code.import java.util.HashMap;import java.util.pdf
 
Given below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdfGiven below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdf
 
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdfCisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
 
As we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdfAs we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdf
 
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdfAmount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
 
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
 
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf1.They trade away higher fecundity for future reproduction.2.Resou.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
 

Recently uploaded

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.christianmathematics
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
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.pptxDenish Jangid
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
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 17Celine George
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
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.pptxAreebaZafar22
 

Recently uploaded (20)

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.
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
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
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
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
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
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
 

package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf

  • 1. package com.java2novice.ds.linkedlist; import java.util.NoSuchElementException; public class DoublyLinkedListImpl { private Node head; private Node tail; private int size; public DoublyLinkedListImpl() { size = 0; } /** * this class keeps track of each element information * @author java2novice * */ private class Node { E element; Node next; Node prev; public Node(E element, Node next, Node prev) { this.element = element; this.next = next; this.prev = prev; } } /** * returns the size of the linked list * @return */ public int size() { return size; } /** * return whether the list is empty or not * @return */
  • 2. public boolean isEmpty() { return size == 0; } /** * adds element at the starting of the linked list * @param element */ public void addFirst(E element) { Node tmp = new Node(element, head, null); if(head != null ) {head.prev = tmp;} head = tmp; if(tail == null) { tail = tmp;} size++; System.out.println("adding: "+element); } /** * adds element at the end of the linked list * @param element */ public void addLast(E element) { Node tmp = new Node(element, null, tail); if(tail != null) {tail.next = tmp;} tail = tmp; if(head == null) { head = tmp;} size++; System.out.println("adding: "+element); } /** * this method walks forward through the linked list */ public void iterateForward(){ System.out.println("iterating forward.."); Node tmp = head;
  • 3. while(tmp != null){ System.out.println(tmp.element); tmp = tmp.next; } } /** * this method walks backward through the linked list */ public void iterateBackward(){ System.out.println("iterating backword.."); Node tmp = tail; while(tmp != null){ System.out.println(tmp.element); tmp = tmp.prev; } } /** * this method removes element from the start of the linked list * @return */ public E removeFirst() { if (size == 0) throw new NoSuchElementException(); Node tmp = head; head = head.next; head.prev = null; size--; System.out.println("deleted: "+tmp.element); return tmp.element; } /** * this method removes element from the end of the linked list * @return
  • 4. */ public E removeLast() { if (size == 0) throw new NoSuchElementException(); Node tmp = tail; tail = tail.prev; tail.next = null; size--; System.out.println("deleted: "+tmp.element); return tmp.element; } public static void main(String a[]){ DoublyLinkedListImpl dll = new DoublyLinkedListImpl(); dll.addFirst(10); dll.addFirst(34); dll.addLast(56); dll.addLast(364); dll.iterateForward(); dll.removeFirst(); dll.removeLast(); dll.iterateBackward(); } } Solution package com.java2novice.ds.linkedlist; import java.util.NoSuchElementException; public class DoublyLinkedListImpl { private Node head; private Node tail; private int size; public DoublyLinkedListImpl() { size = 0;
  • 5. } /** * this class keeps track of each element information * @author java2novice * */ private class Node { E element; Node next; Node prev; public Node(E element, Node next, Node prev) { this.element = element; this.next = next; this.prev = prev; } } /** * returns the size of the linked list * @return */ public int size() { return size; } /** * return whether the list is empty or not * @return */ public boolean isEmpty() { return size == 0; } /** * adds element at the starting of the linked list * @param element */ public void addFirst(E element) { Node tmp = new Node(element, head, null); if(head != null ) {head.prev = tmp;} head = tmp;
  • 6. if(tail == null) { tail = tmp;} size++; System.out.println("adding: "+element); } /** * adds element at the end of the linked list * @param element */ public void addLast(E element) { Node tmp = new Node(element, null, tail); if(tail != null) {tail.next = tmp;} tail = tmp; if(head == null) { head = tmp;} size++; System.out.println("adding: "+element); } /** * this method walks forward through the linked list */ public void iterateForward(){ System.out.println("iterating forward.."); Node tmp = head; while(tmp != null){ System.out.println(tmp.element); tmp = tmp.next; } } /** * this method walks backward through the linked list */ public void iterateBackward(){
  • 7. System.out.println("iterating backword.."); Node tmp = tail; while(tmp != null){ System.out.println(tmp.element); tmp = tmp.prev; } } /** * this method removes element from the start of the linked list * @return */ public E removeFirst() { if (size == 0) throw new NoSuchElementException(); Node tmp = head; head = head.next; head.prev = null; size--; System.out.println("deleted: "+tmp.element); return tmp.element; } /** * this method removes element from the end of the linked list * @return */ public E removeLast() { if (size == 0) throw new NoSuchElementException(); Node tmp = tail; tail = tail.prev; tail.next = null; size--; System.out.println("deleted: "+tmp.element); return tmp.element; }
  • 8. public static void main(String a[]){ DoublyLinkedListImpl dll = new DoublyLinkedListImpl(); dll.addFirst(10); dll.addFirst(34); dll.addLast(56); dll.addLast(364); dll.iterateForward(); dll.removeFirst(); dll.removeLast(); dll.iterateBackward(); } }