【问题标题】:Sending a queue through a constructor in java?通过java中的构造函数发送队列?
【发布时间】:2018-08-20 00:03:23
【问题描述】:

目标是通过构造函数传递数据结构(队列),并在通过方法后返回一个新队列。我创建了一个 Queue 类型的方法,可以将中缀转换为后缀顺序。问题是,当我通过构造函数传递队列时,我输出的是所有的 'a' 而不是方程本身。所以,我知道链表传递的是队列的长度,而不是字符本身。 输出:

a+b+c/(d+f)
aaaaaaaaaaa

主类:

import java.io.*;
import java.lang.*;
class Convert
{

    static int Prec(char ch)
    {
        switch (ch)
        {
        case '+':
        case '-':
            return 1;

        case '*':
        case '/':
            return 2;

        case '^':
            return 3;
        }
        return -1;
    }


    public static QueueADT infixToPostFix(QueueADT in)
    {
        QueueADT infix = in;
        QueueADT result = new QueueADT();
        StackADT stack = new StackADT();

        while(infix.empty() == false)
        {
            char c = infix.dequeue();

            if (Character.isLetterOrDigit(c))
                result.enqueue(c);

            else if (c == '(')
                stack.push(c);


            else if (c == ')')
            {
                while (!stack.empty() && stack.peek() != '(')
                    result.enqueue(stack.pop());

                stack.pop();
            }
            else // an operator is encountered
            {
                while (!stack.empty() && Prec(c) <= Prec(stack.peek()))
                    result.enqueue(stack.pop());
                stack.push(c);
            }

        }

        // pop all the operators from the stack
        while (!stack.empty())
            result.enqueue(stack.pop());

        return result;
    }

    public static void main(String[] args) 
    {
        QueueADT infix = new QueueADT();
        String str = "a+b+c/(d+f)";
        for(int i=0; i < str.length(); i++)
        {
            infix.enqueue(str.charAt(i));
            System.out.print(str.charAt(i));
        }
        QueueADT postfix = infixToPostFix(infix);
        System.out.println();
        while(!postfix.empty())
        {
            System.out.print(postfix.dequeue());
        }
    }
}

队列类:

public class QueueADT 
{
    private int size;
    private Node front;
    private Node rear;

    public QueueADT()
    {
        size = 0;
        front = null;
        rear = null;
    }

    public boolean empty()
    {
        return(size == 0);
    }
    public int size()
    {
        return size;
    }
    public void enqueue(char character)
    {
        Node newNode = new Node();
        newNode.setData(character);
        newNode.setNext(null);
        if(this.empty())
        {
            front = newNode;
        }
        else
            rear.setNext(newNode);
        rear = newNode;
        size++;
    }
    public char dequeue()
    {
        char i;
        i = front.getData();
        size--;
        if(this.empty())
            rear = null;
        return i;
    }
    public char front()
    {
        return front.getData();
    }
}

堆栈类:

public class StackADT 
{
    private Node top;
    private int size;
    public StackADT()
    {
        top = null;
        size = 0;
    }
    public boolean empty()
    {
        return (top == null);
    }
    public char peek()
    {
        return top.getData();
    }
    public int size()
    {
        return size;
    }
    public void push(char character)
    {
        Node newNode = new Node();
        newNode.setData(character);
        newNode.setNext(top);
        top = newNode;
        size++;
    }
    public char pop()
    {
        char i;
        i = top.getData();
        top = top.getNext();
        size--;
        return i;
    }
    public int onTop()
    {
        char i = pop();
        push(i);
        return i;
    }
}

节点类:

public class Node 
{
    private char data;
    private Node next;

    public Node()
    {
        data = 0;
        next = null;
    }
    public Node(char d)
    {
        data = d;
    }
    public Node(char d, Node n)
    {
        data = d;
        next = n;
    }
    public void setData(char newData)
    {
        data = newData;
    }
    public void setNext(Node newNext)
    {
        next = newNext;
    }
    public char getData()
    {
        return data;
    }
    public Node getNext()
    {
        return next;
    }
    public void displayNode()
    {
        System.out.print(data);
    }
}

【问题讨论】:

    标签: java list stack queue


    【解决方案1】:

    您在 QueueADT 类中的 dequeue 方法的实现不正确。您永远不会更改字段“front”,这就是为什么在您的情况下调用该方法时,总是返回“a”。添加 front = front.getNext(); 线后 char i = front.getData(); 该代码还有更多问题 - 尝试分别测试每个方法,而不仅仅是整个程序。

    【讨论】:

      猜你喜欢
      • 2021-07-30
      • 2013-06-02
      • 1970-01-01
      • 2012-09-03
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 2015-02-22
      相关资源
      最近更新 更多