Question
Write a method public static void reverse(LinkedList strings) that reverses the entries in a linked list. //importing required packages import java.util.LinkedList; import java.util.Stack; //class ReverseLinked
Write a method public static void reverse(LinkedList strings) that reverses the entries in a linked list.
//importing required packages import java.util.LinkedList; import java.util.Stack; //class ReverseLinked public class ReverseLinked { //main method public static void main(String[] args) { //declarating linked list 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); //call the reverse method by passing linked list reverse(employeeName); } //end main // method for reversing the linked list 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(st.pop()); } //end while System.out.println("linked list in reverse order: " + strings); } // end reverse } //end ReverseLinked
These are the errors i get when I compile the program.
/tmp/java_96Dknr/ReverseLinked.java:14: warning: [unchecked] unchecked call to add(E) as a member of the raw type LinkedList employeeName.add("Thor"); ^ where E is a type-variable: E extends Object declared in class LinkedList /tmp/java_96Dknr/ReverseLinked.java:15: warning: [unchecked] unchecked call to add(E) as a member of the raw type LinkedList employeeName.add("Rogers"); ^ where E is a type-variable: E extends Object declared in class LinkedList /tmp/java_96Dknr/ReverseLinked.java:16: warning: [unchecked] unchecked call to add(E) as a member of the raw type LinkedList employeeName.add("Stark"); ^ where E is a type-variable: E extends Object declared in class LinkedList /tmp/java_96Dknr/ReverseLinked.java:17: warning: [unchecked] unchecked call to add(E) as a member of the raw type LinkedList employeeName.add("Banner"); ^ where E is a type-variable: E extends Object declared in class LinkedList /tmp/java_96Dknr/ReverseLinked.java:33: 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 /tmp/java_96Dknr/ReverseLinked.java:40: warning: [unchecked] unchecked call to add(E) as a member of the raw type LinkedList strings.add(st.pop()); ^ where E is a type-variable: E extends Object declared in class LinkedList 6 warnings
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started