Search code examples
liststackmove

How to move from list to stack and vice versa?


I'm having a little bit of trouble with moving from the list to the stack. I believe that I have to utilize the methods within the linkedlist and pass it into the stack parameters somehow.

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
}

Solution

  • It's pretty simple.

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