Palindrome与基于阵列的堆栈和队列检查

时间:2015-08-11 02:13:30

标签: java palindrome

对于作业,我们正在应用标题中的内容。我已经编写了所有代码,但是当我编译代码时,我得到了处理代码行19的四个错误。

while(!myQueue<String>.isEmpty() & !myStack.isEmpty()){

这是完整的代码,如果它也有帮助

    import java.util.*;
    public class Palindrome{
         public static void main(String[] args){
         Scanner scan = new Scanner(System.in);
         String userInputConversion;
         String userInput;
         MyStack myStack = new MyStack();
         MyQueue<String> myQueue = new MyQueue<String>();
         System.out.println("Enter in a possible Palindrome. ");
         userInputConversion = scan.next();
         userInput = userInputConversion.toLowerCase();
         String s = new String();
    for(int i = 0; i < userInput.length(); i++){
        s = "" + userInput.charAt(i);
        System.out.print(s);
        myQueue.enqueue(s);
        myStack.push(s);
    }
    while(!myQueue<String>.isEmpty() & !myStack.isEmpty()){
        String deQueued = myQueue.dequeue();
        String popped = myStack.pop();
    if(deQueued == popped)
        System.out.println("Input is a palindrome. ");
    else
        System.out.println("input isnt a palindrome. ");
    }
        }
    }
    class MyStack{
    private String[] stack;
    private int top;
    public MyStack(){
    stack = new String [100];
    top = 0;
}
public String push(String pushP){
    if(top >= stack.length){
        System.out.println("Error: MyStack.push(): stack overflow");
        return "yes";
    }
    stack[top] = pushP;
    top++;
}
public String pop(){
    if(top <= 0){
        System.out.print("Error in MyStack.pop(): stack empty");
        return "n";
    }
    top--;
    return stack[top];
}
public boolean isEmpty(){
    if(top == 0){
        return true;
    }
    else{
        return false;
    }
    }
    `}
    class MyQueue<String> implements Iterable<String> {
    private String[] queue;
    private int front = 0;
    private int rear = 0;
    private int currentSize = 0;

public MyQueue(){
    queue = (String[])(new Object[1]);
    front = 0;
    rear = 0;
    currentSize = 0;
}
public boolean isEmpty() {
    return (currentSize == 0);
}
public int currentSize() {
    return currentSize;
}
public void enqueue(String String) {
    if (currentSize == queue.length - 1) {    
        resize(2 * queue.length);
    }

    queue[rear++] = String;

    if (rear == queue.length) {
        rear = 0;  
    }

    currentSize++;
}

public String dequeue() {
    if (this.isEmpty()) {
        throw new RuntimeException("Tried to dequeue an empty queue");
    }
    else {
        String itemToReturn = queue[front];
        queue[front++] = null; 
        currentSize--;
        if (front == queue.length) {
            front = 0;
        }
        if (currentSize == queue.length / 4) {
            resize(queue.length / 2);
        }

        return itemToReturn;
    }
}

private void resize(int capacity) {
    String[] newArray = (String[]) new Object[capacity];
    for (int i = 0; i < currentSize; i++) {
        newArray[i] = queue[(front + i) % queue.length];
    }
    queue = newArray;
    front = 0;
    rear = currentSize;
}
}

如果有人可以提供帮助,那就太棒了或者给出一些指示。

2 个答案:

答案 0 :(得分:0)

首先你的制作很复杂,对于一个简单的字符串,你为什么要使用堆栈或队列。我想以下逻辑会帮助你

String original, reverse = "";
      Scanner in = new Scanner(System.in);

      System.out.println("Enter a string to check if it is a palindrome");
      original = in.nextLine();

      int length = original.length();

      for ( int i = length - 1; i >= 0; i-- )
         reverse = reverse + original.charAt(i);

      if (original.equals(reverse))
         System.out.println("Entered string is a palindrome.");
      else
         System.out.println("Entered string is not a palindrome.");

答案 1 :(得分:0)

对于第二次编译错误SELECT up.*,pl.id, pl.user_playlist_id, ( CASE WHEN pl.video_id IS NULL THEN 0 ELSE 1 END AS is_video_id_select ) FROM user_playlist as up INNER JOIN playlist ON up.playlist_id = pl.id WHERE up.user_id = 'x' // where x is a user id ,您可以

  • 实施The type MyQueue<String> must implement the inherited abstract method Iterable<String>.iterator()方法
  • 删除public Iterator<String> iterator()声明
  • 或制作MyQueue implements Iterable<String>

制作MyQueue abstract不会对你有所帮助。我也没有在代码中看到您需要abstract的任何地方,或者使用iteratorMyQueue的事实。作为一个队列,你会想要使用它的签名方法 - Iterable&amp; enqueue。因此,您可以安全地选择选项2.除此之外,这个answer应该有所帮助。

你还没有完美地实现type arguments的概念。您可能希望在类定义中使用dequeue;例如Type Parameter变为class MyQueue<String>。同样,成员变量&amp;方法也会改变。

第3次编译错误,class MyQueue<T>只是因为您的This method must return a result of type String方法最后没有push()语句。最简单的做法是return,因为您并未在任何地方使用返回的void。对于String "yes",您可以抛出StackOverflow,就像在RuntimeException中一样。

几个指针

  • 您在dequeue语句中将字符串与==而不是.equals()进行比较时犯了一个经典错误。
  • 请务必关闭scanner /资源,即使在这种情况下没有任何伤害。

你的if (deQueued == popped)中有一个比较角色的逻辑错误 - 我会让你想出那个。