SlideShare a Scribd company logo
in Java (ignore the last line thats hidden) Create a doubly linked list whose nodes contain Strings.
Your list should include the following methods: Insert a node in the list in alphabetical order
Find a node that matches a String Traverse the list forwards and print Traverse the list backwards
and print Delete a node from the list Delete/destroy the list In addition to creating the class(es)
for your linked list, you'll need to create a main method that thoroughly tests each function you
build to show that it works. You should do these things in
Solution
/ Java program to create , insert , traverse and delete a node from doubly linked list
class LinkedList {
static Node head = null;
class Node {
int data;
Node next, prev;
Node(int d) {
data = d;
next = prev = null;
}
}
/* Let us create the doubly linked list 10<->8<->4<->2 */
list.push(head, 2);
list.push(head, 4);
list.push(head, 8);
list.push(head, 10);
System.out.println("Original Linked list ");
list.printList(head);
/*Function to delete a node in a Doubly Linked List.
head_ref --> pointer to head node pointer.
del --> pointer to node to be deleted. */
void deleteNode(Node head_ref, Node del) {
/* base case */
if (head == null || del == null) {
return;
}
/* If node to be deleted is head node */
if (head == del) {
head = del.next;
}
/* Change next only if node to be deleted is NOT the last node */
if (del.next != null) {
del.next.prev = del.prev;
}
/* Change prev only if node to be deleted is NOT the first node */
if (del.prev != null) {
del.prev.next = del.next;
}
/* Finally, free the memory occupied by del*/
return;
}
/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginning of the Doubly Linked List */
void push(Node head_ref, int new_data) {
/* allocate node */
Node new_node = new Node(new_data);
/* since we are adding at the begining,
prev is always NULL */
new_node.prev = null;
/* link the old list off the new node */
new_node.next = (head);
/* change prev of head node to new node */
if ((head) != null) {
(head).prev = new_node;
}
/* move the head to point to the new node */
(head) = new_node;
}
/*Function to print nodes in a given doubly linked list
This function is same as printList() of singly linked lsit */
void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
/* delete nodes from the doubly linked list */
list.deleteNode(head, head); /*delete first node*/
list.deleteNode(head, head.next); /*delete middle node*/
list.deleteNode(head, head.next); /*delete last node*/
System.out.println("");
/* Modified linked list will be NULL<-8->NULL */
System.out.println("Modified Linked List");
list.printList(head);
}
}

More Related Content

PDF
Data Structures in C++I am really new to C++, so links are really .pdf
PPTX
Linked list
DOCX
Below is a depiction of a doubly-linked list implementation of the bag.docx
PDF
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
PDF
Use C++ Write a function to merge two doubly linked lists. The input.pdf
PDF
How to do insertion sort on a singly linked list with no header usin.pdf
PDF
Program to insert in a sorted list #includestdio.h#include.pdf
PDF
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Data Structures in C++I am really new to C++, so links are really .pdf
Linked list
Below is a depiction of a doubly-linked list implementation of the bag.docx
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdf
How to do insertion sort on a singly linked list with no header usin.pdf
Program to insert in a sorted list #includestdio.h#include.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf

Similar to in Java (ignore the last line thats hidden) Create a doubly linked l.pdf (20)

PDF
Lec-4_Linked-List (1).pdf
PDF
LinkedList1LinkedList1LinkedList1111.pdf
PDF
This assignment and the next (#5) involve design and development of a.pdf
PDF
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
PDF
Describe an algorithm for concatenating two singly linked lists L and.pdf
DOCX
Linked list.docx
PPTX
C Exam Help
PDF
This is problem is same problem which i submitted on 22017, I just.pdf
PDF
I have been tasked to write a code for a Singly Linked list that inc.pdf
PPTX
linkedlistforslideshare-210123143943.pptx
PPTX
C Homework Help
PDF
C++Write a method Node Nodereverse() which reverses a list..pdf
PPTX
Linked list
PDF
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
PDF
Write a program to implement below operations with both singly and d.pdf
PDF
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
DOCX
C++ please put everthing after you answer it- thanks Complete the stub.docx
PDF
1#include stdio.h#include stdlib.h#include assert.h .pdf
PDF
Sorted number list implementation with linked listsStep 1 Inspec.pdf
DOC
Linked List
Lec-4_Linked-List (1).pdf
LinkedList1LinkedList1LinkedList1111.pdf
This assignment and the next (#5) involve design and development of a.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdf
Linked list.docx
C Exam Help
This is problem is same problem which i submitted on 22017, I just.pdf
I have been tasked to write a code for a Singly Linked list that inc.pdf
linkedlistforslideshare-210123143943.pptx
C Homework Help
C++Write a method Node Nodereverse() which reverses a list..pdf
Linked list
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
Write a program to implement below operations with both singly and d.pdf
in C++ , Design a linked list class named IntegerList to hold a seri.pdf
C++ please put everthing after you answer it- thanks Complete the stub.docx
1#include stdio.h#include stdlib.h#include assert.h .pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
Linked List
Ad

More from sauravmanwanicp (20)

PDF
Are hydrophobic interactions weak or strongSolutionHydrophobi.pdf
PDF
What is another word for account based forecast What is anothe.pdf
PDF
What function can be used to return multiple numeric data such as in.pdf
PDF
Answer the following questions in reference to Drosophila melanogast.pdf
PDF
There are five feasible solutions to a three-objective optimization p.pdf
PDF
An air-filled parallel-plate capacitor has capacitance Co. If two ide.pdf
PDF
The incandescent lightbulb was actually invented by Humphry Davy in 1.pdf
PDF
The fossil record provides little information about ancient mosses. D.pdf
PDF
The DNA sequence below is a shortened version of the sequence on the .pdf
PDF
the conducting tissue found in vascular plants that functions in tra.pdf
PDF
Show that f(x)=57^x+1 and f^-1(x)=7x-75 are inverses Choose the ap.pdf
PDF
Read the short article Horizontal Gene Transfer in E Coli and answer.pdf
PDF
Please select the best answer and click submit. The owners of fou.pdf
PDF
One function of the spliceosome is to (Select all that apply.)A. .pdf
PDF
Match the following organelles with their key functions in the cell .pdf
PDF
please choose the correct answerIn the ABO blood-type phenotype, w.pdf
PDF
mo Press Submit until all questions are colored green and you have do.pdf
PDF
matching chose the stage of the light reactions which the statement.pdf
PDF
Let E be the set of all even integers, and let O be the set of all o.pdf
PDF
JavaIm not sure how to implement this code can someone help me .pdf
Are hydrophobic interactions weak or strongSolutionHydrophobi.pdf
What is another word for account based forecast What is anothe.pdf
What function can be used to return multiple numeric data such as in.pdf
Answer the following questions in reference to Drosophila melanogast.pdf
There are five feasible solutions to a three-objective optimization p.pdf
An air-filled parallel-plate capacitor has capacitance Co. If two ide.pdf
The incandescent lightbulb was actually invented by Humphry Davy in 1.pdf
The fossil record provides little information about ancient mosses. D.pdf
The DNA sequence below is a shortened version of the sequence on the .pdf
the conducting tissue found in vascular plants that functions in tra.pdf
Show that f(x)=57^x+1 and f^-1(x)=7x-75 are inverses Choose the ap.pdf
Read the short article Horizontal Gene Transfer in E Coli and answer.pdf
Please select the best answer and click submit. The owners of fou.pdf
One function of the spliceosome is to (Select all that apply.)A. .pdf
Match the following organelles with their key functions in the cell .pdf
please choose the correct answerIn the ABO blood-type phenotype, w.pdf
mo Press Submit until all questions are colored green and you have do.pdf
matching chose the stage of the light reactions which the statement.pdf
Let E be the set of all even integers, and let O be the set of all o.pdf
JavaIm not sure how to implement this code can someone help me .pdf
Ad

Recently uploaded (20)

PPTX
Institutional Correction lecture only . . .
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Pre independence Education in Inndia.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
Basic Mud Logging Guide for educational purpose
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Sports Quiz easy sports quiz sports quiz
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Lesson notes of climatology university.
PPTX
Cell Structure & Organelles in detailed.
Institutional Correction lecture only . . .
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pre independence Education in Inndia.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
01-Introduction-to-Information-Management.pdf
Cell Types and Its function , kingdom of life
Basic Mud Logging Guide for educational purpose
Anesthesia in Laparoscopic Surgery in India
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharma ospi slides which help in ospi learning
Module 4: Burden of Disease Tutorial Slides S2 2025
102 student loan defaulters named and shamed – Is someone you know on the list?
O7-L3 Supply Chain Operations - ICLT Program
Sports Quiz easy sports quiz sports quiz
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Lesson notes of climatology university.
Cell Structure & Organelles in detailed.

in Java (ignore the last line thats hidden) Create a doubly linked l.pdf

  • 1. in Java (ignore the last line thats hidden) Create a doubly linked list whose nodes contain Strings. Your list should include the following methods: Insert a node in the list in alphabetical order Find a node that matches a String Traverse the list forwards and print Traverse the list backwards and print Delete a node from the list Delete/destroy the list In addition to creating the class(es) for your linked list, you'll need to create a main method that thoroughly tests each function you build to show that it works. You should do these things in Solution / Java program to create , insert , traverse and delete a node from doubly linked list class LinkedList { static Node head = null; class Node { int data; Node next, prev; Node(int d) { data = d; next = prev = null; } } /* Let us create the doubly linked list 10<->8<->4<->2 */ list.push(head, 2); list.push(head, 4); list.push(head, 8); list.push(head, 10); System.out.println("Original Linked list "); list.printList(head); /*Function to delete a node in a Doubly Linked List. head_ref --> pointer to head node pointer. del --> pointer to node to be deleted. */ void deleteNode(Node head_ref, Node del) { /* base case */ if (head == null || del == null) { return; }
  • 2. /* If node to be deleted is head node */ if (head == del) { head = del.next; } /* Change next only if node to be deleted is NOT the last node */ if (del.next != null) { del.next.prev = del.prev; } /* Change prev only if node to be deleted is NOT the first node */ if (del.prev != null) { del.prev.next = del.next; } /* Finally, free the memory occupied by del*/ return; } /* UTILITY FUNCTIONS */ /* Function to insert a node at the beginning of the Doubly Linked List */ void push(Node head_ref, int new_data) { /* allocate node */ Node new_node = new Node(new_data); /* since we are adding at the begining, prev is always NULL */ new_node.prev = null; /* link the old list off the new node */ new_node.next = (head); /* change prev of head node to new node */ if ((head) != null) { (head).prev = new_node; } /* move the head to point to the new node */ (head) = new_node; } /*Function to print nodes in a given doubly linked list This function is same as printList() of singly linked lsit */ void printList(Node node) { while (node != null) {
  • 3. System.out.print(node.data + " "); node = node.next; } } public static void main(String[] args) { LinkedList list = new LinkedList(); /* delete nodes from the doubly linked list */ list.deleteNode(head, head); /*delete first node*/ list.deleteNode(head, head.next); /*delete middle node*/ list.deleteNode(head, head.next); /*delete last node*/ System.out.println(""); /* Modified linked list will be NULL<-8->NULL */ System.out.println("Modified Linked List"); list.printList(head); } }