Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use the class below as the driver for this lab called LinkedListRunner with a ma

ID: 3695693 • Letter: U

Question

Use the class below as the driver for this lab called LinkedListRunner with a main method that instantiates a LinkedList<String>. Add the following strings to the linked list:

import java.util.LinkedList;
import java.util.Stack;
public class StackRunner
{
public static void main(String[] args)
{
LinkedList<String> myLinkedList1 = new LinkedList<String>();
myLinkedList1.add("aaa");
myLinkedList1.add("bbb");
myLinkedList1.add("ccc");
myLinkedList1.add("ddd");
myLinkedList1.add("eee");
// print the first linked list
System.out.println("My Linked List 1:");

// … your code goes here

Stack<String> myStack = new Stack<String>();
//Iterate through elements in the linked list (don't remove them), but
// push all the elements onto the stack

// … your code goes here

//Pop all the stack elements off the stack and add them to
// a new linked list
// … your code goes here

//print the second linked list
System.out.println("My LinkedList 2:");

// … your code goes here
}
}

How is the second list ordered compared to the first? Why?

Explanation / Answer

The second order will be a reversed order of first list, because it goes in stack and pops out in LIFO order. import java.util.LinkedList; import java.util.Stack; class StackRunner { public static void main(String[] args) { LinkedList myLinkedList1 = new LinkedList(); myLinkedList1.add("aaa"); myLinkedList1.add("bbb"); myLinkedList1.add("ccc"); myLinkedList1.add("ddd"); myLinkedList1.add("eee"); // print the first linked list System.out.println("My Linked List 1:"); for(String s : myLinkedList1){ System.out.println(s); } Stack myStack = new Stack(); //Iterate through elements in the linked list (don't remove them), but // push all the elements onto the stack for(String s : myLinkedList1){ myStack.push(s); } //Pop all the stack elements off the stack and add them to // a new linked list LinkedList myLinkedList2 = new LinkedList(); for(int i=0; i