返回java中对象堆栈的副本

返回java中对象堆栈的副本,java,list,linked-list,stack,reverse,Java,List,Linked List,Stack,Reverse,我有一个堆栈方法,假设返回此对象的反向*副本*。 我需要这个对象链接到那个对象。谢谢 更新 澄清一下,, 创建的堆栈对象推送从该对象弹出的项目。 我希望此对象在此对象变空后引用该对象。我真正想要的是返回该对象的反向副本。明白了吗 public LinkedStack<E> reversed() { LinkedStack<E> that= new LinkedStack<E>(); if(this.isEmpty()){ ret

我有一个堆栈方法,假设返回
对象的反向*副本*。 我需要
这个
对象链接到
那个
对象。谢谢

更新

澄清一下,, 创建的堆栈对象推送从该对象弹出的项目。 我希望此对象此对象变空后引用该对象。我真正想要的是返回该对象的反向副本。明白了吗

public LinkedStack<E> reversed()
{
    LinkedStack<E> that= new LinkedStack<E>();
    if(this.isEmpty()){
        return this;
    }
    else{
        while(top!=null)
            {
                that.push(pop());
            }
        }
    return this;
    }
public-LinkedStack-reversed()
{
LinkedStack,它=新LinkedStack();
if(this.isEmpty()){
归还这个;
}
否则{
while(top!=null)
{
推(砰的一声);
}
}
归还这个;
}
全班

import java.util.NoSuchElementException;
//import java.util.Stack;
public class LinkedStack<E>{
    @SuppressWarnings("hiding")
    public class Node<E>{
        private E info;
        private Node<E> link;
        public Node(E info,Node<E>link){
            this.info=info;
            this.link=link;
        }//Node constructor
    public void Setinfo(E info){this.info =info;}

    public E getinfo(){return info;}

    public void setLink(Node<E> newLink){this.link=newLink;}

    public Node<E> getLink(){return this.link;}
}//end of node

protected Node<E> upnode;
public LinkedStack(){
    upnode=null;
}

//isEmpty method
public boolean isEmpty(){
    if(upnode==null){
        return true;
    }
    else
        return false;
}
//item push
public void push(E item)
{
    Node<E> sth=new Node<E>(item,upnode);
    sth.setLink(upnode);
    upnode=sth;
}
//LinkedStack push
public void push(LinkedStack<E> s)
{
    if(s.isEmpty()==true)
    {
        throw new NoSuchElementException();
    }
    else{
        while(!(s.isEmpty()))
        {
            this.push(s.pop());
        }

    }
}
//peek method
public E peek()
    {
    if(upnode==null){
        throw new NoSuchElementException();
        }
    else
        return upnode.getinfo();
    }
//pop method
public E pop()
{
    if(upnode==null){
        throw new NoSuchElementException();
    }
    else{
        E item=peek();
        upnode=upnode.link;
        return item;
    }
}

public int size(){
int ct=0;
if(this.isEmpty()==true){
    throw new NoSuchElementException();
}
else{
    while(this.isEmpty()==false){
        ct++;
        upnode=upnode.getLink();
        }
    }
return ct;
}

//Reverse method
public LinkedStack<E> reversed()
{
    LinkedStack<E> that = new LinkedStack<E>();
    if(this.isEmpty()){
        return this;
    }
    else{
        while(!this.isEmpty())
            {
                that.push(pop());
            }
        }
    return this;
    }
//Returns a string representation of this stack
public String toString()
{
    String result="";
    Node<E> current=upnode;//set the current node to upnode
    while(current !=null)
    {//while link isn't null
        result=result+(current.getinfo()).toString()+"\n";//get info and call toString
        current=current.getLink();//Get the link of the current node
    }
    return result;//return result
    }
}//end of LinkedStack
import java.util.NoSuchElementException;
//导入java.util.Stack;
公共类LinkedStack{
@抑制警告(“隐藏”)
公共类节点{
私人电子信息;
专用节点链路;
公共节点(E信息、节点链接){
this.info=info;
this.link=link;
}//节点构造函数
public void Setinfo(E info){this.info=info;}
public E getinfo(){return info;}
public void setLink(Node newLink){this.link=newLink;}
公共节点getLink(){返回this.link;}
}//节点末端
受保护节点upnode;
公共LinkedStack(){
upnode=null;
}
//等空法
公共布尔值为空(){
if(upnode==null){
返回true;
}
其他的
返回false;
}
//项目推送
公共无效推送(E项)
{
Node sth=新节点(项目,upnode);
设置链接(上行节点);
upnode=sth;
}
//链接堆栈推送
公共无效推送(LinkedStack s)
{
如果(s.isEmpty()==true)
{
抛出新的NoTouchElementException();
}
否则{
而(!(s.isEmpty())
{
这个.推(s.pop());
}
}
}
//peek法
公共E peek()
{
if(upnode==null){
抛出新的NoTouchElementException();
}
其他的
返回upnode.getinfo();
}
//流行方法
公共E-pop()
{
if(upnode==null){
抛出新的NoTouchElementException();
}
否则{
E项=peek();
upnode=upnode.link;
退货项目;
}
}
公共整数大小(){
int-ct=0;
if(this.isEmpty()==true){
抛出新的NoTouchElementException();
}
否则{
while(this.isEmpty()==false){
ct++;
upnode=upnode.getLink();
}
}
返回ct;
}
//反向法
公共链接堆栈已反转()
{
LinkedStack,它=新LinkedStack();
if(this.isEmpty()){
归还这个;
}
否则{
而(!this.isEmpty())
{
推(砰的一声);
}
}
归还这个;
}
//返回此堆栈的字符串表示形式
公共字符串toString()
{
字符串结果=”;
Node current=upnode;//将当前节点设置为upnode
while(当前!=null)
{//while链接不为null
result=result+(current.getinfo()).toString()+“\n”//获取信息并调用toString
current=current.getLink();//获取当前节点的链接
}
返回结果;//返回结果
}
}//LinkedStack的结束

你的问题没有多大意义。(你下面的评论也没有)

但是您当前的方法肯定是错误的,因为
this.pop()
操作将改变
this
的状态


正确的解决方案将取决于
LinkedStack
类的内部详细信息。。。您还没有向我们展示。

while(top!=null)
更改为
while(!this.isEmpty())
当您修改实例时,您不需要创建辅助
LinkedStack

例如:

public LinkedStack<E> reversed(){
     int size = size();
     while(size-- > 0){ // to prevent infinite loop
        push(pop());
     }

     return this; // this is not necesary you can make the method void.
}
public-LinkedStack-reversed(){
int size=size();
而(大小-->0){//可防止无限循环
推(pop());
}
返回此;//这不是必需的,您可以使该方法无效。
}

问题是什么?“我需要这个链接到这个堆栈的反向副本。”-这句话对我来说毫无意义。这个问题的措辞让我想起了Nuthin'but a'G Thang(经典美国说唱歌曲)的歌词。无论如何,目标是创建一个能够返回自身反向版本的自定义堆栈吗?我想创建一个使用该类创建的对象的副本。我希望该类创建的对象引用该对象。@Vidya。我不太喜欢rap,但是是的,目标是“创建一个能够返回自身反向版本的自定义堆栈”。运行了一些测试,object.reversed()未返回任何内容。我想复制使用该类创建的对象。我希望该类创建的对象在从该对象中弹出所有项后引用该对象。@pyler-我仍然不明白。(你真的需要学习如何写清晰准确的英语。这是成为一名成功程序员的先决条件!)@Pyrer我真的没有得到你想要的,我理解的是你想要修改你的实例。我想要它返回一个堆栈正如我理解你的问题,你想要修改你的实例,如果你想要返回它,就返回它,但这不是必须的。