Guest User

Untitled

a guest
Oct 18th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class node {
  4. int data;
  5. node next;
  6. node prev;
  7.  
  8. static void insert(node start, int data)
  9. {
  10. node newNode = new node();
  11. newNode.data = data;
  12.  
  13. if(start==null) {
  14. newNode.next=newNode.prev=null;
  15. start = newNode;
  16. } else {
  17. node t = start;
  18. while(t.next!=null)
  19. t = t.next;
  20. newNode.prev = t;
  21. t.next = newNode;
  22. newNode.next=null;
  23. }
  24. }
  25.  
  26. static void delete(node start, int data) {
  27. node t = start;
  28. try {
  29. while(t.data!=data)
  30. t = t.next;
  31. t.prev.next = t.next;
  32. t.next.prev = t.prev;
  33. } catch (NullPointerException e) {
  34. System.out.println("Data NOT found!");
  35. }
  36. }
  37.  
  38. static void traverse(node start) {
  39. node t = start;
  40. while(t!=null) {
  41. System.out.println(t.data);
  42. t=t.next;
  43. }
  44. }
  45. }
  46.  
  47. public class Main {
  48.  
  49. public static void main(String[] args) {
  50. Scanner sc = new Scanner(System.in);
  51.  
  52. node start=null;
  53.  
  54. int ch=0;
  55. while(ch!=4)
  56. {
  57. System.out.println("n1. Insert Node");
  58. System.out.println("2. Delete Node");
  59. System.out.println("3. Print nodes");
  60. System.out.println("4. Exit!");
  61. System.out.print("Enter Your Choice: ");
  62.  
  63. ch = sc.nextInt();
  64.  
  65. switch(ch)
  66. {
  67. case 1: System.out.print("nEnter the value of the node to insert: ");
  68. int val = sc.nextInt();
  69. node.insert(start, val);
  70. System.out.println("DEBUG : Start val: "+start.data);
  71. break;
  72. case 2: System.out.print("nEnter the value of the node you want to delete: ");
  73. node.delete(start, sc.nextInt());
  74. break;
  75. case 3: node.traverse(start);
  76. break;
  77. case 4: System.out.println("nBye!!");
  78. break;
  79. default: System.out.println("nWrong value: Please try again!");
  80. }
  81. }
  82. }
  83. }
Add Comment
Please, Sign In to add comment