2014-09-24 128 views
-2

有没有人知道如何编写方法toStringRec递归地打印堆栈? 下面是类和主要方法文件 我有蜜蜂试图运行它几小时,但未能完成。递归栈打印方法

public class IntLinkedStack 
{ 
private IntNode top; 
public static int temp = 0; 

public boolean isEmpty() 
{ 
    if (top == null) 
    return true; 
    else 
    return false; 
} 

public int size() 
{ 
    return sizeRec(top); 
} 
private int sizeRec(IntNode t) 
{ 

    IntNode aux = t; 

    if (aux != null){ 
     temp++; 
     sizeRec(aux.next); 
    } 

    return temp ; 

} 

public String toString() 
{ 
    return toStringRec(top); 
} 

// WRITE THIS METHOD: toStringRec 
private String toStringRec(IntNode t) 
{   

} 

public int peek() 
{ 
    if (isEmpty()) 
    throw new RuntimeException("Peek attempted on empty stack"); 
    else 
    return top.data; 
} 

public void push(int m) 
{ 
    IntNode temp; 
    temp = new IntNode(); 
    temp.data = m; 

    temp.next = top; 
    top = temp; 
} 

public int pop() 
{ 
    int value; 

    if (isEmpty()) 
    throw new RuntimeException("Pop attempted on empty stack"); 
    else 
    { 
     value = top.data; 
     top = top.next; 
     return value; 
    } 
} 

} 

接下来的主要方法

import java.util.Scanner; 

public class ITDIntLinkedStack 
{ 
public static void displayMenu() 
{ 
System.out.println("\n(1) display menu"); 
System.out.println("(2) check isEmpty"); 
System.out.println("(3) push"); 
System.out.println("(4) peek"); 
System.out.println("(5) pop"); 
System.out.println("(6) empty the stack"); 
System.out.println("(7) size"); 
System.out.println("(8) toString"); 
System.out.println("(0) quit"); 
} 

public static void main(String [] args) 
{ 
if (true || false) 
    System.out.println("YES"); 



Scanner kbd = new Scanner(System.in); 
IntLinkedStack x = new IntLinkedStack(); 
int choice = 1; 
int value; 

System.out.println("Here are your choices: "); 
displayMenu(); 

System.out.print("\nEnter your choice#: "); 
    choice = kbd.nextInt(); 

while (choice >= 1 && choice <= 8) 
{ 
    if (choice == 1) 
    displayMenu(); 
    else if (choice == 2) 
    System.out.println("Stack isEmpty?: " + x.isEmpty()); 
    else if (choice == 3) 
    { 
    System.out.print("Enter value to push: "); 
    value = kbd.nextInt(); 
    x.push(value); 
    System.out.println("Pushed value " + value + "."); 
    } 
    else if (choice == 4) 
    { 
    value = x.peek(); 
    System.out.println("Peeked at top of stack: " + value + "."); 
    } 
    else if (choice == 5) 
    { 
    value = x.pop(); 
    System.out.println("Popped " + value + " off the stack."); 
    } 

    else if (choice == 6) 
    { 
    System.out.println("Emptying stack..."); 
    while (!x.isEmpty()) 
     System.out.println("Popped " + x.pop()); 
    System.out.println("Stack is now emptied."); 
    } 

    else if (choice == 7) 
    System.out.println("Size is " + x.size()); 

    else if (choice == 8) 
    { 
    System.out.println("Here is a view of the stack: "); 
    System.out.println(x.toString()); 
    } 


    System.out.print("\nEnter your choice#: "); 
    choice = kbd.nextInt(); 

} // end while (choice >= 1 && <= 9) 

System.out.println("Bye!"); 

} // end public static void main(String [] args) 


} // end public class ITDArrayStack 
+0

而在这几个小时,你没能写出toStringRec方法单行?你知道递归的意思吗? – ElDuderino 2014-09-25 11:34:31

+0

刚刚接触java ... – 2014-09-26 21:51:15

+0

感谢您的帮助 – 2014-09-26 21:51:39

回答

0

打印堆栈元素递归:

private String toStringRec(IntNode t) { 
    if (t == null) 
     return ""; 
    return t.data + "\n" + toStringRec(t.next); 
    }