Salve. Devo creare un programma che legge dei caratteri e li salva in una lista concatenata, poi devo creare una nuova lista con l'ordine inverso di quella precedente.
Queste sono le classi per creare una lista:
codice:package cap7; class ListNode { Object data; ListNode nextNode; ListNode (Object object) { this (object, null); } ListNode (Object object, ListNode node) { data = object; nextNode = node; } Object getObject () { return data; } ListNode getNext () { return nextNode; } } public class ListCharacter { private static ListNode firstNode; private ListNode lastNode; private String name; public ListCharacter () { this ("list"); } public ListCharacter (String listName) { name = listName; firstNode = lastNode = null; } public void insertAtFront (Object insertItem) { if (isEmpty ()) { firstNode = lastNode = new ListNode (insertItem); } else { firstNode = new ListNode (insertItem, firstNode); } } public void insertAtBack (Object insertItem) { if (isEmpty ()) { firstNode = lastNode = new ListNode (insertItem); } else { lastNode = lastNode.nextNode = new ListNode (insertItem); } } public Object removeFromFront () throws EmptyListException { if (isEmpty ()) { throw new EmptyListException (name); } Object removedItem = firstNode.data; if (firstNode == lastNode) { firstNode = lastNode = null; } else { firstNode = firstNode.nextNode; } return removedItem; } public Object removeFromBack () throws EmptyListException { if (isEmpty ()) { throw new EmptyListException (name); } Object removedItem = lastNode.data; if (firstNode == lastNode) { firstNode = lastNode = null; } else { ListNode current = firstNode; while (current.nextNode != lastNode) { current = current.nextNode; } lastNode = current; current.nextNode = null; } return removedItem; } public boolean isEmpty () { return firstNode == null; } public void print () { if (isEmpty ()) { System.out.printf ("Empty %s\n", name); return; } System.out.printf ("The %s is : ", name); ListNode current = firstNode; while (current != null) { System.out.printf ("%s ", current.data); current = current.nextNode; } System.out.println ("\n"); } public ListCharacter Inv () { ListCharacter finalList = new ListCharacter(); print(); ListNode current = firstNode; while (current != null) { finalList.insertAtFront (current.data); current = current.nextNode; } return finalList; } }Ho evidenziato in grassetto la parte in cui avviene la "cosa" strana. Siccome quando vado a stampare la lista invl risulta essere vuota, ho provato a stampare la lista nel metodo Inv attraverso il metodo print. Se richiamo print prima di ListCharacter finalList = new ListCharacter(); , la lista viene stampata senza problemi. Se invece richiamo print dopo questa dichiarazione, la lista risulta vuota... come è possibile che accada questa stranezza??codice:import java.util.Scanner; import cap7.ListCharacter; public class ListChar { public static void main (String[] args) { Scanner scanner = new Scanner (System.in); ListCharacter list = new ListCharacter(); ListCharacter invl = new ListCharacter(); for (int i = 0; i < 5; i++) { System.out.print ("Inserisci carattere: "); list.insertAtBack (scanner.next()); } list.print(); invl = list.Inv(); System.out.println ("Ordine inverso:"); invl.print(); } }

Rispondi quotando
