Untitled

 avatar
unknown
plain_text
4 years ago
6.9 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;
	static final boolean ADD = true;
	static final boolean REMOVE = false;

	public class Node {
		protected char data;
		protected int cursorPosition;
		protected Node next;
		protected boolean actionAdd;

		public Node() {
			super();
			this.data = ' ';
			this.cursorPosition = 0;
			this.next = null;
			this.actionAdd = true;
		}

		public Node(char data, int cursorPosition, Node next, boolean actionAdd) {
			super();
			this.data = data;
			this.cursorPosition = cursorPosition;
			this.next = next;
			this.actionAdd = actionAdd;
		}

		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 boolean getActionAdd() {
			return actionAdd;
		}

		public void setActionAdd(boolean actionAdd) {
			this.actionAdd = actionAdd;
		}

	}

	public class LinkedList {
		protected Node head;
		protected Node tail;
		protected int size;

		public LinkedList() {
			super();
			head = null;
			tail = null;
			size = 0;
		}

		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, true);
			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, true);
			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, true);
			if (size == 0 || cursorPosition == 0) { // Them dau
				InsertAtHead(data, cursorPosition);
				return;
			}
			if (size == cursorPosition) { // 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--;
			currentCursor--;
			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 isEmptyStack() {
			return size == 0;
		}

		public void Reset() {
			this.top = null;
			this.size = 0;
		}

		public void Push(char data, int cursorPosition, boolean action) {
			Node temp = new Node(data, cursorPosition, null, action);
			if (isEmptyStack()) {
				top = temp;
			} else {
				temp.setNext(top);
				top = temp;
			}
			size++;
		}

		public Node Pop() {
			if (isEmptyStack()) {
				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 (!redo.isEmptyStack()) {
			redo.Reset();
		}
		if (ch == '\b') {
			if (currentCursor > 0) {
				Node temp = list.DeleteAtPosition(currentCursor);
				undo.Push(temp.getData(), currentCursor, REMOVE);
			}
		} else {
			list.InsertAtPostion(ch, currentCursor);
			undo.Push(ch, currentCursor, ADD);
		}
	}

	public void moveCursor(int pos) {
		currentCursor = pos;
	}

	public void undo() {
		if (this.undo.isEmptyStack() == true) {
			return;
		}
		Node temp = undo.Pop();
		currentCursor = temp.getCursorPosition();
		if (temp.getActionAdd() == false) {
			list.InsertAtPostion(temp.getData(), currentCursor);
			redo.Push(temp.getData(), currentCursor, REMOVE);
		} else {
			list.DeleteAtPosition(currentCursor);
			redo.Push(temp.getData(), currentCursor, ADD);
		}
	}

	public void redo() {
		if (this.redo.isEmptyStack() == true) {
			return;
		}
		Node temp = redo.Pop();
		currentCursor = temp.getCursorPosition();
		if (temp.getActionAdd() == false) {
			list.DeleteAtPosition(currentCursor);
			undo.Push(temp.getData(), currentCursor, REMOVE);
		} else {
			list.InsertAtPostion(temp.getData(), currentCursor);
			undo.Push(temp.getData(), currentCursor, ADD);
		}
	}

	public char[] show() {
		String s = list.show();
		return s.toCharArray();
	}

}
Editor is loading...