Implement a Doubly Linked List with a sentinel node. The class should support fi
ID: 3742624 • Letter: I
Question
Implement a Doubly Linked List with a sentinel node. The class should support find , insertion() and remove() operations from head and tail of the list.
Explanation / Answer
package com.config; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class DLL { public static void main(String args[]) throws NumberFormatException, IOException{ int choice = 0; Node temp = null; Node head = null; boolean flage = true; int nodeCounter = 0; BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); do{ Node node = new Node(); System.out.println("Enter Node data"); node.data = Integer.parseInt(read.readLine()); if(flage){ flage = false; head = node; } if(temp!=null){ temp.next = node; node.prev = temp; } temp = node; nodeCounter++; System.out.println("Press 0 to quite for more node entry."); choice = Integer.parseInt(read.readLine()); }while(choice != 0); temp.next = head; head.prev = temp; System.out.println("You have created "+nodeCounter+" nodes in doubly linked list"); System.out.println("Retriving and Manipulation Operation perform : "); Node node = head; do{ System.out.println("Press 1 for get all nodes from doubly linked list."); System.out.println("Press 2 for get all nodes in reverse from doubly linked list."); System.out.println("Press 3 for get length of doubly linked list."); System.out.println("Press 4 for remove nth node from doubly linked list."); System.out.println("Press 5 for find node in doubly linked list."); System.out.println("Press 0 for quite."); choice = Integer.parseInt(read.readLine()); switch (choice) { case 1: Node.getAllNodes(node); break; case 2: Node.getAllNodesReverse(node); break; case 3 : System.out.println("Length : "+Node.getDoublyLinkedListLength(node)); break; case 4 : System.out.println("Enter Position to remove from DLL"); choice = Integer.parseInt(read.readLine()); node = Node.removeNthNode(node, choice); break; case 5 :System.out.println("Enter Node data to find in DLL"); choice = Integer.parseInt(read.readLine()); choice = Node.findNode(node, choice); if(choice == 0){ System.out.println("Node Not Found in DLL"); choice = 1; }else System.out.println("Node Position : "+choice); break; default: break; } }while(choice != 0); } } class Node{ int data = 0; Node next = null; Node prev = null; public static void getAllNodes(Node head){ int nodeCounter = 0; if(head!=null){ Node tail = head.prev; while(head.next != tail.next ){ nodeCounter++; System.out.println(nodeCounter+". Node : "+head.data); head = head.next; } nodeCounter++; System.out.println(nodeCounter+". Node : "+head.data); } } public static int getDoublyLinkedListLength(Node head){ int nodeCounter = 0; if(head!=null){ Node tail = head.prev; while(head.next != tail.next ){ nodeCounter++; head = head.next; } nodeCounter++; } return nodeCounter; } public static int findNode(Node head,int data){ int nodeCounter = 0; boolean flage = false; if(head!=null){ Node tail = head.prev; while(head.next != tail.next ){ nodeCounter++; if(head.data == data){ flage = true; break; } head = head.next; } } return flage ? nodeCounter : 0; } public static void getAllNodesReverse(Node head){ if(head!= null){ int nodeCounter = 0; Node tail = head.prev; while(tail.prev!= head.prev){ nodeCounter++; System.out.println(nodeCounter+". Node : "+tail.data); tail = tail.prev; } nodeCounter++; System.out.println(nodeCounter+". Node : "+tail.data); } } public static Node removeNthNode(Node head, int removePosition){ int length = getDoublyLinkedListLength(head); if(head!=null){ int counter = 1; if(length >2){ if(length+1 > removePosition && removePosition > 0){ while(counter != removePosition){ counter++; head = head.next; } head.prev.next = head.next; head.next.prev = head.prev; }else{ System.out.println("Given Position not exist"); } }else{ System.out.println("At least two nodes must be in doubly linked list."); } } if(removePosition==1 || removePosition==length) return head.next; else return head; } } package com.config; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class DLL { public static void main(String args[]) throws NumberFormatException, IOException{ int choice = 0; Node temp = null; Node head = null; boolean flage = true; int nodeCounter = 0; BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); do{ Node node = new Node(); System.out.println("Enter Node data"); node.data = Integer.parseInt(read.readLine()); if(flage){ flage = false; head = node; } if(temp!=null){ temp.next = node; node.prev = temp; } temp = node; nodeCounter++; System.out.println("Press 0 to quite for more node entry."); choice = Integer.parseInt(read.readLine()); }while(choice != 0); temp.next = head; head.prev = temp; System.out.println("You have created "+nodeCounter+" nodes in doubly linked list"); System.out.println("Retriving and Manipulation Operation perform : "); Node node = head; do{ System.out.println("Press 1 for get all nodes from doubly linked list."); System.out.println("Press 2 for get all nodes in reverse from doubly linked list."); System.out.println("Press 3 for get length of doubly linked list."); System.out.println("Press 4 for remove nth node from doubly linked list."); System.out.println("Press 5 for find node in doubly linked list."); System.out.println("Press 0 for quite."); choice = Integer.parseInt(read.readLine()); switch (choice) { case 1: Node.getAllNodes(node); break; case 2: Node.getAllNodesReverse(node); break; case 3 : System.out.println("Length : "+Node.getDoublyLinkedListLength(node)); break; case 4 : System.out.println("Enter Position to remove from DLL"); choice = Integer.parseInt(read.readLine()); node = Node.removeNthNode(node, choice); break; case 5 :System.out.println("Enter Node data to find in DLL"); choice = Integer.parseInt(read.readLine()); choice = Node.findNode(node, choice); if(choice == 0){ System.out.println("Node Not Found in DLL"); choice = 1; }else System.out.println("Node Position : "+choice); break; default: break; } }while(choice != 0); } } class Node{ int data = 0; Node next = null; Node prev = null; public static void getAllNodes(Node head){ int nodeCounter = 0; if(head!=null){ Node tail = head.prev; while(head.next != tail.next ){ nodeCounter++; System.out.println(nodeCounter+". Node : "+head.data); head = head.next; } nodeCounter++; System.out.println(nodeCounter+". Node : "+head.data); } } public static int getDoublyLinkedListLength(Node head){ int nodeCounter = 0; if(head!=null){ Node tail = head.prev; while(head.next != tail.next ){ nodeCounter++; head = head.next; } nodeCounter++; } return nodeCounter; } public static int findNode(Node head,int data){ int nodeCounter = 0; boolean flage = false; if(head!=null){ Node tail = head.prev; while(head.next != tail.next ){ nodeCounter++; if(head.data == data){ flage = true; break; } head = head.next; } } return flage ? nodeCounter : 0; } public static void getAllNodesReverse(Node head){ if(head!= null){ int nodeCounter = 0; Node tail = head.prev; while(tail.prev!= head.prev){ nodeCounter++; System.out.println(nodeCounter+". Node : "+tail.data); tail = tail.prev; } nodeCounter++; System.out.println(nodeCounter+". Node : "+tail.data); } } public static Node removeNthNode(Node head, int removePosition){ int length = getDoublyLinkedListLength(head); if(head!=null){ int counter = 1; if(length >2){ if(length+1 > removePosition && removePosition > 0){ while(counter != removePosition){ counter++; head = head.next; } head.prev.next = head.next; head.next.prev = head.prev; }else{ System.out.println("Given Position not exist"); } }else{ System.out.println("At least two nodes must be in doubly linked list."); } } if(removePosition==1 || removePosition==length) return head.next; else return head; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.