【问题标题】:Returning a copy of an object stack in java在java中返回对象堆栈的副本
【发布时间】:2013-10-07 01:27:18
【问题描述】:

我有一个堆栈方法应该返回 thisreversed *副本*目的。 我需要 this 对象链接到 that 对象。谢谢。

更新

为了澄清, 创建的 that 堆栈对象推送从 this 对象弹出的项目。 我希望 this objectthis object 变空后引用 that object。我真正想要的是返回 this 对象的反向副本。清楚吗?

public LinkedStack<E> reversed()
{
    LinkedStack<E> that= new LinkedStack<E>();
    if(this.isEmpty()){
        return this;
    }
    else{
        while(top!=null)
            {
                that.push(pop());
            }
        }
    return this;
    }

全班

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

【问题讨论】:

  • 问题是什么?
  • “我需要这个链接到那个,这个堆栈的反向副本。” - 这句话对我来说毫无意义。
  • 这个问题的措辞让我想起了 Nuthin' 的歌词,而是一首 'G' Thang(经典的美国说唱歌曲)。无论如何,目标是创建一个能够返回自身反向版本的自定义堆栈吗?
  • 我想复制使用该类创建的对象。我希望类创建的对象引用该对象。
  • @Vidya。我不是很喜欢说唱,但是是的,目标是“创建一个能够返回自身反向版本的自定义堆栈。”

标签: java list linked-list stack reverse


【解决方案1】:

while(top!=null) 更改为while(!this.isEmpty())

【讨论】:

  • 做到了。跑了一些测试,object.reversed() 什么也没返回
【解决方案2】:

在修改实例时,您无需创建辅助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.
}

【讨论】:

  • 我希望它返回一个堆栈
  • 据我了解你的问题,你想修改你的实例,如果你想退回它,退回它,但不是必须的。
【解决方案3】:

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

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

正确的解决方案将取决于 LinkedStack 类的内部细节......你没有向我们展示。

【讨论】:

  • 我想复制使用该类创建的对象。我希望该类创建的对象在从中弹出所有项目后引用该对象。
  • @pyler - 我还是不明白。 (你真的需要学习如何写出清晰准确的英语。这是成为一名成功程序员的先决条件!)
  • @pyler 我真的不明白你想要什么,我理解的是你想修改你的实例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-21
  • 1970-01-01
  • 2016-04-08
  • 2021-03-05
  • 2011-11-25
相关资源
最近更新 更多