Guest User

Untitled

a guest
Jan 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Editor {
  4.  
  5. public static void main(String[] args) {
  6. // create new object from class Result
  7. Result list = new Result();
  8. list.initialise();
  9. list.print();
  10.  
  11. }
  12. }
  13.  
  14. class ListNode {
  15. protected char element;
  16. protected ListNode nextNode;
  17.  
  18. public ListNode(char input){
  19. element = input;
  20. nextNode = null;
  21. }
  22.  
  23. public ListNode(char input, ListNode n){
  24. element = input;
  25. nextNode = n;
  26. }
  27.  
  28. public ListNode getNext(){
  29. return this.nextNode;
  30. }
  31.  
  32. public char getElement(){
  33. return this.element;
  34. }
  35. }
  36.  
  37. class Result {
  38.  
  39. // declare the member field
  40. protected ListNode head = null;
  41. protected ListNode cursor = null;
  42. protected int num_nodes = 0;
  43.  
  44. // declare the constructor
  45. public void initialise(){
  46. String input_type, input_p, text_input;
  47. int N, index =0;
  48. char temp, first;
  49. Scanner myScanner = new Scanner(System.in);
  50. text_input = myScanner.next();
  51. N = myScanner.nextInt();
  52. temp = text_input.charAt(index);
  53. index++;
  54. this.addFirst(temp);
  55. ListNode current = this.getFirstPtr();
  56.  
  57. for(int i=1;i<text_input.length();i++){
  58. temp = text_input.charAt(index);
  59. this.addAfter(current, temp);
  60. current = current.nextNode;
  61. index++;
  62. }
  63. cursor = current;
  64.  
  65. for(int i=0;i<N;i++){
  66. input_type = myScanner.next();
  67.  
  68. if(input_type.equals("L"))
  69. this.doL();
  70. if(input_type.equals("D"))
  71. this.doD();
  72. if(input_type.equals("B"))
  73. this.doB();
  74. if(input_type.equals("P")){
  75. input_p = myScanner.next();
  76. first = input_p.charAt(0);
  77. this.doP(first);
  78. }
  79. }
  80. }
  81.  
  82. /* doP: implement P command
  83. * PRE-condition:
  84. * POST-condition:
  85. */
  86. public void doP(char character) {
  87. // implementation
  88. if(cursor == null){
  89. head = new ListNode(character, head);
  90. cursor = head;
  91. num_nodes++;
  92. }
  93. else{
  94. this.addAfter(cursor, character);
  95. cursor = cursor.nextNode;
  96. }
  97. }
  98.  
  99.  
  100. /* doL: implement L command
  101. * PRE-condition:
  102. * POST-condition:
  103. */
  104. public void doL() {
  105. // implementation
  106. ListNode temp = head;
  107.  
  108. if(cursor==head){
  109. cursor = null;
  110. }
  111. else if(cursor!=head && cursor!=null){
  112. while(temp.nextNode!= cursor){
  113. temp = temp.nextNode;
  114. }
  115. cursor = temp;
  116. }
  117. }
  118.  
  119. /* doD: implement D command
  120. * PRE-condition:
  121. * POST-condition:
  122. */
  123. public void doD() {
  124. // implementation
  125. if(cursor == null){
  126. cursor = head;
  127. }else{
  128. if(cursor.nextNode !=null)
  129. cursor = cursor.nextNode;
  130. }
  131. }
  132.  
  133. /* doB: implement B command
  134. * PRE-condition:
  135. * POST-condition:
  136. */
  137. public void doB() {
  138. // implementation
  139. ListNode temp = head;
  140. if(cursor!=null){
  141. for(int i=0;i<num_nodes;i++){
  142. if(cursor==head){
  143. head = head.nextNode;
  144. num_nodes--;
  145. cursor = null;
  146. }else if(temp.nextNode==cursor){
  147. temp.nextNode = temp.nextNode.nextNode;
  148. num_nodes--;
  149. cursor = temp;
  150. }
  151. temp = temp.nextNode;
  152. }
  153. }
  154. }
  155.  
  156. // implement other necessary methods
  157. public void addFirst(char newChar) {
  158. // implementation
  159. head = new ListNode (newChar, head);
  160. num_nodes++;
  161. }
  162.  
  163. public void addAfter(ListNode current, char newChar){
  164. if(current!=null){
  165. current.nextNode = new ListNode (newChar, current.nextNode);
  166. num_nodes++;
  167. }
  168. }
  169.  
  170. public ListNode getFirstPtr()
  171. {return head;}
  172.  
  173. public void print() throws NoSuchElementException{
  174. if(head==null)
  175. throw new NoSuchElementException("Nothing to print...");
  176.  
  177. ListNode ln = head;
  178. System.out.printf("%c", ln.element);
  179. for(int i=1;i<num_nodes;i++){
  180. ln = ln.nextNode;
  181. System.out.printf("%c", ln.element);
  182. }
  183. //System.out.printf(".\n");
  184. }
  185.  
  186. }
Add Comment
Please, Sign In to add comment