SlideShare a Scribd company logo
1 of 4
Download to read offline
You can list anything, it doesn't matter. I just want to see code for this problem. Thank you
Solution
Include header files .......
Source code:
public class SinglyLinkedListImpl {
private Node head;
private Node tail;
public void add(T element){
Node nd = new Node();
nd.setValue(element);
System.out.println("Adding: "+element);
if(head == null){
head = nd;
tail = nd;
} else {
tail.setNextRef(nd);
}
tail = nd;
}
}
public void addAfter(T element, T after){
Node tmp = head;
Node refNode = null;
System.out.println("Traversing to all nodes..");
while(true){
if(tmp == null){
break;
}
if(tmp.compareTo(after) == 0){
refNode = tmp;
break;
}
tmp = tmp.getNextRef();
}
if(refNode != null){
Node nd = new Node();
nd.setValue(element);
nd.setNextRef(tmp.getNextRef());
if(tmp == tail){
tail = nd;
}
tmp.setNextRef(nd);
} else {
System.out.println("Unable to find the given element...");
}
}
public void deleteFront(){
if(head == null){
System.out.println("Underflow...");
}
Node tmp = head;
head = tmp.getNextRef();
if(head == null){
tail = null;
}
System.out.println("Deleted: "+tmp.getValue());
}
public void deleteAfter(T after){
Node tmp = head;
Node refNode = null;
System.out.println("Traversing to all nodes..");
while(true){
if(tmp == null){
break;
}
if(tmp.compareTo(after) == 0){
refNode = tmp;
break;
}
tmp = tmp.getNextRef();
}
if(refNode != null){
tmp = refNode.getNextRef();
refNode.setNextRef(tmp.getNextRef());
if(refNode.getNextRef() == null){
tail = refNode;
}
System.out.println("Deleted: "+tmp.getValue());
} else {
System.out.println("Unable to find the given element...");
}
}
public void traverse(){
Node tmp = head;
while(true){
if(tmp == null){
break;
}
System.out.println(tmp.getValue());
tmp = tmp.getNextRef();
}
}
public static void main(String a[]){
SinglyLinkedListImpl sl = new SinglyLinkedListImpl();
sl.add(3);
sl.add(32);
sl.add(54);
sl.add(89);
sl.addAfter(76, 54);
sl.deleteFront();
sl.deleteAfter(76);
sl.traverse();
}
}
class Node implements Comparable {
private T value;
private Node nextRef;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public Node getNextRef() {
return nextRef;
}
public void setNextRef(Node ref) {
this.nextRef = ref;
}
public int compareTo(T arg) {
if(arg == this.value){
return 0;
} else {
return 1;
}
}
}
This program implement the concept of single linked list.

More Related Content

Similar to You can list anything, it doesnt matter. I just want to see code f.pdf

In this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfIn this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdf
charanjit1717
 
Solve using Java programming language- ----------------------------.pdf
Solve using Java programming language-   ----------------------------.pdfSolve using Java programming language-   ----------------------------.pdf
Solve using Java programming language- ----------------------------.pdf
aksahnan
 
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
 
Write the Java source code necessary to build a solution for the pro.pdf
Write the Java source code necessary to build a solution for the pro.pdfWrite the Java source code necessary to build a solution for the pro.pdf
Write the Java source code necessary to build a solution for the pro.pdf
fckindswear
 
public class SLLT { protected SLLNodeT head, tail; pub.pdf
public class SLLT {     protected SLLNodeT head, tail;     pub.pdfpublic class SLLT {     protected SLLNodeT head, tail;     pub.pdf
public class SLLT { protected SLLNodeT head, tail; pub.pdf
clarityvision
 
Please need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdfPlease need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdf
nitinarora01
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
ritu1806
 
#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf
ankitmobileshop235
 
C# code pleaseWrite a program that creates a link list object of 1.pdf
C# code pleaseWrite a program that creates a link list object of 1.pdfC# code pleaseWrite a program that creates a link list object of 1.pdf
C# code pleaseWrite a program that creates a link list object of 1.pdf
duttakajal70
 
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
 
I want help in the following C++ programming task. Please do coding .pdf
I want help in the following C++ programming task. Please do coding .pdfI want help in the following C++ programming task. Please do coding .pdf
I want help in the following C++ programming task. Please do coding .pdf
bermanbeancolungak45
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
tienlivick
 
#include iostream #include cstddefusing namespace std;temp.pdf
#include iostream #include cstddefusing namespace std;temp.pdf#include iostream #include cstddefusing namespace std;temp.pdf
#include iostream #include cstddefusing namespace std;temp.pdf
karan8801
 

Similar to You can list anything, it doesnt matter. I just want to see code f.pdf (20)

In this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdfIn this lab, you will be given a simple code for a min Heap, and you.pdf
In this lab, you will be given a simple code for a min Heap, and you.pdf
 
137 Lab-2.2.pdf
137 Lab-2.2.pdf137 Lab-2.2.pdf
137 Lab-2.2.pdf
 
Solve using Java programming language- ----------------------------.pdf
Solve using Java programming language-   ----------------------------.pdfSolve using Java programming language-   ----------------------------.pdf
Solve using Java programming language- ----------------------------.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
 
Write the Java source code necessary to build a solution for the pro.pdf
Write the Java source code necessary to build a solution for the pro.pdfWrite the Java source code necessary to build a solution for the pro.pdf
Write the Java source code necessary to build a solution for the pro.pdf
 
public class SLLT { protected SLLNodeT head, tail; pub.pdf
public class SLLT {     protected SLLNodeT head, tail;     pub.pdfpublic class SLLT {     protected SLLNodeT head, tail;     pub.pdf
public class SLLT { protected SLLNodeT head, tail; pub.pdf
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Please need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdfPlease need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdf
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
Lab-2.2 717822E504.pdf
Lab-2.2 717822E504.pdfLab-2.2 717822E504.pdf
Lab-2.2 717822E504.pdf
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
Lab-2.4 101.pdf
Lab-2.4 101.pdfLab-2.4 101.pdf
Lab-2.4 101.pdf
 
Binary Tree
Binary  TreeBinary  Tree
Binary Tree
 
#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf
 
C# code pleaseWrite a program that creates a link list object of 1.pdf
C# code pleaseWrite a program that creates a link list object of 1.pdfC# code pleaseWrite a program that creates a link list object of 1.pdf
C# code pleaseWrite a program that creates a link list object of 1.pdf
 
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
 
I want help in the following C++ programming task. Please do coding .pdf
I want help in the following C++ programming task. Please do coding .pdfI want help in the following C++ programming task. Please do coding .pdf
I want help in the following C++ programming task. Please do coding .pdf
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 
#include iostream #include cstddefusing namespace std;temp.pdf
#include iostream #include cstddefusing namespace std;temp.pdf#include iostream #include cstddefusing namespace std;temp.pdf
#include iostream #include cstddefusing namespace std;temp.pdf
 

More from fashionbigchennai

This is my code but not complete, please complete my.pdf
This is my code but not complete, please complete my.pdfThis is my code but not complete, please complete my.pdf
This is my code but not complete, please complete my.pdf
fashionbigchennai
 
The terminology employed in factory automation is often confusing in.pdf
The terminology employed in factory automation is often confusing in.pdfThe terminology employed in factory automation is often confusing in.pdf
The terminology employed in factory automation is often confusing in.pdf
fashionbigchennai
 
Step 2 Formulate a HypothesisFormulate a hypothesis by making an .pdf
Step 2 Formulate a HypothesisFormulate a hypothesis by making an .pdfStep 2 Formulate a HypothesisFormulate a hypothesis by making an .pdf
Step 2 Formulate a HypothesisFormulate a hypothesis by making an .pdf
fashionbigchennai
 
Reaction StatisticsBackgroundWhen collecting experimental data f.pdf
Reaction StatisticsBackgroundWhen collecting experimental data f.pdfReaction StatisticsBackgroundWhen collecting experimental data f.pdf
Reaction StatisticsBackgroundWhen collecting experimental data f.pdf
fashionbigchennai
 
Please explain in detaile the European debt crisisSolutionIn 2.pdf
Please explain in detaile the European debt crisisSolutionIn 2.pdfPlease explain in detaile the European debt crisisSolutionIn 2.pdf
Please explain in detaile the European debt crisisSolutionIn 2.pdf
fashionbigchennai
 
Part 1 Many phone companies are replacing the wire in their phone s.pdf
Part 1 Many phone companies are replacing the wire in their phone s.pdfPart 1 Many phone companies are replacing the wire in their phone s.pdf
Part 1 Many phone companies are replacing the wire in their phone s.pdf
fashionbigchennai
 

More from fashionbigchennai (20)

Write a Java program that performs these operationsPrompt the use.pdf
Write a Java program that performs these operationsPrompt the use.pdfWrite a Java program that performs these operationsPrompt the use.pdf
Write a Java program that performs these operationsPrompt the use.pdf
 
Write a complete PUP enabled website using WebMatrix. At the beginnin.pdf
Write a complete PUP enabled website using WebMatrix. At the beginnin.pdfWrite a complete PUP enabled website using WebMatrix. At the beginnin.pdf
Write a complete PUP enabled website using WebMatrix. At the beginnin.pdf
 
Which of the following is a flaw, loophole, oversight, or error that.pdf
Which of the following is a flaw, loophole, oversight, or error that.pdfWhich of the following is a flaw, loophole, oversight, or error that.pdf
Which of the following is a flaw, loophole, oversight, or error that.pdf
 
what trade agreements were initiated by the United States and Canada.pdf
what trade agreements were initiated by the United States and Canada.pdfwhat trade agreements were initiated by the United States and Canada.pdf
what trade agreements were initiated by the United States and Canada.pdf
 
Use the series and parallel commands in Matlab to find the transfer .pdf
Use the series and parallel commands in Matlab to find the transfer .pdfUse the series and parallel commands in Matlab to find the transfer .pdf
Use the series and parallel commands in Matlab to find the transfer .pdf
 
True or False Even if there is a strong correlation between the ind.pdf
True or False Even if there is a strong correlation between the ind.pdfTrue or False Even if there is a strong correlation between the ind.pdf
True or False Even if there is a strong correlation between the ind.pdf
 
This is my code but not complete, please complete my.pdf
This is my code but not complete, please complete my.pdfThis is my code but not complete, please complete my.pdf
This is my code but not complete, please complete my.pdf
 
The terminology employed in factory automation is often confusing in.pdf
The terminology employed in factory automation is often confusing in.pdfThe terminology employed in factory automation is often confusing in.pdf
The terminology employed in factory automation is often confusing in.pdf
 
The probability that A hits a target is 14 and the probability that.pdf
The probability that A hits a target is 14 and the probability that.pdfThe probability that A hits a target is 14 and the probability that.pdf
The probability that A hits a target is 14 and the probability that.pdf
 
Step 2 Formulate a HypothesisFormulate a hypothesis by making an .pdf
Step 2 Formulate a HypothesisFormulate a hypothesis by making an .pdfStep 2 Formulate a HypothesisFormulate a hypothesis by making an .pdf
Step 2 Formulate a HypothesisFormulate a hypothesis by making an .pdf
 
Since the directions of v and a are entirely independent of each othe.pdf
Since the directions of v and a are entirely independent of each othe.pdfSince the directions of v and a are entirely independent of each othe.pdf
Since the directions of v and a are entirely independent of each othe.pdf
 
Reaction StatisticsBackgroundWhen collecting experimental data f.pdf
Reaction StatisticsBackgroundWhen collecting experimental data f.pdfReaction StatisticsBackgroundWhen collecting experimental data f.pdf
Reaction StatisticsBackgroundWhen collecting experimental data f.pdf
 
Please explain in detaile the European debt crisisSolutionIn 2.pdf
Please explain in detaile the European debt crisisSolutionIn 2.pdfPlease explain in detaile the European debt crisisSolutionIn 2.pdf
Please explain in detaile the European debt crisisSolutionIn 2.pdf
 
Part 1 Many phone companies are replacing the wire in their phone s.pdf
Part 1 Many phone companies are replacing the wire in their phone s.pdfPart 1 Many phone companies are replacing the wire in their phone s.pdf
Part 1 Many phone companies are replacing the wire in their phone s.pdf
 
Mary is an excellent waitress. In the restaurant, she can easily hand.pdf
Mary is an excellent waitress. In the restaurant, she can easily hand.pdfMary is an excellent waitress. In the restaurant, she can easily hand.pdf
Mary is an excellent waitress. In the restaurant, she can easily hand.pdf
 
List three characteristics that influence the rate of simple diffusi.pdf
List three characteristics that influence the rate of simple diffusi.pdfList three characteristics that influence the rate of simple diffusi.pdf
List three characteristics that influence the rate of simple diffusi.pdf
 
Its all worked out, but Im still very confused by the steps .pdf
Its all worked out, but Im still very confused by the steps .pdfIts all worked out, but Im still very confused by the steps .pdf
Its all worked out, but Im still very confused by the steps .pdf
 
Is the world overpopulated What are the arguments for the notion th.pdf
Is the world overpopulated What are the arguments for the notion th.pdfIs the world overpopulated What are the arguments for the notion th.pdf
Is the world overpopulated What are the arguments for the notion th.pdf
 
In the ellipse shown below, each point marked with a dot is called a(.pdf
In the ellipse shown below, each point marked with a dot is called a(.pdfIn the ellipse shown below, each point marked with a dot is called a(.pdf
In the ellipse shown below, each point marked with a dot is called a(.pdf
 
Identify the following Level of caudal end of subarachnoid space E.pdf
Identify the following  Level of caudal end of subarachnoid space  E.pdfIdentify the following  Level of caudal end of subarachnoid space  E.pdf
Identify the following Level of caudal end of subarachnoid space E.pdf
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 
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)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
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Ă...
 
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.
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
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
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).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
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
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)
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 

You can list anything, it doesnt matter. I just want to see code f.pdf

  • 1. You can list anything, it doesn't matter. I just want to see code for this problem. Thank you Solution Include header files ....... Source code: public class SinglyLinkedListImpl { private Node head; private Node tail; public void add(T element){ Node nd = new Node(); nd.setValue(element); System.out.println("Adding: "+element); if(head == null){ head = nd; tail = nd; } else { tail.setNextRef(nd); } tail = nd; } } public void addAfter(T element, T after){ Node tmp = head; Node refNode = null; System.out.println("Traversing to all nodes.."); while(true){ if(tmp == null){ break; } if(tmp.compareTo(after) == 0){ refNode = tmp;
  • 2. break; } tmp = tmp.getNextRef(); } if(refNode != null){ Node nd = new Node(); nd.setValue(element); nd.setNextRef(tmp.getNextRef()); if(tmp == tail){ tail = nd; } tmp.setNextRef(nd); } else { System.out.println("Unable to find the given element..."); } } public void deleteFront(){ if(head == null){ System.out.println("Underflow..."); } Node tmp = head; head = tmp.getNextRef(); if(head == null){ tail = null; } System.out.println("Deleted: "+tmp.getValue()); } public void deleteAfter(T after){ Node tmp = head; Node refNode = null; System.out.println("Traversing to all nodes.."); while(true){ if(tmp == null){ break;
  • 3. } if(tmp.compareTo(after) == 0){ refNode = tmp; break; } tmp = tmp.getNextRef(); } if(refNode != null){ tmp = refNode.getNextRef(); refNode.setNextRef(tmp.getNextRef()); if(refNode.getNextRef() == null){ tail = refNode; } System.out.println("Deleted: "+tmp.getValue()); } else { System.out.println("Unable to find the given element..."); } } public void traverse(){ Node tmp = head; while(true){ if(tmp == null){ break; } System.out.println(tmp.getValue()); tmp = tmp.getNextRef(); } } public static void main(String a[]){ SinglyLinkedListImpl sl = new SinglyLinkedListImpl(); sl.add(3); sl.add(32); sl.add(54); sl.add(89); sl.addAfter(76, 54);
  • 4. sl.deleteFront(); sl.deleteAfter(76); sl.traverse(); } } class Node implements Comparable { private T value; private Node nextRef; public T getValue() { return value; } public void setValue(T value) { this.value = value; } public Node getNextRef() { return nextRef; } public void setNextRef(Node ref) { this.nextRef = ref; } public int compareTo(T arg) { if(arg == this.value){ return 0; } else { return 1; } } } This program implement the concept of single linked list.