Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Editor {
- public static void main(String[] args) {
- // create new object from class Result
- Result list = new Result();
- list.initialise();
- list.print();
- }
- }
- class ListNode {
- protected char element;
- protected ListNode nextNode;
- public ListNode(char input){
- element = input;
- nextNode = null;
- }
- public ListNode(char input, ListNode n){
- element = input;
- nextNode = n;
- }
- public ListNode getNext(){
- return this.nextNode;
- }
- public char getElement(){
- return this.element;
- }
- }
- class Result {
- // declare the member field
- protected ListNode head = null;
- protected ListNode cursor = null;
- protected int num_nodes = 0;
- // declare the constructor
- public void initialise(){
- String input_type, input_p, text_input;
- int N, index =0;
- char temp, first;
- Scanner myScanner = new Scanner(System.in);
- text_input = myScanner.next();
- N = myScanner.nextInt();
- temp = text_input.charAt(index);
- index++;
- this.addFirst(temp);
- ListNode current = this.getFirstPtr();
- for(int i=1;i<text_input.length();i++){
- temp = text_input.charAt(index);
- this.addAfter(current, temp);
- current = current.nextNode;
- index++;
- }
- cursor = current;
- for(int i=0;i<N;i++){
- input_type = myScanner.next();
- if(input_type.equals("L"))
- this.doL();
- if(input_type.equals("D"))
- this.doD();
- if(input_type.equals("B"))
- this.doB();
- if(input_type.equals("P")){
- input_p = myScanner.next();
- first = input_p.charAt(0);
- this.doP(first);
- }
- }
- }
- /* doP: implement P command
- * PRE-condition:
- * POST-condition:
- */
- public void doP(char character) {
- // implementation
- if(cursor == null){
- head = new ListNode(character, head);
- cursor = head;
- num_nodes++;
- }
- else{
- this.addAfter(cursor, character);
- cursor = cursor.nextNode;
- }
- }
- /* doL: implement L command
- * PRE-condition:
- * POST-condition:
- */
- public void doL() {
- // implementation
- ListNode temp = head;
- if(cursor==head){
- cursor = null;
- }
- else if(cursor!=head && cursor!=null){
- while(temp.nextNode!= cursor){
- temp = temp.nextNode;
- }
- cursor = temp;
- }
- }
- /* doD: implement D command
- * PRE-condition:
- * POST-condition:
- */
- public void doD() {
- // implementation
- if(cursor == null){
- cursor = head;
- }else{
- if(cursor.nextNode !=null)
- cursor = cursor.nextNode;
- }
- }
- /* doB: implement B command
- * PRE-condition:
- * POST-condition:
- */
- public void doB() {
- // implementation
- ListNode temp = head;
- if(cursor!=null){
- for(int i=0;i<num_nodes;i++){
- if(cursor==head){
- head = head.nextNode;
- num_nodes--;
- cursor = null;
- }else if(temp.nextNode==cursor){
- temp.nextNode = temp.nextNode.nextNode;
- num_nodes--;
- cursor = temp;
- }
- temp = temp.nextNode;
- }
- }
- }
- // implement other necessary methods
- public void addFirst(char newChar) {
- // implementation
- head = new ListNode (newChar, head);
- num_nodes++;
- }
- public void addAfter(ListNode current, char newChar){
- if(current!=null){
- current.nextNode = new ListNode (newChar, current.nextNode);
- num_nodes++;
- }
- }
- public ListNode getFirstPtr()
- {return head;}
- public void print() throws NoSuchElementException{
- if(head==null)
- throw new NoSuchElementException("Nothing to print...");
- ListNode ln = head;
- System.out.printf("%c", ln.element);
- for(int i=1;i<num_nodes;i++){
- ln = ln.nextNode;
- System.out.printf("%c", ln.element);
- }
- //System.out.printf(".\n");
- }
- }
Add Comment
Please, Sign In to add comment