2017-09-25 2 views
0

J'ai des problèmes avec l'ajout des valeurs à la pile liée, au moins avec l'impression des valeurs. Je ne suis même pas sûr si j'ai codé cela correctement pour même ajouter les valeurs à la pile. J'ai fait 3 classes pour cela, une classe Node, une classe LinkedStack et un Driver pour lancer le programme.Rien ajouté à la méthode toString() dans la méthode LinkedStack personnalisée

Voici la classe Node:

public class Node 
{ 
    int data; 
    Node link; 

    //contructor 
    public Node(int d) 
    { 
     data = d; 
     link = null; 
    } 

    public int getData() 
    { 
     return data; 
    } 

    public Node getLink() 
    { 
     return link; 
    } 

    public void setData(int d) 
    { 
     data = d; 
    } 

    public void setLink(Node n) 
    { 
     link = n; 
    } 
} 

Voici la classe LinkedStack:

import java.util.NoSuchElementException; 

public class LinkedStack 
{ 

    //declare variables 
    private Node top; 
    private int size; 

    //constructor to initialize variables 
    public LinkedStack() 
    { 
     size = 0; 
     top = null; 
    } 

    //push method to add numbers to the stack 
    public void push(int value) 
    { 
     if(!isEmpty()) 
     { 
      top.setData(value); 
      size++; 
     } 
    } 

    //method to remove the top number in the stack 
    public int pop() 
    { 
     Node temp = top; 
     if(!isEmpty()) 
     { 
      temp = top; 
      size--; 
     } 

     return temp.getData(); 
    } 

    //boolean method to check if the stack is empty 
    public boolean isEmpty() 
    { 
     if(top == null) 
      return true; 
     else 
      return false; 
    } 

    //method to return the size of the stack 
    public int size() 
    { 
     return size; 
    } 

    //method to print out the top number in the stack 
    public int peek() 
    { 
     if (isEmpty() == true) 
     { 
      throw new NoSuchElementException("Stack is empty."); 
     } 
     else 
     { 
      return top.getData(); 
     } 
    } 

    //method to turn the stack into a String 
    public String toString() 
    { 
     String returnStr = "Stack: ["; 
     for(int i = 0 ; i < size; i++) 
     { 
      returnStr += top.getData() + ", "; 
     } 

     returnStr += "]"; 

     return returnStr; 
    } 
} 

Voici la classe du pilote:

import java.util.Scanner; 

public class Driver 
{ 
    public static void main(String[] args) 
    { 
     //declare variables and initialize scanner 
     Scanner key = new Scanner(System.in); 
     int size, choice, value, end; 

     end = 0; 

     //declare and initialize the stack 
     LinkedStack stack1 = new LinkedStack(); 

     //loop to continue operations 
     while(end == 0) 
     { 
      //print out menu for commands 
      System.out.println("\t1) Push \n\t2) Pop \n\t3) Peek \n\t4) Size \n\t5) isEmpty \n\t6) End"); 
      System.out.print("Please choose an option: "); 
      choice = key.nextInt(); 

      //switch the choice and execute commands 
      switch (choice) 
      { 
       case 1: System.out.println("Please enter a value: "); 
        value = key.nextInt(); 
        stack1.push(value); 
        System.out.println(stack1.toString()); 
        break; 
       case 2: stack1.pop(); 
        System.out.println(stack1.toString()); 
        break; 
       case 3: stack1.peek(); 
        System.out.println(stack1.toString()); 
        break; 
       case 4: System.out.println("Size: " + stack1.size()); 
        System.out.println(stack1.toString()); 
        break; 
       case 5: if(stack1.isEmpty()) 
       { 
        System.out.println("Stack is empty."); 
       } 
       else 
        System.out.println("Stack is NOT empty."); 
        System.out.println(stack1.toString()); 
        break; 
       case 6: end = 1; 
        System.out.println("Goodbye!"); 
        break; 
      } 
     } 
    } 
} 

La question que je reçois est rien imprime dans la pile. Merci d'avance pour l'aide!

+1

La méthode 'push()' devrait créer un nouveau 'noeud 'et l'ajouter à la liste. Vous semblez ne jamais utiliser le membre 'link' de la classe' Node'. –

Répondre

0

Je mis à jour certaines de vos méthodes, je pense que les méthodes de repos sont bons pour aller.

public int pop() 
{ 
    Node temp = top; 
    if(!isEmpty()) 
    { 
     top = temp.link; 
     size--; 
    } else { 
      throw new IllegalAccessError(); 
    } 
    return temp.getData(); 
} 

public void push(int value) 
{ 
    Node node = new Node(value); 
    if(!isEmpty()) 
    { 
     node.link = top; 
     top = node;  
    } else { 
     top = node; 
    } 
    size++; 
} 
    public String toString() 
    { 
     String returnStr = "Stack: ["; 
     Node tmp = top; 
     while(tmp != null) { 
      returnStr += tmp.getData() + ", "; 
      tmp = tmp.link; 
     } 
     returnStr += "]"; 

     return returnStr; 
    } 
+0

Merci! Cela aide beaucoup – Jdmon1998

0

Vous avez oublié d'initialiser un objet Node

//push method to add numbers to the stack 
    public void push(int value) { 
     if(!isEmpty()){ 
      top.setData(value); 
      size++; 
     } else { 
      top = new Node(value); 
      size++; 
     } 
    } 
+0

Cela a aidé aussi, je voudrais vérifier le vôtre aussi, mais je ne peux pas vérifier les deux réponses. – Jdmon1998