Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. class Node{
  2. int data;
  3. Node prev;
  4. Node next;
  5.  
  6. public Node(int data){
  7. this.data = data;
  8. }
  9.  
  10. public void tampil(){
  11. System.out.println("{"+data+"}");
  12. }
  13. }
  14.  
  15. public class CircularLL {
  16. Node first;
  17. Node last;
  18. int size;
  19.  
  20. public CircularLL(){
  21. first = null;
  22. last = null;
  23. }
  24.  
  25. public boolean isEmpty(){
  26. return(first==null);
  27. }
  28.  
  29. public void insertFirst(int data){
  30. Node node = new Node(data);
  31. if(isEmpty()){
  32. last = node;
  33. first = node;
  34. }else{
  35. node.next = first;
  36. first.prev = node;
  37. }
  38. first = node;
  39. first.prev = last;
  40. last.next = first;
  41. size++;
  42. }
  43.  
  44. public void insertLast(int data){
  45. Node node = new Node(data);
  46. if(isEmpty()){
  47. last = node;
  48. first = node;
  49. }else{
  50. node.prev = last;
  51. last.next = node;
  52. }
  53. last = node;
  54. first.prev = last;
  55. last.next = first;
  56. size++;
  57. }
  58.  
  59. public void displayForward(){
  60. System.out.println("List (first-->last): "+size);
  61. Node current = first;
  62. int no =1;
  63. while(no <= size){
  64. current.tampil();
  65. current = current.next;
  66. no++;
  67. }
  68. System.out.println("");
  69. }
  70.  
  71. public void displayBackward(){
  72. System.out.println("List (last-->first): "+size);
  73. Node current = last;
  74. int no =1;
  75. while(no <= size){
  76. current.tampil();
  77. current = current.prev;
  78. no++;
  79. }
  80. System.out.println("");
  81. }
  82.  
  83. public Node deleteFirst(){
  84. Node temp = first;
  85. if(first.next == null){
  86. last = null;
  87. }else{
  88. first.next.prev = last;
  89. first = first.next;
  90. last.next = first;
  91. }
  92. size--;
  93. return temp;
  94. }
  95.  
  96. public Node deleteLast(){
  97. Node temp = last;
  98. if(first.next == null){
  99. last = null;
  100. }else{
  101. last.prev.next = first;
  102. last = last.prev;
  103. first.prev = last;
  104. }
  105. size--;
  106. return temp;
  107. }
  108.  
  109. public Node deleteKey(int key){
  110. Node current = first;
  111. while(current.data != key){
  112. current = current.next;
  113. }
  114. if(current==first){
  115. current = deleteFirst();
  116. }else if(current==last){
  117. current = deleteLast();
  118. }else{
  119. current.next.prev = current.prev;
  120. current.prev.next = current.next;
  121. }
  122. size--;
  123. return current;
  124. }
  125.  
  126. public void display(){
  127. Node current = last.prev;
  128.  
  129. current.tampil();
  130. System.out.println("");
  131. }
  132.  
  133. public boolean insertAfter(int key, int data){
  134. Node current = first;
  135. while(current.data != key){
  136. current = current.next;
  137. }
  138. Node node = new Node(data);
  139.  
  140. if(current==last){
  141. insertLast(data);
  142. }else{
  143. current.next.prev = node;
  144. node.next = current.next;
  145. node.prev = current;
  146. current.next = node;
  147. size++;
  148. }
  149. return true;
  150. }
  151.  
  152. public static void main(String[] args){
  153. CircularLL list = new CircularLL();
  154. list.insertFirst(1);
  155. list.insertFirst(2);
  156. list.insertFirst(3);
  157. list.insertFirst(4);
  158. list.insertLast(0);
  159. list.insertLast(5);
  160. list.displayForward();
  161. list.deleteLast();
  162. list.displayForward();
  163. list.deleteKey(55);
  164. list.insertAfter(5,10);
  165. list.displayForward();
  166. }
  167.  
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement