Professional Documents
Culture Documents
Editor Texto
Editor Texto
Editor Texto
Scanner;
//---------------------------------------Linear Data
Structure----------------------------------------
class Node<DataType> {
DataType data;
Node<DataType> next;
Node<DataType> previous;
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;
}
}
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);
int length(){
return this.count;
}
boolean isEmpty(){
return this.count == 0;
}
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;
return toReturn;
}
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);
}
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();
}