如何从列表移动到堆栈,反之亦然?

时间:2015-10-01 17:12:55

标签: list stack move

我从列表移到堆栈时遇到了一些麻烦。我相信我必须利用链表中的方法并以某种方式将其传递给堆栈参数。

public class ProjectOne 
{
public static void main(String [] args)
    {
    //declare variables
    SinglyLinkedList<GameEntry> listOne;
    GameEntry entry;
    LinkedStack<GameEntry> ls;

    //create listOne
    listOne = new SinglyLinkedList<GameEntry> ();

    //populate the lists
    listOne.addFirst(new GameEntry("Michael", 1234));   //#1
    listOne.addFirst(new GameEntry("Henry", 62));   //#2
    listOne.addFirst(new GameEntry("Darryl", 143)); //#3
    listOne.addFirst(new GameEntry("George", 5000));    //#4
    listOne.addFirst(new GameEntry("Malak", 9999)); //#5
    listOne.addFirst(new GameEntry("Olan", 7777));  //#6
    listOne.addFirst(new GameEntry("Gustav", 161)); //#7
    listOne.addFirst(new GameEntry("Agro", 900));   //#8
    listOne.addFirst(new GameEntry("Reinhart", 654321));    //#9
    listOne.addFirst(new GameEntry("Alexander", 111111));   //#10

    //declare a LinkedStack of Generic Type: GameEntry
    ls = new LinkedStack<GameEntry>();

    }   //end main method
}

1 个答案:

答案 0 :(得分:1)

非常简单。

while (listOne.size() > 0)
    ls.push(listOne.removeFirst());