Untitled

 avatar
unknown
plain_text
4 years ago
6.8 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;

	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;
		protected int cursorPosition;

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

		public LinkedList(Node head, Node tail, int size, int cursorPosition) {
			super();
			this.head = head;
			this.tail = tail;
			this.size = size;
			this.cursorPosition = cursorPosition;
		}

		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 int getCursorPosition() {
			return cursorPosition;
		}

		public void setCursorPosition(int cursorPosition) {
			this.cursorPosition = cursorPosition;
		}

		public void reset() {
			head = null;
			tail = null;
			size = 0;
			cursorPosition = 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;
			}
			this.cursorPosition++;
			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;
				this.size++;
				this.cursorPosition++;
			}
		}

		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
				tail.setNext(temp);
				tail = temp;
				size++;
				this.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);
			this.size++;
			this.cursorPosition++;
		}

		public void DeleteAtHead() {
			// TODO Auto-generated method stub
			head = head.getNext();
			this.size--;
			this.cursorPosition--;
			if (size == 0) {
				reset();
			}
		}

		public void DeleteAtPosition(int cursorPosition) {
			if (size == 0 || cursorPosition == 0) {
				return;
			}
			if (cursorPosition == 1) { // Xoa dau
				DeleteAtHead();
				return;
			}
			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;
				this.size--;
				this.cursorPosition--;
				return;
			}
			// Neu xoa o giua
			int index = 0;
			while (index < cursorPosition - 1) {
				ptr = p;
				p = p.getNext();
				index++;
			}
			ptr.setNext(p.getNext());
			this.size--;
			this.cursorPosition--;
		}

		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 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();
		for (int i = 0; i < str.length; i++) {
			list.InsertAtTail(str[i], list.cursorPosition);
			undo.Push(str[i], list.cursorPosition);
		}
	}

	public void write(char ch) {
		if (ch == '\b') {
			if (list.cursorPosition != 0) {
				list.DeleteAtPosition(list.cursorPosition);
				undo.Push(ch, list.cursorPosition);
			}
		} else {
			list.InsertAtPostion(ch, list.cursorPosition);
			undo.Push(ch, list.cursorPosition);
		}

	}

	public void moveCursor(int pos) {
		list.cursorPosition = pos;
	}

	public void undo() {
		if (this.undo.isEmpty() == true) {
			return;
		}
		Node temp = undo.Pop();
		redo.Push(temp.getData(), temp.getCursorPosition()); // Day vao redo
		list.setCursorPosition(temp.getCursorPosition());
		if (temp.getData() == '\b') {
			list.InsertAtPostion(temp.getData(), list.getCursorPosition());
		} else {

			list.DeleteAtPosition(list.getCursorPosition());
		}

	}

	public void redo() {
		if (this.redo.isEmpty() == true || list.cursorPosition == 0) {
			return;
		}
		Node temp = redo.Pop();
		undo.Push(temp.getData(), temp.getCursorPosition());
		list.setCursorPosition(temp.getCursorPosition());
		if (temp.getData() == '\b') {
			list.InsertAtPostion(temp.getData(), list.getCursorPosition());
		} else {
			list.DeleteAtPosition(list.getCursorPosition());
		}
	}

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

}
Editor is loading...