Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Stranissimo comportamento di un mio programma con liste concatenate

    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;
    	}
    }
    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();
    	}
    }
    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??

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Vedo che c'è un:

    private static ListNode firstNode;

    Non va bene, deve essere di istanza, non di classe.

    Quando fai il new ListCharacter() per creare la lista invertita in Inv() viene messo a null firstNode ma essendo unico e globale (appunto perché di classe), la lista iniziale la fai "saltare" via.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Originariamente inviato da andbin
    Vedo che c'è un:

    private static ListNode firstNode;

    Non va bene, deve essere di istanza, non di classe.

    Quando fai il new ListCharacter() per creare la lista invertita in Inv() viene messo a null firstNode ma essendo unico e globale (appunto perché di classe), la lista iniziale la fai "saltare" via.
    Grazie mille, funziona! ^^ Credo che dovrò studiarmi prima bene le varie diciture private, static & company

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.