Esercizio 1
This commit is contained in:
@@ -25,7 +25,7 @@ public class LinkedList {
|
||||
|
||||
do {
|
||||
System.out.println("Scegliere un'opzione:");
|
||||
System.out.println("1. ");
|
||||
System.out.println("1. Esercizio 1");
|
||||
System.out.println("2. ");
|
||||
System.out.println("3. ");
|
||||
System.out.println("0. Esci");
|
||||
@@ -39,7 +39,7 @@ public class LinkedList {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
|
||||
esercizio1();
|
||||
pausa();
|
||||
break;
|
||||
case 2:
|
||||
@@ -55,8 +55,7 @@ public class LinkedList {
|
||||
pausa();
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (InputMismatchException _) {
|
||||
} catch (InputMismatchException _) {
|
||||
System.out.println(ERRORE_GENERICO);
|
||||
pausa();
|
||||
}
|
||||
@@ -67,4 +66,42 @@ public class LinkedList {
|
||||
System.out.println("Premere un tasto per continuare. . .");
|
||||
sc.nextLine();
|
||||
}
|
||||
|
||||
private static void esercizio1() {
|
||||
boolean error;
|
||||
char[] caratteriDigitati;
|
||||
Lista<Character> listaCaratteriInseriti;
|
||||
do {
|
||||
error = false;
|
||||
System.out.print("Digitare i caratteri: ");
|
||||
caratteriDigitati = sc.nextLine().toLowerCase().toCharArray();
|
||||
listaCaratteriInseriti = new Lista<>();
|
||||
|
||||
for (int i = 0; i < caratteriDigitati.length && !error; i++) {
|
||||
if (caratteriDigitati[i] < 'a' || caratteriDigitati[i] > 'z') {
|
||||
error = true;
|
||||
}
|
||||
}
|
||||
if (error) {
|
||||
System.out.println("Errore:inserire solo lettere ASCII.");
|
||||
pausa();
|
||||
}
|
||||
} while (error);
|
||||
|
||||
for (char carattere : caratteriDigitati) {
|
||||
listaCaratteriInseriti.addLast(carattere);
|
||||
}
|
||||
System.out.println("Lista caratteri inseriti:");
|
||||
System.out.println(listaCaratteriInseriti.toString());
|
||||
listaCaratteriInseriti.sort();
|
||||
|
||||
for (int i = 0; i < listaCaratteriInseriti.getSize() && ('a' + i) < listaCaratteriInseriti.getElementAt(listaCaratteriInseriti.getSize() - 1); i++) {
|
||||
if (listaCaratteriInseriti.getElementAt(i) != 'a' + i) {
|
||||
listaCaratteriInseriti.addElementAt((char) ('a' + i), i);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("Lista caratteri aggiunti:");
|
||||
System.out.println(listaCaratteriInseriti.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package linkedlist;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
public class Lista<E> {
|
||||
public class Lista<E extends Comparable<E>> {
|
||||
|
||||
private Nodo<E> testa;
|
||||
private int size = 0;
|
||||
@@ -45,13 +45,13 @@ public class Lista<E> {
|
||||
public void addElementAt(E info, int posizione) throws IndexOutOfBoundsException {
|
||||
if (posizione == 0 || this.testa == null) {
|
||||
addFirst(info);
|
||||
} else if (posizione == this.size - 1) {
|
||||
} else if (posizione == this.size) {
|
||||
addLast(info);
|
||||
} else if (posizione < 0 || posizione >= this.size) {
|
||||
} else if (posizione < 0 || posizione > this.size) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
} else {
|
||||
Nodo<E> corrente = this.testa;
|
||||
for (int i = 0; i < posizione; i++) {
|
||||
for (int i = 0; i < posizione - 1; i++) {
|
||||
corrente = corrente.getNext();
|
||||
}
|
||||
Nodo<E> nuovoNodo = new Nodo<>(info);
|
||||
@@ -90,9 +90,9 @@ public class Lista<E> {
|
||||
public E removeElementAt(int posizione) throws IndexOutOfBoundsException {
|
||||
if (posizione == 0 || this.testa == null) {
|
||||
return removeFirst();
|
||||
} else if (posizione == this.size - 1) {
|
||||
} else if (posizione == this.size) {
|
||||
return removeLast();
|
||||
} else if (posizione < 0 || posizione >= this.size) {
|
||||
} else if (posizione < 0 || posizione > this.size) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
} else {
|
||||
Nodo<E> corrente = this.testa;
|
||||
@@ -131,7 +131,7 @@ public class Lista<E> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
public String toString() {
|
||||
if (this.testa != null) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Nodo<E> coda = this.testa;
|
||||
@@ -141,11 +141,62 @@ public class Lista<E> {
|
||||
sb.append(" ]-->");
|
||||
coda = coda.getNext();
|
||||
}
|
||||
sb.append("[ ");
|
||||
sb.append(coda.getInfo().toString());
|
||||
sb.append(" ]-->");
|
||||
sb.append("NULL");
|
||||
return sb.toString();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return "[ ]-->NULL";
|
||||
}
|
||||
}
|
||||
|
||||
public void sort() {
|
||||
int itr = 0;
|
||||
boolean swapped;
|
||||
|
||||
// Iterating over the whole linked list
|
||||
while (itr < this.getSize()) {
|
||||
Nodo<E> traverseNode = this.testa;
|
||||
Nodo<E> prevNode = this.testa;
|
||||
swapped = false;
|
||||
|
||||
while (traverseNode.getNext() != null) {
|
||||
|
||||
// Temporary pointer to store the next
|
||||
// pointer of traverseNode
|
||||
Nodo<E> ptr = traverseNode.getNext();
|
||||
if (traverseNode.getInfo().compareTo(ptr.getInfo()) > 0) {
|
||||
swapped = true;
|
||||
if (traverseNode == this.testa) {
|
||||
|
||||
// Performing swap operations and
|
||||
// updating the head of the linked
|
||||
// list
|
||||
traverseNode.setNext(ptr.getNext());
|
||||
ptr.setNext(traverseNode);
|
||||
prevNode = ptr;
|
||||
this.testa = prevNode;
|
||||
} else {
|
||||
|
||||
// Performing swap operation
|
||||
traverseNode.setNext(ptr.getNext());
|
||||
ptr.setNext(traverseNode);
|
||||
prevNode.setNext(ptr);
|
||||
prevNode = ptr;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
prevNode = traverseNode;
|
||||
traverseNode = traverseNode.getNext();
|
||||
}
|
||||
|
||||
// If no swap occurred, break the loop
|
||||
if (!swapped) {
|
||||
break;
|
||||
}
|
||||
|
||||
itr++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package linkedlist;
|
||||
|
||||
public class Nodo<E> {
|
||||
public class Nodo<E extends Comparable<E>> {
|
||||
private Nodo<E> next;
|
||||
private E info;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user