SlideShare a Scribd company logo
/**
* Exception to indicate that Singly LinkedList is empty.
*/
class LinkedListEmptyException extends RuntimeException {
public LinkedListEmptyException() {
super();
}
public LinkedListEmptyException(String message) {
super(message);
}
}
/**
* Node class, which holds data and contains next which points to next Node.
*/
class Node {
public int data; // data in Node.
public Node next; // points to next Node in list.
/**
* Constructor
*/
public Node(int data) {
this.data = data;
}
/**
* Display Node's data
*/
public void displayNode() {
System.out.print(data + " ");
}
}
/**
* Singly LinkedList class
*/
class LinkedList {
private Node first; // ref to first link on list
/**
* LinkedList constructor
*/
public LinkedList() {
first = null;
}
/**
* Insert New Node at first position
*/
public void insertFirst(int data) {
Node newNode = new Node(data); // Creation of New Node.
newNode.next = first; // newLink ---> old first
first = newNode; // first ---> newNode
}
/**
* Method deletes specific Node from Singly LinkedList in java.
*/
public Node deleteSpecificNode(int deleteKey) {
// Case1: when there is no element in LinkedList
if (first == null) { // means LinkedList in empty, throw exception.
throw new LinkedListEmptyException(
"LinkedList doesn't contain any Nodes.");
}
// Case2: when there is only one element in LinkedList- check whether we
// have to delete that Node or not.
if (first.data == deleteKey) { // means LinkedList consists of only one
// element, delete that.
Node tempNode = first; // save reference to first Node in tempNode-
// so that we could return saved reference.
first = first.next;
System.out.println("Node with data=" + tempNode.data
+ " was found on first and has been deleted.");
return tempNode; // return deleted Node.
}
// Case3: when there are atLeast two elements in LinkedList
Node previous = null;
Node current = first;
while (current != null) {
if (current.data == deleteKey) {
System.out.println("Node with data=" + current.data
+ " has been deleted.");
previous.next = current.next; // make previous node's next point
// to current node's next.
return current; // return deleted Node.
} else {
if (current.next == null) { // Means Node wasn't found.
System.out.println("Node with data=" + deleteKey
+ " wasn't found for deletion.");
return null;
}
previous = current;
current = current.next; // move to next node.
}
}
return null;
}
/**
* Display Singly LinkedList
*/
public void displayLinkedList() {
System.out.print("Displaying LinkedList [first--->last]: ");
Node tempDisplay = first; // start at the beginning of linkedList
while (tempDisplay != null) { // Executes until we don't find end of
// list.
tempDisplay.displayNode();
tempDisplay = tempDisplay.next; // move to next Node
System.out.print("-->");
}
System.out.println();
}
}
/**
* Main class - To test LinkedList.
*/
public class SinglyLinkedListDeleteNodeExample {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList(); // creation of Linked List
linkedList.insertFirst(92);
linkedList.insertFirst(20);
linkedList.insertFirst(19);
linkedList.insertFirst(29);
linkedList.displayLinkedList(); // display LinkedList
linkedList.deleteSpecificNode(20);
linkedList.displayLinkedList(); // Again display LinkedList
}
}
OUTPUT:
Displaying LinkedList [first--->last]: 29 -->19 -->20 -->92 -->
Node with data=20 has been deleted.
Node with data=11 wasn't found for deletion.
Displaying LinkedList [first--->last]: 29 -->19 -->92 -->
Solution
/**
* Exception to indicate that Singly LinkedList is empty.
*/
class LinkedListEmptyException extends RuntimeException {
public LinkedListEmptyException() {
super();
}
public LinkedListEmptyException(String message) {
super(message);
}
}
/**
* Node class, which holds data and contains next which points to next Node.
*/
class Node {
public int data; // data in Node.
public Node next; // points to next Node in list.
/**
* Constructor
*/
public Node(int data) {
this.data = data;
}
/**
* Display Node's data
*/
public void displayNode() {
System.out.print(data + " ");
}
}
/**
* Singly LinkedList class
*/
class LinkedList {
private Node first; // ref to first link on list
/**
* LinkedList constructor
*/
public LinkedList() {
first = null;
}
/**
* Insert New Node at first position
*/
public void insertFirst(int data) {
Node newNode = new Node(data); // Creation of New Node.
newNode.next = first; // newLink ---> old first
first = newNode; // first ---> newNode
}
/**
* Method deletes specific Node from Singly LinkedList in java.
*/
public Node deleteSpecificNode(int deleteKey) {
// Case1: when there is no element in LinkedList
if (first == null) { // means LinkedList in empty, throw exception.
throw new LinkedListEmptyException(
"LinkedList doesn't contain any Nodes.");
}
// Case2: when there is only one element in LinkedList- check whether we
// have to delete that Node or not.
if (first.data == deleteKey) { // means LinkedList consists of only one
// element, delete that.
Node tempNode = first; // save reference to first Node in tempNode-
// so that we could return saved reference.
first = first.next;
System.out.println("Node with data=" + tempNode.data
+ " was found on first and has been deleted.");
return tempNode; // return deleted Node.
}
// Case3: when there are atLeast two elements in LinkedList
Node previous = null;
Node current = first;
while (current != null) {
if (current.data == deleteKey) {
System.out.println("Node with data=" + current.data
+ " has been deleted.");
previous.next = current.next; // make previous node's next point
// to current node's next.
return current; // return deleted Node.
} else {
if (current.next == null) { // Means Node wasn't found.
System.out.println("Node with data=" + deleteKey
+ " wasn't found for deletion.");
return null;
}
previous = current;
current = current.next; // move to next node.
}
}
return null;
}
/**
* Display Singly LinkedList
*/
public void displayLinkedList() {
System.out.print("Displaying LinkedList [first--->last]: ");
Node tempDisplay = first; // start at the beginning of linkedList
while (tempDisplay != null) { // Executes until we don't find end of
// list.
tempDisplay.displayNode();
tempDisplay = tempDisplay.next; // move to next Node
System.out.print("-->");
}
System.out.println();
}
}
/**
* Main class - To test LinkedList.
*/
public class SinglyLinkedListDeleteNodeExample {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList(); // creation of Linked List
linkedList.insertFirst(92);
linkedList.insertFirst(20);
linkedList.insertFirst(19);
linkedList.insertFirst(29);
linkedList.displayLinkedList(); // display LinkedList
linkedList.deleteSpecificNode(20);
linkedList.displayLinkedList(); // Again display LinkedList
}
}
OUTPUT:
Displaying LinkedList [first--->last]: 29 -->19 -->20 -->92 -->
Node with data=20 has been deleted.
Node with data=11 wasn't found for deletion.
Displaying LinkedList [first--->last]: 29 -->19 -->92 -->
Ad

More Related Content

Similar to Exception to indicate that Singly LinkedList is empty. .pdf (20)

In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
flashfashioncasualwe
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
climatecontrolsv
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
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
fmac5
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
malavshah9013
 
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
mail931892
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
fathimafancyjeweller
 
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
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
HUST
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
HUST
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
kostikjaylonshaewe47
 
Note             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdfNote             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdf
Ankitchhabra28
 
Jhtp5 20 Datastructures
Jhtp5 20 DatastructuresJhtp5 20 Datastructures
Jhtp5 20 Datastructures
martha leon
 
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdfWrite the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
fathimalinks
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
mail931892
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
freddysarabia1
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
ARCHANASTOREKOTA
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
fathimahardwareelect
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
RashidFaridChishti
 
TutorialII_Updated____niceupdateprogram.pdf
TutorialII_Updated____niceupdateprogram.pdfTutorialII_Updated____niceupdateprogram.pdf
TutorialII_Updated____niceupdateprogram.pdf
BappyAgarwal
 
In C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdfIn C++Write a recursive function to determine whether or not a Lin.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
flashfashioncasualwe
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
climatecontrolsv
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
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
fmac5
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
malavshah9013
 
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
mail931892
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
fathimafancyjeweller
 
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
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
HUST
 
Csphtp1 23
Csphtp1 23Csphtp1 23
Csphtp1 23
HUST
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
kostikjaylonshaewe47
 
Note             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdfNote             Given Code modified as required and required met.pdf
Note             Given Code modified as required and required met.pdf
Ankitchhabra28
 
Jhtp5 20 Datastructures
Jhtp5 20 DatastructuresJhtp5 20 Datastructures
Jhtp5 20 Datastructures
martha leon
 
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdfWrite the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
fathimalinks
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
mail931892
 
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdfLabprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
Labprogram.javaLinkedList.javaimport java.util.NoSuchElementEx.pdf
freddysarabia1
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
ARCHANASTOREKOTA
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
fathimahardwareelect
 
Data Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptxData Structures and Agorithm: DS 04 Linked List.pptx
Data Structures and Agorithm: DS 04 Linked List.pptx
RashidFaridChishti
 
TutorialII_Updated____niceupdateprogram.pdf
TutorialII_Updated____niceupdateprogram.pdfTutorialII_Updated____niceupdateprogram.pdf
TutorialII_Updated____niceupdateprogram.pdf
BappyAgarwal
 

More from aravlitraders2012 (20)

Electronegativity - Electronegativity is an atom.pdf
                     Electronegativity - Electronegativity is an atom.pdf                     Electronegativity - Electronegativity is an atom.pdf
Electronegativity - Electronegativity is an atom.pdf
aravlitraders2012
 
Sequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdf
Sequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdfSequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdf
Sequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdf
aravlitraders2012
 
In the 1st Question, As the polymorphisms occur in the non-coding se.pdf
  In the 1st Question, As the polymorphisms occur in the non-coding se.pdf  In the 1st Question, As the polymorphisms occur in the non-coding se.pdf
In the 1st Question, As the polymorphisms occur in the non-coding se.pdf
aravlitraders2012
 
I noticed that temperature is not mentioned. A) .pdf
                     I noticed that temperature is not mentioned.  A) .pdf                     I noticed that temperature is not mentioned.  A) .pdf
I noticed that temperature is not mentioned. A) .pdf
aravlitraders2012
 
1. Briefly describe the structured design approach and the object-or.pdf
1. Briefly describe the structured design approach and the object-or.pdf1. Briefly describe the structured design approach and the object-or.pdf
1. Briefly describe the structured design approach and the object-or.pdf
aravlitraders2012
 
1) Protocols are needed so your computer can interact with other com.pdf
1) Protocols are needed so your computer can interact with other com.pdf1) Protocols are needed so your computer can interact with other com.pdf
1) Protocols are needed so your computer can interact with other com.pdf
aravlitraders2012
 
argon is inert gas , doesnot chemically reacts wi.pdf
                     argon is inert gas , doesnot chemically reacts wi.pdf                     argon is inert gas , doesnot chemically reacts wi.pdf
argon is inert gas , doesnot chemically reacts wi.pdf
aravlitraders2012
 
#include sstream #include linkylist.h #include iostream.pdf
#include sstream #include linkylist.h #include iostream.pdf#include sstream #include linkylist.h #include iostream.pdf
#include sstream #include linkylist.h #include iostream.pdf
aravlitraders2012
 
Function header If a program contain a function definition, that .pdf
Function header If a program contain a function definition, that .pdfFunction header If a program contain a function definition, that .pdf
Function header If a program contain a function definition, that .pdf
aravlitraders2012
 
GameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdfGameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdf
aravlitraders2012
 
With the vast increase in technology, the number of ways that the us.pdf
With the vast increase in technology, the number of ways that the us.pdfWith the vast increase in technology, the number of ways that the us.pdf
With the vast increase in technology, the number of ways that the us.pdf
aravlitraders2012
 
Let n = size of the hash table hashTable = array of size n, w.pdf
Let n = size of the hash table  hashTable = array of size n, w.pdfLet n = size of the hash table  hashTable = array of size n, w.pdf
Let n = size of the hash table hashTable = array of size n, w.pdf
aravlitraders2012
 
What is the need of the t Distribution According to the central.pdf
What is the need of the t Distribution According to the central.pdfWhat is the need of the t Distribution According to the central.pdf
What is the need of the t Distribution According to the central.pdf
aravlitraders2012
 
We can see in course of evolution of plant there was a transition fr.pdf
We can see in course of evolution of plant there was a transition fr.pdfWe can see in course of evolution of plant there was a transition fr.pdf
We can see in course of evolution of plant there was a transition fr.pdf
aravlitraders2012
 
Wave CharacterMany of the things that light does are only expla.pdf
 Wave CharacterMany of the things that light does are only expla.pdf Wave CharacterMany of the things that light does are only expla.pdf
Wave CharacterMany of the things that light does are only expla.pdf
aravlitraders2012
 
The solvent doesnt interfere with the purification processOil Bath.pdf
The solvent doesnt interfere with the purification processOil Bath.pdfThe solvent doesnt interfere with the purification processOil Bath.pdf
The solvent doesnt interfere with the purification processOil Bath.pdf
aravlitraders2012
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdf
aravlitraders2012
 
The answers can be found below as discussedPart 1)The human gen.pdf
The answers can be found below as discussedPart 1)The human gen.pdfThe answers can be found below as discussedPart 1)The human gen.pdf
The answers can be found below as discussedPart 1)The human gen.pdf
aravlitraders2012
 
Solution (3)The smallest respiratory bronchioles subdivide into t.pdf
Solution (3)The smallest respiratory bronchioles subdivide into t.pdfSolution (3)The smallest respiratory bronchioles subdivide into t.pdf
Solution (3)The smallest respiratory bronchioles subdivide into t.pdf
aravlitraders2012
 
An asset manager of a hedge fund or a mutual funds is the one who di.pdf
An asset manager of a hedge fund or a mutual funds is the one who di.pdfAn asset manager of a hedge fund or a mutual funds is the one who di.pdf
An asset manager of a hedge fund or a mutual funds is the one who di.pdf
aravlitraders2012
 
Electronegativity - Electronegativity is an atom.pdf
                     Electronegativity - Electronegativity is an atom.pdf                     Electronegativity - Electronegativity is an atom.pdf
Electronegativity - Electronegativity is an atom.pdf
aravlitraders2012
 
Sequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdf
Sequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdfSequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdf
Sequence.h#ifndef MAIN #define MAIN #include cstdlibclass .pdf
aravlitraders2012
 
In the 1st Question, As the polymorphisms occur in the non-coding se.pdf
  In the 1st Question, As the polymorphisms occur in the non-coding se.pdf  In the 1st Question, As the polymorphisms occur in the non-coding se.pdf
In the 1st Question, As the polymorphisms occur in the non-coding se.pdf
aravlitraders2012
 
I noticed that temperature is not mentioned. A) .pdf
                     I noticed that temperature is not mentioned.  A) .pdf                     I noticed that temperature is not mentioned.  A) .pdf
I noticed that temperature is not mentioned. A) .pdf
aravlitraders2012
 
1. Briefly describe the structured design approach and the object-or.pdf
1. Briefly describe the structured design approach and the object-or.pdf1. Briefly describe the structured design approach and the object-or.pdf
1. Briefly describe the structured design approach and the object-or.pdf
aravlitraders2012
 
1) Protocols are needed so your computer can interact with other com.pdf
1) Protocols are needed so your computer can interact with other com.pdf1) Protocols are needed so your computer can interact with other com.pdf
1) Protocols are needed so your computer can interact with other com.pdf
aravlitraders2012
 
argon is inert gas , doesnot chemically reacts wi.pdf
                     argon is inert gas , doesnot chemically reacts wi.pdf                     argon is inert gas , doesnot chemically reacts wi.pdf
argon is inert gas , doesnot chemically reacts wi.pdf
aravlitraders2012
 
#include sstream #include linkylist.h #include iostream.pdf
#include sstream #include linkylist.h #include iostream.pdf#include sstream #include linkylist.h #include iostream.pdf
#include sstream #include linkylist.h #include iostream.pdf
aravlitraders2012
 
Function header If a program contain a function definition, that .pdf
Function header If a program contain a function definition, that .pdfFunction header If a program contain a function definition, that .pdf
Function header If a program contain a function definition, that .pdf
aravlitraders2012
 
GameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdfGameOfLife.cs using System; using System.Collections.Generic;.pdf
GameOfLife.cs using System; using System.Collections.Generic;.pdf
aravlitraders2012
 
With the vast increase in technology, the number of ways that the us.pdf
With the vast increase in technology, the number of ways that the us.pdfWith the vast increase in technology, the number of ways that the us.pdf
With the vast increase in technology, the number of ways that the us.pdf
aravlitraders2012
 
Let n = size of the hash table hashTable = array of size n, w.pdf
Let n = size of the hash table  hashTable = array of size n, w.pdfLet n = size of the hash table  hashTable = array of size n, w.pdf
Let n = size of the hash table hashTable = array of size n, w.pdf
aravlitraders2012
 
What is the need of the t Distribution According to the central.pdf
What is the need of the t Distribution According to the central.pdfWhat is the need of the t Distribution According to the central.pdf
What is the need of the t Distribution According to the central.pdf
aravlitraders2012
 
We can see in course of evolution of plant there was a transition fr.pdf
We can see in course of evolution of plant there was a transition fr.pdfWe can see in course of evolution of plant there was a transition fr.pdf
We can see in course of evolution of plant there was a transition fr.pdf
aravlitraders2012
 
Wave CharacterMany of the things that light does are only expla.pdf
 Wave CharacterMany of the things that light does are only expla.pdf Wave CharacterMany of the things that light does are only expla.pdf
Wave CharacterMany of the things that light does are only expla.pdf
aravlitraders2012
 
The solvent doesnt interfere with the purification processOil Bath.pdf
The solvent doesnt interfere with the purification processOil Bath.pdfThe solvent doesnt interfere with the purification processOil Bath.pdf
The solvent doesnt interfere with the purification processOil Bath.pdf
aravlitraders2012
 
An object of class StatCalc can be used to compute several simp.pdf
 An object of class StatCalc can be used to compute several simp.pdf An object of class StatCalc can be used to compute several simp.pdf
An object of class StatCalc can be used to compute several simp.pdf
aravlitraders2012
 
The answers can be found below as discussedPart 1)The human gen.pdf
The answers can be found below as discussedPart 1)The human gen.pdfThe answers can be found below as discussedPart 1)The human gen.pdf
The answers can be found below as discussedPart 1)The human gen.pdf
aravlitraders2012
 
Solution (3)The smallest respiratory bronchioles subdivide into t.pdf
Solution (3)The smallest respiratory bronchioles subdivide into t.pdfSolution (3)The smallest respiratory bronchioles subdivide into t.pdf
Solution (3)The smallest respiratory bronchioles subdivide into t.pdf
aravlitraders2012
 
An asset manager of a hedge fund or a mutual funds is the one who di.pdf
An asset manager of a hedge fund or a mutual funds is the one who di.pdfAn asset manager of a hedge fund or a mutual funds is the one who di.pdf
An asset manager of a hedge fund or a mutual funds is the one who di.pdf
aravlitraders2012
 
Ad

Recently uploaded (20)

How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
APM Midlands Region April 2025 Sacha Hind Circulated.pdf
APM Midlands Region April 2025 Sacha Hind Circulated.pdfAPM Midlands Region April 2025 Sacha Hind Circulated.pdf
APM Midlands Region April 2025 Sacha Hind Circulated.pdf
Association for Project Management
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
Nguyen Thanh Tu Collection
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE  BY sweety Tamanna Mahapatra MSc PediatricAPGAR SCORE  BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
SweetytamannaMohapat
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Tax evasion, Tax planning & Tax avoidance.pptx
Tax evasion, Tax  planning &  Tax avoidance.pptxTax evasion, Tax  planning &  Tax avoidance.pptx
Tax evasion, Tax planning & Tax avoidance.pptx
manishbaidya2017
 
Grade 3 - English - Printable Worksheet (PDF Format)
Grade 3 - English - Printable Worksheet  (PDF Format)Grade 3 - English - Printable Worksheet  (PDF Format)
Grade 3 - English - Printable Worksheet (PDF Format)
Sritoma Majumder
 
Debunking the Myths behind AI - v1, Carl Dalby
Debunking the Myths behind AI -  v1, Carl DalbyDebunking the Myths behind AI -  v1, Carl Dalby
Debunking the Myths behind AI - v1, Carl Dalby
Association for Project Management
 
How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18
Celine George
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx   quiz by Ridip HazarikaTHE STG QUIZ GROUP D.pptx   quiz by Ridip Hazarika
THE STG QUIZ GROUP D.pptx quiz by Ridip Hazarika
Ridip Hazarika
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
Nguyen Thanh Tu Collection
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE  BY sweety Tamanna Mahapatra MSc PediatricAPGAR SCORE  BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
SweetytamannaMohapat
 
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
PHYSIOLOGY MCQS By DR. NASIR MUSTAFA (PHYSIOLOGY)
Dr. Nasir Mustafa
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18How to Configure Public Holidays & Mandatory Days in Odoo 18
How to Configure Public Holidays & Mandatory Days in Odoo 18
Celine George
 
Tax evasion, Tax planning & Tax avoidance.pptx
Tax evasion, Tax  planning &  Tax avoidance.pptxTax evasion, Tax  planning &  Tax avoidance.pptx
Tax evasion, Tax planning & Tax avoidance.pptx
manishbaidya2017
 
Grade 3 - English - Printable Worksheet (PDF Format)
Grade 3 - English - Printable Worksheet  (PDF Format)Grade 3 - English - Printable Worksheet  (PDF Format)
Grade 3 - English - Printable Worksheet (PDF Format)
Sritoma Majumder
 
How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18How to Create A Todo List In Todo of Odoo 18
How to Create A Todo List In Todo of Odoo 18
Celine George
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
Ad

Exception to indicate that Singly LinkedList is empty. .pdf

  • 1. /** * Exception to indicate that Singly LinkedList is empty. */ class LinkedListEmptyException extends RuntimeException { public LinkedListEmptyException() { super(); } public LinkedListEmptyException(String message) { super(message); } } /** * Node class, which holds data and contains next which points to next Node. */ class Node { public int data; // data in Node. public Node next; // points to next Node in list. /** * Constructor */ public Node(int data) { this.data = data; } /** * Display Node's data */ public void displayNode() { System.out.print(data + " "); } } /** * Singly LinkedList class */ class LinkedList { private Node first; // ref to first link on list
  • 2. /** * LinkedList constructor */ public LinkedList() { first = null; } /** * Insert New Node at first position */ public void insertFirst(int data) { Node newNode = new Node(data); // Creation of New Node. newNode.next = first; // newLink ---> old first first = newNode; // first ---> newNode } /** * Method deletes specific Node from Singly LinkedList in java. */ public Node deleteSpecificNode(int deleteKey) { // Case1: when there is no element in LinkedList if (first == null) { // means LinkedList in empty, throw exception. throw new LinkedListEmptyException( "LinkedList doesn't contain any Nodes."); } // Case2: when there is only one element in LinkedList- check whether we // have to delete that Node or not. if (first.data == deleteKey) { // means LinkedList consists of only one // element, delete that. Node tempNode = first; // save reference to first Node in tempNode- // so that we could return saved reference. first = first.next; System.out.println("Node with data=" + tempNode.data + " was found on first and has been deleted."); return tempNode; // return deleted Node. } // Case3: when there are atLeast two elements in LinkedList Node previous = null;
  • 3. Node current = first; while (current != null) { if (current.data == deleteKey) { System.out.println("Node with data=" + current.data + " has been deleted."); previous.next = current.next; // make previous node's next point // to current node's next. return current; // return deleted Node. } else { if (current.next == null) { // Means Node wasn't found. System.out.println("Node with data=" + deleteKey + " wasn't found for deletion."); return null; } previous = current; current = current.next; // move to next node. } } return null; } /** * Display Singly LinkedList */ public void displayLinkedList() { System.out.print("Displaying LinkedList [first--->last]: "); Node tempDisplay = first; // start at the beginning of linkedList while (tempDisplay != null) { // Executes until we don't find end of // list. tempDisplay.displayNode(); tempDisplay = tempDisplay.next; // move to next Node System.out.print("-->"); } System.out.println(); } } /**
  • 4. * Main class - To test LinkedList. */ public class SinglyLinkedListDeleteNodeExample { public static void main(String[] args) { LinkedList linkedList = new LinkedList(); // creation of Linked List linkedList.insertFirst(92); linkedList.insertFirst(20); linkedList.insertFirst(19); linkedList.insertFirst(29); linkedList.displayLinkedList(); // display LinkedList linkedList.deleteSpecificNode(20); linkedList.displayLinkedList(); // Again display LinkedList } } OUTPUT: Displaying LinkedList [first--->last]: 29 -->19 -->20 -->92 --> Node with data=20 has been deleted. Node with data=11 wasn't found for deletion. Displaying LinkedList [first--->last]: 29 -->19 -->92 --> Solution /** * Exception to indicate that Singly LinkedList is empty. */ class LinkedListEmptyException extends RuntimeException { public LinkedListEmptyException() { super(); } public LinkedListEmptyException(String message) { super(message); } } /** * Node class, which holds data and contains next which points to next Node. */
  • 5. class Node { public int data; // data in Node. public Node next; // points to next Node in list. /** * Constructor */ public Node(int data) { this.data = data; } /** * Display Node's data */ public void displayNode() { System.out.print(data + " "); } } /** * Singly LinkedList class */ class LinkedList { private Node first; // ref to first link on list /** * LinkedList constructor */ public LinkedList() { first = null; } /** * Insert New Node at first position */ public void insertFirst(int data) { Node newNode = new Node(data); // Creation of New Node. newNode.next = first; // newLink ---> old first first = newNode; // first ---> newNode } /**
  • 6. * Method deletes specific Node from Singly LinkedList in java. */ public Node deleteSpecificNode(int deleteKey) { // Case1: when there is no element in LinkedList if (first == null) { // means LinkedList in empty, throw exception. throw new LinkedListEmptyException( "LinkedList doesn't contain any Nodes."); } // Case2: when there is only one element in LinkedList- check whether we // have to delete that Node or not. if (first.data == deleteKey) { // means LinkedList consists of only one // element, delete that. Node tempNode = first; // save reference to first Node in tempNode- // so that we could return saved reference. first = first.next; System.out.println("Node with data=" + tempNode.data + " was found on first and has been deleted."); return tempNode; // return deleted Node. } // Case3: when there are atLeast two elements in LinkedList Node previous = null; Node current = first; while (current != null) { if (current.data == deleteKey) { System.out.println("Node with data=" + current.data + " has been deleted."); previous.next = current.next; // make previous node's next point // to current node's next. return current; // return deleted Node. } else { if (current.next == null) { // Means Node wasn't found. System.out.println("Node with data=" + deleteKey + " wasn't found for deletion."); return null; } previous = current;
  • 7. current = current.next; // move to next node. } } return null; } /** * Display Singly LinkedList */ public void displayLinkedList() { System.out.print("Displaying LinkedList [first--->last]: "); Node tempDisplay = first; // start at the beginning of linkedList while (tempDisplay != null) { // Executes until we don't find end of // list. tempDisplay.displayNode(); tempDisplay = tempDisplay.next; // move to next Node System.out.print("-->"); } System.out.println(); } } /** * Main class - To test LinkedList. */ public class SinglyLinkedListDeleteNodeExample { public static void main(String[] args) { LinkedList linkedList = new LinkedList(); // creation of Linked List linkedList.insertFirst(92); linkedList.insertFirst(20); linkedList.insertFirst(19); linkedList.insertFirst(29); linkedList.displayLinkedList(); // display LinkedList linkedList.deleteSpecificNode(20); linkedList.displayLinkedList(); // Again display LinkedList } } OUTPUT:
  • 8. Displaying LinkedList [first--->last]: 29 -->19 -->20 -->92 --> Node with data=20 has been deleted. Node with data=11 wasn't found for deletion. Displaying LinkedList [first--->last]: 29 -->19 -->92 -->