Untitled
unknown
plain_text
4 years ago
6.5 kB
7
Indexable
package Dreampad; public class UserSolution { // static int MAX_STR_SIZE = 50000; // // static char[] str = new char[MAX_STR_SIZE]; LinkedList list; Stack undo; Stack redo; int currentCursor; boolean newAction; public class Node { protected char data; protected int cursorPosition; protected Node next; public Node() { super(); this.data = ' '; this.cursorPosition = 0; this.next = null; } public Node(char data, int cursorPosition, Node next) { super(); this.data = data; this.cursorPosition = cursorPosition; this.next = next; } public char getData() { return data; } public void setData(char data) { this.data = data; } public int getCursorPosition() { return cursorPosition; } public void setCursorPosition(int cursorPosition) { this.cursorPosition = cursorPosition; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } } public class LinkedList { protected Node head; protected Node tail; protected int size; public LinkedList() { super(); head = null; tail = null; size = 0; } public LinkedList(Node head, Node tail, int size) { super(); this.head = head; this.tail = tail; this.size = size; } public Node getHead() { return head; } public void setHead(Node head) { this.head = head; } public Node getTail() { return tail; } public void setTail(Node tail) { this.tail = tail; } public int getSize() { return size; } public void setSize(int size) { this.size = size; } public void reset() { head = null; tail = null; size = 0; } public void InsertAtHead(char data, int cursorPosition) { Node temp = new Node(data, cursorPosition, null); if (size == 0) { head = temp; tail = temp; } else { temp.setNext(head); head = temp; } currentCursor++; this.size++; } public void InsertAtTail(char data, int cursorPosition) { if (size == 0) { InsertAtHead(data, cursorPosition); return; } Node temp = new Node(data, cursorPosition, null); if (size == cursorPosition) { tail.setNext(temp); tail = temp; currentCursor++; this.size++; } } public void InsertAtPostion(char data, int cursorPosition) { Node temp = new Node(data, cursorPosition, null); if (size == 0 || cursorPosition == 0) { // Them dau InsertAtHead(data, cursorPosition); return; } if (cursorPosition == size) { // Them cuoi InsertAtTail(data, cursorPosition); return; } // Them giua Node p = head; Node ptr = head; int index = 0; while (index < cursorPosition) { ptr = p; p = p.getNext(); index++; } ptr.setNext(temp); temp.setNext(p); currentCursor++; this.size++; } public Node DeleteAtHead() { // TODO Auto-generated method stub Node temp = head; head = head.getNext(); temp.setNext(null); this.size--; if (size == 0) { reset(); } return temp; } public Node DeleteAtPosition(int cursorPosition) { if (size == 0 || cursorPosition == 0) { return null; } if (cursorPosition == 1) { // Xoa dau return DeleteAtHead(); } Node p = head; Node ptr = head; if (cursorPosition == size) { // Neu xoa o cuoi while (p.getNext() != null) { ptr = p; p = p.getNext(); } ptr.setNext(p.getNext()); this.tail = ptr; currentCursor--; this.size--; return p; } // Neu xoa o giua int index = 0; while (index < cursorPosition - 1) { ptr = p; p = p.getNext(); index++; } ptr.setNext(p.getNext()); p.setNext(null); currentCursor--; this.size--; return p; } public String show() { String s = ""; if (this.size == 0) { } else { Node temp = head; while (temp != null) { s = s + temp.getData(); temp = temp.getNext(); } } return s; } } public class Stack { protected Node top; protected int size; public Stack() { super(); top = null; size = 0; } public Stack(Node top, int size) { super(); this.top = top; this.size = size; } public int getSize() { return size; } public boolean isEmpty() { return size == 0; } public void Reset() { this.top = null; this.size = 0; } public void Push(char data, int cursorPosition) { Node temp = new Node(data, cursorPosition, null); if (isEmpty()) { top = temp; } else { temp.setNext(top); top = temp; } size++; } public Node Pop() { if (isEmpty()) { return null; } else { Node temp = top; top = temp.getNext(); temp.setNext(null); size--; return temp; } } } public void init(char[] str) { list = new LinkedList(); undo = new Stack(); redo = new Stack(); currentCursor = 0; for (int i = 0; i < str.length; i++) { list.InsertAtTail(str[i], i); } newAction = true; } public void write(char ch) { if (ch == '\b') { if (currentCursor > 0) { Node temp = list.DeleteAtPosition(currentCursor); undo.Push(temp.getData(), currentCursor); undo.Push(ch, currentCursor); } } else { list.InsertAtPostion(ch, currentCursor); undo.Push(ch, currentCursor); } if (newAction == false) { if (!undo.isEmpty()) { undo.Reset(); } if (!redo.isEmpty()) { redo.Reset(); } newAction = true; } } public void moveCursor(int pos) { currentCursor = pos; } public void undo() { if (this.undo.isEmpty() == true) { return; } newAction = false; Node temp = undo.Pop(); Node temp1; if (temp.getData() == '\b') { temp1 = undo.Pop(); list.InsertAtPostion(temp1.getData(), temp1.getCursorPosition()); redo.Push(temp1.getData(), currentCursor); redo.Push(temp.getData(), currentCursor); } else { temp1 = list.DeleteAtPosition(temp.getCursorPosition()); redo.Push(temp.getData(), currentCursor); } } public void redo() { if (this.redo.isEmpty() == true) { return; } Node temp = redo.Pop(); Node temp1; if (temp.getData() == '\b') { temp1 = list.DeleteAtPosition(temp.getCursorPosition()); undo.Push(temp1.getData(), currentCursor); undo.Push(temp.getData(), currentCursor); } else { list.InsertAtPostion(temp.getData(), temp.getCursorPosition()); undo.Push(temp.getData(), currentCursor); } } public char[] show() { String s = list.show(); return s.toCharArray(); } }
Editor is loading...