-1

オブジェクトの逆の*コピー*を返すことになっているスタック メソッドがあります。オブジェクトにリンクするオブジェクトが必要です。ありがとう。thisthisthat

アップデート

明確にするために、作成されたそのスタック オブジェクトは、このオブジェクトからポップされたアイテムをプッシュします。このオブジェクトが空になった後、このオブジェクトがそのオブジェクト参照するようにし ます。本当に欲しいのは、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
4

3 に答える 3

1

while(top!=null)に変更while(!this.isEmpty())

于 2013-10-07T01:56:46.360 に答える
0

あなたの質問はあまり意味がありません。(そして、以下のあなたのコメントもそうではありません)

this.pop()しかし、操作によっての状態が変化するため、現在のアプローチは間違いなく間違っていthisます。

正しい解決策は、クラスの内部の詳細に依存しLinkedStackます...あなたが私たちに示していない.

于 2013-10-07T01:40:50.970 に答える
0

インスタンスを変更しているため、Auxiliar を作成する必要はありません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.
}
于 2013-10-07T02:18:13.987 に答える