Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.79 KB | None | 0 0
  1. import java.util.*;
  2. class Node{
  3. int info;
  4. Node next;
  5. Node prev;
  6. }
  7. public class DLinkedList
  8. {
  9.  
  10.  
  11.  
  12.  
  13. static Node create(Node start,Node end){
  14. Node p;
  15. Scanner sc=new Scanner(System.in);
  16. System.out.println(" Enter the node info:");
  17. start.info=sc.nextInt();
  18.  
  19. start.prev=start.next=null;
  20. System.out.println("Do u want to add new node Yes-1/No-0 \n");
  21. int option=sc.nextInt();
  22. while(option != 0 )
  23. {
  24. p=new Node();
  25. System.out.println("Enter the node info");
  26. p.info=sc.nextInt();
  27. p.prev=p.next=null;
  28. p.prev=end;
  29. end.next=p;
  30. end=p;
  31. System.out.println("Do u want to add new node Yes-1/No-0 \n");
  32. option=sc.nextInt();
  33. }
  34.  
  35. return end;
  36.  
  37.  
  38.  
  39. }
  40.  
  41.  
  42.  
  43.  
  44. static void display(Node start,Node end)
  45. {
  46. Node p=start;
  47. while(p!=null)
  48. {
  49. System.out.println(p.info);
  50. p=p.next;
  51. }
  52. }
  53.  
  54.  
  55. static Node InsertBeg(Node start, Node end)
  56. {
  57. Scanner sc=new Scanner(System.in);
  58. Node p=new Node();
  59. System.out.println("Enter the node info");
  60. p.info=sc.nextInt();
  61.  
  62. p.prev=null;
  63. p.next=start;
  64. start.prev=p;
  65. start=p;
  66. return start;
  67. }
  68.  
  69.  
  70. static Node InsertEnd(Node start, Node end)
  71. {
  72. Scanner sc=new Scanner(System.in);
  73. Node p=new Node();
  74. System.out.println("Enter the node info");
  75. p.info=sc.nextInt();
  76.  
  77. p.next=null;
  78. p.prev=end;
  79. end.next=p;
  80. end=p;
  81. return end;
  82. }
  83. static Node InsertAny(Node start, Node end){
  84. Scanner sc=new Scanner(System.in);
  85. System.out.println("Enter the index/location where to insert:");
  86. int loc=sc.nextInt();
  87. int count=1;
  88. Node p=start;
  89. while(( p!=null)&&(count<(loc-1))){
  90. p= p.next;count++;
  91. }
  92. if( p==null) System.out.println("Too high index");
  93. else{
  94. Node curr=new Node();
  95. System.out.println("Enter the node info");
  96. curr.info=sc.nextInt();
  97.  
  98. curr.next=curr.prev=null;
  99. curr.next= p.next;
  100. p.next.prev=curr;
  101. curr.prev= p;
  102. p.next=curr;
  103. }
  104. return end;
  105. }
  106.  
  107. static Node DeleteBeg(Node start,Node end)
  108. {
  109. start.next.prev=null;
  110. start=start.next;
  111. return start;
  112. }
  113.  
  114. static Node DeleteEnd(Node s,Node end)
  115. {
  116. e.prev.next=null;
  117. end =end.prev;
  118. return end;
  119. }
  120.  
  121. static Node DeleteAny(Node start,Node end)
  122. {
  123. Scanner sc=new Scanner(System.in);
  124. Node p=start;
  125. System.out.println("Enter the node info to delete");
  126. int item=sc.nextInt();
  127. while(p!=null && p.info!=item)
  128. p=p.next;
  129. if(p==null) System.out.println("Node not found");
  130. else{
  131. p.prev.next=p.next;
  132. p.next.prev=p.prev;
  133. }
  134. return start;
  135. }
  136.  
  137.  
  138. static void Search(Node start)
  139. {
  140. Scanner sc=new Scanner(System.in);
  141. System.out.println("Enter the node info to search");
  142. int roll=sc.nextInt();
  143. while(start!=null && start.info!=roll){
  144. if(start.info==roll) break;
  145. else start=start.next;
  146. }
  147. if(start==null) System.out.println("node not present");
  148. else System.out.println("node is present");
  149. }
  150.  
  151.  
  152. public static void main(String[] args)
  153. {
  154. Scanner sc=new Scanner(System.in);
  155. Node start=new Node();
  156. Node end=start;
  157. while(true){
  158. System.out.println("*********************MENU(Doubly Linked List)***************************");
  159. System.out.println("0: Exit");
  160. System.out.println("1: Creation");
  161. System.out.println("2: Display");
  162. System.out.println("3: Insert at the begenning");
  163. System.out.println("4: Insert at the end");
  164. System.out.println("5: Insert at any location");
  165. System.out.println("6: Delete the first node");
  166. System.out.println("7: Delete the last node");
  167. System.out.println("8: Delete any node");
  168. System.out.println("9: Search a node");
  169. System.out.println("Enter your choice");
  170. int choice=sc.nextInt();
  171. switch(choice){
  172. case 0: System.exit(0);
  173. case 1:
  174. end=create(start,end);
  175. break;
  176. case 2:
  177. display(start,end);
  178. break;
  179. case 3:
  180. start=InsertBeg(start,end);
  181. break;
  182. case 4:
  183. end=InsertEnd(start,end);
  184. break;
  185. case 5:
  186. end=InsertAny(start,end);
  187. break;
  188. case 6:
  189. start=DeleteBeg(start,end);
  190. break;
  191. case 7:
  192. end=DeleteEnd(start,end);
  193. break;
  194. case 8:
  195. start=DeleteAny(start,end);
  196. break;
  197. case 9:
  198. Search(start);
  199. break;
  200. default:
  201. System.out.println("Wrong choice");
  202. }
  203. }
  204. }
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement