我正在尝试使用堆栈来反转Java中的链表我继续收到此错误:
LinkStackApp.java:84: error: constructor LinkStack in class LinkStack cannot be applied to given types;
LinkStack stackrev = new LinkStack(stackSize);
^
required: no arguments
found: int
reason: actual and formal argument lists differ in length
1 error
我不知道它在哪里找到int,我尝试将字符串更改为char,反之亦然,但只是继续获取更多错误的任何想法?这是我的完整代码:
import java.io.*;
import java.util.Scanner;
class Link
{
public char dData;
public Link next;
public Link(char dd)
{
dData = dd;
}
public void displayLink()
{
System.out.print(dData + " ");
}
}
class LinkList
{
private Link first; //ref to first item on the list
public LinkList() //no items on list yet
{
first = null;
}
public boolean isEmpty()
{
return (first == null);
}
public void insertFirst(char dd)
{
Link newLink = new Link(dd);
newLink.next = first;
first = newLink;
}
public char deleteFirst()
{
Link temp = first;
first = first.next;
return temp.dData;
}
public void displayList()
{
Link current = first;
while(current != null)
{
current.displayLink();
current = current.next;
}
System.out.println(" ");
}
}
class LinkStack
{
private LinkList theList;
public LinkStack()
{
theList = new LinkList();
}
public void push(char j)
{
theList.insertFirst(j);
}
public char pop()
{
return theList.deleteFirst();
}
public boolean isEmpty()
{
return (theList.isEmpty());
}
}
class Reverser{
private String input;
private String output;
public Reverser(String in){
input = in;
}
public String doRev(){
int stackSize = input.length();
LinkStack stackrev = new LinkStack(stackSize);
for (int j = 0; j <input.length(); j++){
char ch = input.charAt(j);
stackrev.push(ch);
}
output = "";
while(!stackrev.isEmpty()){
char ch = stackrev.pop();
output = output + ch;
}
return output;
}
}
class LinkStackApp
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String inputString, outputString;
while(true){
System.out.print("Enter A String: ");
inputString = input.nextLine();
if(inputString.equals(""))
break;
Reverser therev = new Reverser(inputString);
outputString = therev.doRev();
System.out.println("Reversed: "+outputString);
}
}
}
答案 0 :(得分:2)
stackSize
是int
。它告诉你调用没有参数的构造函数。
正确的构造函数调用是:
LinkStack stackrev = new LinkStack();