2013-02-27 113 views
1

我正在尝试使用堆栈和链接列表构建一个回文检查器。我正在使用泛型来重用节点和结构来完成这个任务的两个部分(在下一部分中完成不同的部分)。链接列表堆栈/泛型实现(作业)

程序不会将字母推入堆栈 - 它返回空值。我相信问题在于构建push方法,无论是在LinkedStack构建中,还是在StackDriver实现中,或者两者兼而有之。我只是不确定我做错了什么;我已经尝试了很多选择,并且已经查找并尝试了其他方法来构建推送方法,但是随后出现错误并且无法让程序运行。 (我意识到我在这里的2个推送方法是不同的 - 这些是我尝试过的2个版本)。我应该看一些拳击类型的char c使用包装类?

该程序已被设置回它运行的最后一个点。 “颠倒过来”的弹出式元素似乎获得了正确数量的字符 - 为什么?

我意识到这个程序还存在其他问题,但我觉得我不能解决它们,直到我通过这个绊脚石。任何援助将不胜感激 - 谢谢!

迈克

定的接口:

public interface Stack<E> { 
    void push(E data); 
    E pop(); 
    boolean isEmpty(); 
    int size(); 
    E stackTop(); 
    void clear(); 
} 

节点和方法:

public class Node<E> { 

// create the node structure 
    private E data; 
    private Node<E> next; 


    // getters and setters 
    public E getData() { 
     return data; 
    } 
    public void setData(E data) { 
     this.data = data; 
    } 
    public Node<E> getNext() { 
     return next; 
    } 
    public void setNext(Node<E> next) { 
     this.next = next; 
    } 

}

堆栈:

import java.util.EmptyStackException; 


public class LinkedStack<E> implements Stack<E>{ 

// Create the head and nodeCount variables 
private Node<E> head; 
private int nodeCount; 

// also need to be able to convert letters to capitals. 

// constructor for the LinkedStack 
public LinkedStack() 
{ 
    clear(); 
} 


// A method to push the data onto a stack and increment the node count 
public void push(E data) { 
    head = new Node<E>(); 
    nodeCount++; 
    } 


// pop the head off of the stack and decrement the node count 
public E pop() { 
    E item; 

    if (head == null) 
     throw new EmptyStackException(); 

    item = head.getData(); 
    head = head.getNext(); 
    nodeCount--; 
    return item; 
} 


// Check if the stack is empty 
public boolean isEmpty() { 
    if (head == null); 
    return true; 
} 


// check the size of the node 
public int size() { 
    return nodeCount; 
} 


// this is the peek method 
public E stackTop() 
{ 
    if (head == null) 
     throw new EmptyStackException(); 
    return head.getData(); 
} 


// clear the Linked Stack 
public void clear() { 
    head = null; 
    nodeCount = 0; 
} 

// convert to text 
     public String toString() { 
      String rtn = ""; 

      if (nodeCount == 0) { 
       rtn += "<empty>"; 
      } 
      else { 
       Node<E> t = head; 

       while (t != null){ 
        /* return the node data on a line with the head node data 
        at the beginning of the line and the arrow pointing to 
        each successive node*/ 
        rtn += t.getData() + "->"; 
        // go on to the next 
        t = t.getNext(); 
       } 
      rtn += "null"; 
      } 
      return rtn; 

     } 


     } 

和司机:

import java.util.Iterator; 
import java.util.Scanner; 

public class StackDriver<E> implements Iterator<E>{ 

/** 
* @param args 
*/ 
public static void main(String[] args) { 

//Initialize the driver 
StackDriver run = new StackDriver(); 
run.doIt(); 

} 
public void doIt() { 

    // gather the input 
    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Please enter a phrase. This program will verify" + 
      " if the phrase is a palindrome."); 

    // holder for the phrase 
    String phrase; 

    // holder for the reversed phrase 
    String reversed = ""; 

    phrase = keyboard.nextLine().toUpperCase(); 
    System.out.println("You entered: "+ phrase); 

    // create the two stacks for the characters 
    LinkedStack<E> alpha = new LinkedStack<E>(); 
    LinkedStack<E> punctuation = new LinkedStack<E>(); 

    //------------------------------------------ 
     for(int i=0; i<phrase.length(); i++) 
     { 
     // if the character is a letter, push it onto the letters stack 
      char c = phrase.charAt(i); 
      if (true == Character.isLetter(c)) 

     { 
      // (testing purposes only- remove next line) 
       System.out.println("LETTER"); 
       String A = Character.toString(c); 

      // push the letter onto the stack 
       alpha.push((E) new Node<E>());  
     } 

     // else push it onto the characters stack 
     else 
     { 
      // (testing purposes only- remove next line) 
      System.out.println("NOT LETTER"); 
      String B = Character.toString(c); 

      // push the character onto the stack 
      punctuation.push((E) new String(B));  
     } 
      // then pop the letters stack 
     while (!alpha.isEmpty()); 
     { 
      reversed += alpha.pop(); 
     } 
     } 
     //------------------------------------------ 
    // if it equals the String phrase 
     if (reversed == phrase) 
      // it is a palindrome 
      System.out.println("The phrase you entered is a palindrome"); 
    else 
     System.out.println("The phrase you entered is NOT a palindrome"); 
     System.out.println("phrase: " + phrase); 
     System.out.println("alpha: " + alpha); 
     System.out.println("reversed: " + reversed); 
} 
@Override 
public boolean hasNext() { 
    // TODO Auto-generated method stub 
    return true; 
} 
@Override 
public E next() { 
    // TODO Auto-generated method stub 
    return null; 
} 
@Override 
public void remove() { 
    // TODO Auto-generated method stub 

} 


} 

而结果:

Please enter a phrase. This program will verify if the phrase is a palindrome. 
mom 
You entered: MOM 
LETTER 
LETTER 
LETTER 
The phrase you entered is NOT a palindrome 
phrase: MOM 
alpha: <empty> 
reversed: nullnullnull 
+0

尝试用更少的代码,以显示您的问题... – Aubin 2013-02-27 18:05:29

+0

@Aubin:这将是人谁是不做作业非常好的建议,或者如果它是在别人的库可重复的错误。但是在迈克德默斯目前正在编程的层面上,他不应该指望他能够用更少的代码来展示问题。此外,如果他知道要显示的确切代码,他也会知道问题出在哪里。 – atk 2013-02-27 18:19:53

回答

0

这可能是也可能不是你的整个问题,但它绝对是其中的一部分......

// A method to push the data onto a stack and increment the node count 
public void push(E data) { 
    head = new Node<E>(); 
    nodeCount++; 
} 
  • 总是会替换头不包含数据的新节点,即使E的对象类型已通过。你需要调用head.setData(data)。
  • 你还需要添加到列表而不是替换头。
    if(head == null) head = new Node(); head.setData(data); } else { Node n = new Node(); n.setData(data); 节点last = ...; //留下列表中的最后一个节点作为练习 last.setNext(n); }
+0

谢谢大家对此的帮助 - 这是我需要再次移动的东西。我很感激! – 2013-02-28 15:14:01

4

如果我收到了你的问题正确,我认为这个问题确实是在LinkedStack类的push方法。看一看。

public void push(E data) { 
    head = new Node<E>(); 
    nodeCount++; 
} 

您创建一个新的Node,将其分配给head和增加你的筹码的节点数量,但你从来没有真正链接老头或填充新的现任掌门人,你只是更换头一个没有上一个或下一个元素的新节点。

+0

我注意到这一点,即将发布,但决定首先刷新。每次他将头部的链接列表中断时,也不会将数据存储在非本身内部,因此它什么都不做。 – 2013-02-27 18:07:08

+0

@JesusAdoboLuzon正是!它只是一个空头节点,没有数据,但有一个精确的elementCount(即堆栈说他有4个元素,但它有一个空头)。 – Gamb 2013-02-27 18:08:24

2
// A method to push the data onto a stack and increment the node count 
public void push(E data) { 
    head = new Node<E>(); 
    nodeCount++; 
    } 

这种方法是错误的。当你推新节点时,你需要将它设置为next到当前的头部。您还需要使用您的数据填充该节点。