Question: Write a method public static void reverse(LinkedList strings) that reverses the entries in a linked list. Not working import java.util.LinkedList; import java.util.Stack; public class ReverseLinked

Write a method public static void reverse(LinkedList strings) that reverses the entries in a linked list.

Not working

import java.util.LinkedList;

import java.util.Stack;

public class ReverseLinked {

public static void main(String[] args) {

LinkedList employeeName = new LinkedList<>();

//add elements into the list by invoking add method

employeeName.add("Thor");

employeeName.add("Rogers");

employeeName.add("Stark");

employeeName.add("Banner");

System.out.println("linked list: " +employeeName);

reverse(employeeName);

}

public static void reverse(LinkedList strings)

{

//getting size of linked list by invoking size method of linked list

int size = strings.size();

Stack st = new Stack();

// loop for pushing element into stack from liked list

for (int i = 0; i < size; i++)

{

//adding element into stack by invoking push method

st.push(strings.get(i));

} // end for

strings.clear();

//run a loop till the stack is empty

while (!st.isEmpty())

{

//pop element from stack and add it into another linked list

strings.add((String) st.pop());

} //end while

System.out.println("linked list in reverse order: " + strings);

} // end reverse

//end ReverseLinked

}

This is the Error that come up when I compile my program.

/tmp/java_BMKtrs/ReverseLinked.java:45: warning: [unchecked] unchecked call to push(E) as a member of the raw type Stack st.push(strings.get(i)); ^ where E is a type-variable: E extends Object declared in class Stack 1 warning

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!