Question

Java 3) Write the-G-code to convert a linked list of integers, to a stack of integers. Thus, your code will traverse the link
0 0
Add a comment Improve this question Transcribed image text
Answer #1
//LinkedListToStack.java
import java.util.LinkedList;
import java.util.Stack;
public class LinkedListToStack {
    public static void main(String[] args) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);

        // converting linked list to stack
        Stack<Integer> stack = new Stack<>();
        for(int i = 0;i<linkedList.size();i++){
            stack.push(linkedList.get(i));
        }
        while (!stack.isEmpty()){
            System.out.println(stack.pop());
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Java 3) Write the-G-code to convert a linked list of integers, to a stack of integers....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT