IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Collection et Stream Java Discussion :

[Débutant][Conception] LIFO et Iterator


Sujet :

Collection et Stream Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 82
    Points : 54
    Points
    54
    Par défaut [Débutant][Conception] LIFO et Iterator
    Bonjour,

    je dois créer ma propre pile LIFO qui fournit les méthodes suivantes:

    public void Push(Object ThisObj)
    ThisObj est inséré en tête de la pile. Si la pile était vide avant l'appel de cette méthode, ThisObj sera le seul élément de la pile

    public Object Pop()
    Si la pile n'est pas vide, Pop() renvoie la référence qui est en haut de la pile et enlève cet élément de la pile. Sinon un null est renvoyé

    public Iterator iterator()
    Crée un Iterator object pour la pile LIFO et retourne la référence



    J'ai donc d'abord créé l'interface pour la pile LIFO
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    import java.util.*;
     
    public interface LIFO_Interface{
    	public void push (Object ThisObj);
    	public Object pop ();
    	public Iterator iterator ();
    }

    Puis la classe utilisée pour la liste chaînée
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
     
    public class LLNode{
    	private LLNode next;
    	private Object data;
     
    	public LLNode (Object o){
    		data = o;
    	}
     
    	public Object getData(){
    		return data;
    	}
     
    	public void setNext (LLNode thisNode){
    		next = thisNode;
    	}
     
    	public LLNode getNext(){
    		return next;
    	}
    }


    Ensuite, j'ai écrit la classe qui implémente l'interface pour la pile LIFO utilisant une liste chaînée
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
     
    import java.util.*;
     
    public class LLStack implements LIFO_Interface {
    	private LLNode head;
     
    	public LLStack(){
    		head = null;
    	}
     
    	public void push (Object thisObj){
    		LLNode tmp = new LLNode (thisObj);
    		tmp.setNext(head);
    		head = tmp;
    	}
     
    	public Object pop (){
    		Object data = new Object();
    		if(head == null)
    			return null;
    		else{
    			data = head.getData();
    			head = head.getNext();
    			this.iterator().remove();
    		}
    		return data;
    	}
     
    	public Iterator iterator(){
    		LLStackIterator elements = new LLStackIterator(this);
    		return elements;
    	}
     
    	public LLNode getHead (){
    		return head;
    	}
    }
    Où iterator() retourne une instance de la classe LLStackkIterator (que je crée ci dessous)


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
     
    import java.util.*;
     
    public class LLStackIterator implements Iterator{
    	private LLNode cursor;
     
    	public LLStackIterator (LLStack thisStack){
    		LLNode cursor = new LLNode(null);
    		cursor.setNext(thisStack.getHead());
    	}
     
    	public boolean hasNext(){
    		return ( cursor.getNext() != null );
    	}
     
    	public Object next(){
    		cursor = cursor.getNext();
    		return (cursor.getData());
    	}
     
    	public void remove(){
    	}
    }

    Puis j'ai testé tout ça avec
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
     
    import java.io.*;
     
    public class Test{
    	public static void main(String[] args){
    		LLStack stack = new LLStack();
    		stack.push("object1");
    		stack.push("object2");
    		stack.push("object3");
    		stack.push("object4");
    		while (stack.iterator().hasNext()){
    		//for(int i=0; i<4; i++){
    			System.out.println(stack.iterator().next());
    		}
    		while (stack.iterator().hasNext());
    		do{
    			stack.pop();
    			System.out.println(stack.getHead().getData());
    		}
    		while (stack.iterator().hasNext());
    	}
    }

    Evidemment ça ne marche pas
    J'obtiens
    Exception in thread "main" java.lang.NullPointerException
    at LLStackIterator.hasNext(LLStackIterator.java:13)
    at Test.main(Test.java:10)

    Manifestement je n'ai rien compris quant au "comment marche un Iterator"
    A moins que ça soit totalement autre chose
    Ou un combo peut etre
    L'erreur est sans doute d'une stupidité confondante et hilarante pour certains d'entre vous. Je renverrai respectueusement et humblement au [Débutant] ;-)
    Enfin bref, un peu d'aide serait le bienvenu

  2. #2
    Membre actif
    Inscrit en
    Avril 2004
    Messages
    238
    Détails du profil
    Informations forums :
    Inscription : Avril 2004
    Messages : 238
    Points : 265
    Points
    265
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    import java.io.*; 
     
    public class Test{ 
       public static void main(String[] args){ 
          LLStack stack = new LLStack(); 
          stack.push("object1"); 
          stack.push("object2"); 
          stack.push("object3"); 
          stack.push("object4");
          Iterator iterator = stack.iterator();
          while (iterator.hasNext()){ 
          //for(int i=0; i<4; i++){ 
             System.out.println(iterator.next()); 
          } 
          //pareil pour tes autres while
          // tu crée d'abord une instance d'iterator et ensuite tu travaille dessus
          while (stack.iterator().hasNext()); 
          do{ 
             stack.pop(); 
             System.out.println(stack.getHead().getData()); 
          } 
          while (stack.iterator().hasNext()); 
       } 
    }

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 82
    Points : 54
    Points
    54
    Par défaut
    j'ai tjs le meme pb
    Exception in thread "main" java.lang.NullPointerException
    at LLStackIterator.hasNext(LLStackIterator.java:13)
    at Test.main(Test.java:13)

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    17
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 17
    Points : 19
    Points
    19
    Par défaut
    As-tu créé une instance d'itérator pour chaque while?

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 82
    Points : 54
    Points
    54
    Par défaut
    mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    import java.io.*;
    import java.util.*;
     
     
    public class Test{ 
       public static void main(String[] args){ 
          LLStack stack = new LLStack(); 
          stack.push("object1"); 
          stack.push("object2"); 
          stack.push("object3"); 
          stack.push("object4"); 
          Iterator iterator = stack.iterator(); 
          while (iterator.hasNext()){ 
             System.out.println(iterator.next()); 
          } 
          Iterator iterator2 = stack.iterator();  
          do{ 
             stack.pop(); 
             System.out.println(stack.getHead().getData()); 
          } 
          while (iterator2.hasNext()); 
       } 
    }

    le resultat:
    Exception in thread "main" java.lang.NullPointerException
    at LLStackIterator.hasNext(LLStackIterator.java:13)
    at Test.main(Test.java:13)

  6. #6
    Membre actif
    Inscrit en
    Avril 2004
    Messages
    238
    Détails du profil
    Informations forums :
    Inscription : Avril 2004
    Messages : 238
    Points : 265
    Points
    265
    Par défaut
    Essaye ça ca devrait etre bon :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    public class LLStackIterator implements Iterator{ 
       private LLNode cursor; 
     
       public LLStackIterator (LLStack thisStack){ 
          //avant LLNode cursor = ...;
          cursor = new LLNode(null); 
          cursor.setNext(thisStack.getHead()); 
       } 
     
       public boolean hasNext(){ 
          return ( cursor.getNext() != null ); 
       } 
     
       public Object next(){ 
          cursor = cursor.getNext(); 
          return (cursor.getData()); 
       } 
     
       public void remove(){ 
       } 
    }
    Tu devrais faire gaffe, toutes tes erreurs se situe ligne 13 , la malédiction pese sur toi

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 82
    Points : 54
    Points
    54
    Par défaut
    arf
    et encore, j'ai pas encore atteint la ligne 666...

    bon sinon, ça roulotte pour la premiere boucle, le push fonctionne comme il faut
    par contre j'ai l'impression que mon pop a un probleme pasque j'obtiens
    Exception in thread "main" java.lang.NullPointerException
    at Test.main(Test.java:21)
    une fois que la premiere iteration du "do...while" a été effectué

    va falloir que je regarde ça de plus pres
    pas plus tard que maintenant d'ailleurs
    (je propose pas de course, je sais pertinemment que je vais perdre)

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 82
    Points : 54
    Points
    54
    Par défaut
    ma classe LLStack est devenu:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    import java.util.*;
     
    public class LLStack implements LIFO_Interface {
    	private LLNode head;
     
    	public LLStack(){
    		head = null;
    	}
     
    	public void push (Object thisObj){
    		LLNode tmp = new LLNode (thisObj);
    		tmp.setNext(head);
    		head = tmp;
    	}
     
    	public Object pop (){
    		Object data = new Object();
    		if(head == null)
    			return null;
    		else{
    			Iterator iter = this.iterator();
    			data = head.getData();
    			head = head.getNext();
    			iter.remove();
    		}
    		return data;
    	}
     
    	public Iterator iterator(){
    		LLStackIterator elements = new LLStackIterator(this);
    		return elements;
    	}
     
    	public LLNode getHead (){
    		return head;
    	}
    }


    qqun pourrait il m'expliquer pourquoi ds le test ci dessous :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    import java.io.*;
    import java.util.*;
     
     
    public class Test{ 
       public static void main(String[] args){ 
          Object data = new Object();
          LLStack stack = new LLStack(); 
          stack.push("object1"); 
          stack.push("object2"); 
          stack.push("object3"); 
          stack.push("object4"); 
          Iterator iterator = stack.iterator(); 
          while (iterator.hasNext()){ 
             System.out.println(iterator.next()); 
          }  
          for(int i=0;i<4;i++) {  
    	 System.out.println("test pop");
             data = stack.pop();   
    	 System.out.println(data);
          } 
        /* do {  
    	 System.out.println("test pop");
             data = stack.pop();   
    	 System.out.println(data);
          } 
          while (iterator2.hasNext());*/
     
       } 
    }
    tout se passe bien avec le for mais pas avec le do...while?

  9. #9
    Membre du Club
    Profil pro
    Inscrit en
    Octobre 2003
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2003
    Messages : 82
    Points : 54
    Points
    54
    Par défaut
    c bon je viens de comprendre
    j'avais une boucle infinie ce qui est normal: je n'avais pas mis un iterator.next() ds ma boucle, le curseur restait toujours à la meme position et forcement le iterator.hasNext() etait toujours true
    silly me
    apres rectification:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    import java.io.*;
    import java.util.*;
     
     
    public class Test{ 
       public static void main(String[] args){ 
          Object data = new Object();
          LLStack stack = new LLStack(); 
          stack.push("object1"); 
          stack.push("object2"); 
          stack.push("object3"); 
          stack.push("object4"); 
          Iterator iterator = stack.iterator(); 
          while (iterator.hasNext()){ 
             System.out.println(iterator.next()); 
          }  
          Iterator iterator2 = stack.iterator();
          do {  
    	 System.out.println("test pop");
    	 iterator2.next();
             data = stack.pop();   
    	 System.out.println(data); 
          } 
     
       } 
    }
    merci à tous ceux qui m'ont aidé

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Débutant][Conception] Factorielle en Java par recursivité
    Par Sylvester dans le forum Débuter avec Java
    Réponses: 9
    Dernier message: 02/11/2008, 20h42
  2. [Débutant] Conception requêtes SQL
    Par LhIaScZkTer dans le forum Langage SQL
    Réponses: 15
    Dernier message: 10/01/2006, 21h46
  3. [Débutant] Concepts : types + tas de questions
    Par Epouvantail dans le forum C++
    Réponses: 7
    Dernier message: 01/12/2005, 10h27
  4. [Débutante] Conception
    Par bolo dans le forum Assembleur
    Réponses: 14
    Dernier message: 25/11/2004, 03h40
  5. [débutante][Concept] Destruction d'objet, mode d'emploi?
    Par skea dans le forum Général Java
    Réponses: 4
    Dernier message: 12/06/2004, 21h48

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo