From c63e632460c84867a44f43f0eba220dc3b576aef Mon Sep 17 00:00:00 2001 From: La Programmatrice Verde Date: Tue, 24 Mar 2026 17:50:49 +0100 Subject: [PATCH] Esercizio 1 --- src/linkedlist/LinkedList.java | 45 ++++++++++++++++++++-- src/linkedlist/Lista.java | 69 +++++++++++++++++++++++++++++----- src/linkedlist/Nodo.java | 2 +- 3 files changed, 102 insertions(+), 14 deletions(-) diff --git a/src/linkedlist/LinkedList.java b/src/linkedlist/LinkedList.java index ef2ba57..20a030c 100644 --- a/src/linkedlist/LinkedList.java +++ b/src/linkedlist/LinkedList.java @@ -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 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()); + } } diff --git a/src/linkedlist/Lista.java b/src/linkedlist/Lista.java index b1dacea..4e2fe90 100644 --- a/src/linkedlist/Lista.java +++ b/src/linkedlist/Lista.java @@ -2,7 +2,7 @@ package linkedlist; import java.util.NoSuchElementException; -public class Lista { +public class Lista> { private Nodo testa; private int size = 0; @@ -45,13 +45,13 @@ public class Lista { 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 corrente = this.testa; - for (int i = 0; i < posizione; i++) { + for (int i = 0; i < posizione - 1; i++) { corrente = corrente.getNext(); } Nodo nuovoNodo = new Nodo<>(info); @@ -90,9 +90,9 @@ public class Lista { 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 corrente = this.testa; @@ -131,7 +131,7 @@ public class Lista { } @Override - public String toString(){ + public String toString() { if (this.testa != null) { StringBuilder sb = new StringBuilder(); Nodo coda = this.testa; @@ -141,11 +141,62 @@ public class Lista { 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 traverseNode = this.testa; + Nodo prevNode = this.testa; + swapped = false; + + while (traverseNode.getNext() != null) { + + // Temporary pointer to store the next + // pointer of traverseNode + Nodo 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++; + } + } } diff --git a/src/linkedlist/Nodo.java b/src/linkedlist/Nodo.java index b29e3f8..eb13242 100644 --- a/src/linkedlist/Nodo.java +++ b/src/linkedlist/Nodo.java @@ -1,6 +1,6 @@ package linkedlist; -public class Nodo { +public class Nodo> { private Nodo next; private E info;