Java 使用堆栈测试字符串是否为回文
我试图把一个用户输入的单词放到一个堆栈中,然后检查这个单词是否是回文。我试图将所有内容从堆栈中弹出到一个新字符串上,然后比较这些字符串。我认为我当前的问题是,我的pop()函数实际上并没有返回值。它实际上只是切断尾部节点,然后在没有尾部节点的情况下重新打印字符串。我想我的问题是,如何编写pop()函数,使其返回“popped”值 这是我的主要方法Java 使用堆栈测试字符串是否为回文,java,linked-list,stack,Java,Linked List,Stack,我试图把一个用户输入的单词放到一个堆栈中,然后检查这个单词是否是回文。我试图将所有内容从堆栈中弹出到一个新字符串上,然后比较这些字符串。我认为我当前的问题是,我的pop()函数实际上并没有返回值。它实际上只是切断尾部节点,然后在没有尾部节点的情况下重新打印字符串。我想我的问题是,如何编写pop()函数,使其返回“popped”值 这是我的主要方法 Scanner input = new Scanner(System.in); Stack_Scott_Robinson<Chara
Scanner input = new Scanner(System.in);
Stack_Scott_Robinson<Character> myList = new Stack_Scott_Robinson<Character>(); //create a list object
System.out.println("Enter a string: ");
String s = input.next();
for (int i = 0; i < s.length(); i++){
myList.push(s.charAt(i));
}
String reverseInput = "";
while (!myList.isEmpty()){
reverseInput = reverseInput + myList.Pop();
}
if (s.equals(reverseInput)){
System.out.println("This is a palindrome");
}else{
System.out.println("This is not a palidrome");
System.out.print("Would you like to re-run code with different input string(y/n)?");
another = input.next();
}
扫描仪输入=新扫描仪(System.in);
Stack_Scott_Robinson myList=新的Stack_Scott_Robinson()//创建列表对象
System.out.println(“输入字符串:”);
字符串s=input.next();
对于(int i=0;i
这是我的pop()方法
public void Pop()
{
Node countList=end;//在列表前面设置“count”节点
int size=0;//初始化size变量
//将“计数”节点在列表中下移,并增加大小变量
while(countList!=null)
{
countList=countList.next;
大小++;
}
如果(大小==0){//空列表
System.out.println(“错误:空列表”);
}else if(size==1)//一个节点列表
{
top=null;
end=null;
System.out.println(“列表现在为空”);
}
否则{
Node current=end;//在列表前面设置当前节点
对于(inti=0;i,这里是java.util.Vector的实现方式(java.util.Stack扩展了Vector)
及
public synchronized void removeElementAt(int索引){
如果(索引>=元素计数){
抛出新的ArrayIndexOutOfBoundsException(索引+“>=”+
元素计数);
}
else if(索引<0){
将新的ArrayIndex抛出BoundsException(索引);
}
int j=元素计数-索引-1;
如果(j>0){
System.arraycopy(elementData,索引+1,elementData,索引,j);
}
modCount++;
元素计数--;
elementData[elementCount]=null;/*以让gc完成其工作*/
}
在pop方法中,您可以看到,我们知道要删除什么对象,因为他们使用peek()获取该对象;然后他们只删除向量末尾的元素。希望这能有所帮助。我不确定如何在引擎盖下表示堆栈,因此如果没有它,我真的无法给出更好的答案
public void Pop()
{
Node countList = end; //set "count" node at the front of the list
int size = 0; //initialize the size variable
//moves the count node down the list and increments the size variable
while (countList != null)
{
countList = countList.next;
size++;
}
if (size == 0){ //empty list
System.out.println("error: empty list");
}else if (size == 1) //one node list
{
top = null;
end = null;
System.out.println("list is now empty");
}
else{
Node current = end;//set a current node at the front of the list
for (int i = 0; i<size-2; i++)//-2 because the list starts at 0 and we want the second to last position
{
current = current.next;//moves the current node down the list until it is
}
Node temporary = current.next; //the second to last position
Node temp = top;//create a new node space
top = current;//set the new node equal to the second to last position
top.next = null;//sets the node as the tail
}
}
public synchronized E pop() {
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1);
return obj;
}
public synchronized void removeElementAt(int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
else if (index < 0) {
throw new ArrayIndexOutOfBoundsException(index);
}
int j = elementCount - index - 1;
if (j > 0) {
System.arraycopy(elementData, index + 1, elementData, index, j);
}
modCount++;
elementCount--;
elementData[elementCount] = null; /* to let gc do its work */
}