Editor Texto

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

import java.util.

Scanner;

//---------------------------------------Linear Data
Structure----------------------------------------
class Node<DataType> {
DataType data;
Node<DataType> next;
Node<DataType> previous;

public Node(DataType data){


this.data = data;
this.next = null;
this.previous = null;
}

public DataType getData() {


return data;
}
}
class List<DataType>{
protected Node<DataType> first;
protected Node<DataType> last;
protected int count;

public List(){
this.makeEmpty();
}
public List(DataType[] array){
this.makeEmpty();
for (DataType i : array)
insertEnd(i);
}
public List(List<DataType> list){
this.makeEmpty();
Node<DataType> aux = list.first;
while(aux!=null) {
this.insertEnd(aux.data);
aux=aux.next;
}
}

void insertEnd(DataType toInsert){


Node<DataType> newNodo = new Node<>(toInsert);
newNodo.previous = this.last;
newNodo.next = null;

if(this.count == 0){
this.first = newNodo;
}else{
this.last.next = newNodo;
}

this.last = newNodo;
this.count++;
}
void deleteEnd(){
if(this.first == null || this.last == null){
System.err.println("Error, no puedo eliminar de una lista vacía");
return;
}
if(this.first == this.last){
makeEmpty();
return;
}
this.last = this.last.previous;
this.last.next = null;
this.count--;
}
void insertBegin(DataType toInsert){ //O(1)
Node<DataType> newNode = new Node<>(toInsert);
newNode.next = this.first;
newNode.previous = null;

if(this.count == 0){
this.last = newNode;
}else{
this.first.previous = newNode;
}

this.first = newNode;
this.count++;
}
void deleteBegin(){ //O(1)
if(this.first == null || this.last == null){
System.err.println("Error, no puedo eliminar de una lista vacía");
return;
}
if(this.first == this.last){
makeEmpty();
return;
}
this.first = this.first.next;
this.first.previous = null;
this.count--;

}
void insert(int k, DataType data){ //O(n)
if(this.checkIndex(k)){
System.out.println("No es posible hacer la inserción en el indice " + k
+ ".");
return;
}
if(k == 0){
this.insertBegin(data);
return;
}
if(k == this.count-1){
this.insertEnd(data);
return;
}
Node<DataType> newNode = new Node<>(data), aux1 = this.readNode(k-1), aux2
= this.readNode(k+1);

newNode.next = (aux1.next==null)? null : aux1.next;


newNode.previous = (aux2.previous==null)? null : aux2.previous;
aux1.next = newNode;
aux2.previous = newNode;
this.count++;
}
void delete(int k){
if(this.first == null || this.last == null){
System.err.println("Error, no puedo eliminar de una lista vacía.");
return;
}
if(this.checkIndex(k)){
System.out.println("No existe elemento(s) en la posición " + k + ",
exede límite de la lista.");
return;
}
if(k == 0){
this.deleteBegin();
return;
}
if(k == this.count-1){
this.deleteEnd();
return;
}
Node<DataType> aux1 = this.readNode(k-1), aux2 = this.readNode(k+1);
aux1.next = aux2;
aux2.previous = aux1;
this.count--;
}
void set(int k, DataType data){
if(this.checkIndex(k)){
System.out.println("No es posible hacer la inserción en el indice " + k
+ ".");
return;
}
Node<DataType> aux = readNode(k);
aux.data=data;
}
void makeEmpty(){
this.count = 0;
this.first = null;
this.last = null;
}

int length(){
return this.count;
}

boolean isEmpty(){
return this.count == 0;
}

String matchIndex(DataType data){


Integer[] matches = find(data);
String[] stringArray = new String[matches.length];
String matchesToString = "";
for (int i = 0; i<matches.length; i++)
stringArray[i]=String.valueOf(matches[i]);

for(int i = 0; i<stringArray.length; i++){


matchesToString+=stringArray[i];
if(i<stringArray.length-1)
matchesToString+=", ";
}
return "["+matchesToString+"]";
}

DataType read(int k){


if(k < 0 || k >= this.count){
System.out.print("No es posible realizar la búsqueda (Excede límite de
la lista) - ");
return null;
}
if(k==0)
return this.first.data;

if(k==this.count-1)
return this.last.data;

Node<DataType> aux;
if(k<this.count/2){
aux = this.first;
for(int i = 0; i < k; i++)
aux = aux.next;
}else{
aux = this.last;
for(int i = this.count-1; i > k; i--) {
aux = aux.previous;
}
}
return aux.data;

}
DataType firstData(){
return this.first.data;
}
DataType lastData(){
return this.last.data;
}
DataType[] toArray(){
DataType[] toReturn = (DataType[]) new Object[count];
Node<DataType> aux = this.first;

for(int i=0; i<this.count; i++){


toReturn[i] = aux.data;
aux=aux.next;
}

return toReturn;
}

private Integer[] find(DataType data){ //9


if(this.count == 0){
System.err.println("Error, la lista está vacía");
return new Integer[]{-1};
}
Node<DataType> aux = this.first;
int index = 0;
int numMatches = 0;
Integer[] matches = new Integer[this.count];

while (aux != null){


if (data.equals(aux.data)){
matches[numMatches] = index;
numMatches++;
}
index++;
aux = aux.next;
}

matches[numMatches]=null;

if(numMatches==0){
System.out.print("Error, elemento no encontrado ");
matches[0]=-1;
return new Integer[]{-1};
}else{
int finalMatchesLength = 0;
for (int i = 0; i < this.count; i++){
if(matches[i]==null){
finalMatchesLength=i;
break;
}
}
Integer[] finalMatches = new Integer[finalMatchesLength];
for (int i = 0; i < finalMatches.length; i++)
finalMatches[i] = matches[i];
return finalMatches;
}

}
private boolean checkIndex(int k){
return k < 0 || k > this.count-1;
}
private Node<DataType> readNode(int k){ //[0, n-1]
if(k < 0 || k >= this.count){
System.out.println("No es posible realizar la búsqueda");
return null;
}
Node<DataType> aux = this.first;
for(int i = 0; i < k; i++)
aux = aux.next;
return aux;
}

@Override
public String toString(){
String toReturn="[";
Node<DataType> aux = this.first;
while (aux != null){
toReturn+=aux.data;
if(aux.next!=null)
toReturn+=", ";
aux = aux.next;
}
return toReturn+"]";
}
}
class Stack<DataType> extends List<DataType>{

Stack(){
super();
}
Stack(DataType[] array){
super(array);
}

void push(DataType data){


insertBegin(data);
}

DataType pop(){
if(this.first == null || this.last == null){
System.err.println("Error, no es posible desapilar de una lista
vacía");
System.exit(0);
}
DataType aux = this.first.data;
deleteBegin();
return aux;
}

DataType peek(){
return this.first.data;
}
}

//---------------------------------------------
Solution-----------------------------------------------
class TextEditor{
private List<Character> text;
private Stack<List> textSaves;

TextEditor(){
makeEmpty();
}

//Ejecuta los métodos implementados dado un arreglo establecido


void orderExecution(String[] ops){
for(int i=0; i<ops.length; i++) {
switch (ops[i].split(" ")[0]){
case "1":
this.append(ops[i].split(" ")[1]);
break;
case "2":
this.delete(Integer.parseInt(ops[i].split(" ")[1]));
break;
case "3":
this.print(Integer.parseInt(ops[i].split(" ")[1]));

//Bloque empleado para evitar el último salto de linea


producido por el método (causaba conflicto con el compilador de UNCODE)
if(i!=ops.length-1){
for(int k=i+1; k<ops.length; k++){
if(ops[k].split(" ")[0].equals("3")){
System.out.println();
break;
}
}
}
break;
case "4":
this.undo();
break;
}
}
}
/*
Métodos solicitados por el ejercicio:
- Método 1, añade una cadena de carácteres al texto
- Método 2, elimina los ultimos k-ésimos carácters del texto
- Método 3, imprime el k-ésimo carácter del texto
- Método 4, deshace la última acción producida por los métodos 1 o 2
Se encuentran a continuación, ordenados por orden ascendente:
*/
private void append(String w){
Character[] array = w.chars().mapToObj(c -> (char)
c).toArray(Character[]::new);
this.textSaves.push(new List(this.text));
for(Character element : array)
text.insertEnd(element);
} //1
private void delete(int k){
if(!this.text.isEmpty()){
this.textSaves.push(new List(this.text));
for (int i = 0; i < k; i++)
text.deleteEnd();
}
} //2
private void print(int k){
if(!this.text.isEmpty())
System.out.print(text.read(k-1));
} //3
private void undo(){
if(!this.textSaves.isEmpty())
this.text=new List<>(this.textSaves.pop());
} //4

private void makeEmpty(){


text=new List<>();
textSaves= new Stack<>();
}
}

public class Main {


public static void main(String[] args){
Scanner scan = new Scanner(System.in);

String[] orderExecution = new String[Integer.parseInt(scan.nextLine())];


for(int i=0; i<orderExecution.length; i++)
orderExecution[i]=scan.nextLine();
scan.close();

TextEditor myText = new TextEditor();


myText.orderExecution(orderExecution);
}
}

You might also like