Question

1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push(...

1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps:

push( "Jane" );
push( "Jess" );
push( "Jill" );
push( pop() );
push( "Jim" );

String name = pop();
push( peek() );

Write separate programs for each of the data structures Stack, Queue, PriorityQueue, and List. Use the appropriate push(), pop(), peek() for each of the respective ADT's. Use the Java class library of the ADT's as opposed to the author's implementation.

  1. What is in a stack with these steps? (Java class Stack p174)
  2. What is in a queue with these steps? (Java interface Queue p219)   
  3. What is in a priority queue with these steps? (Java class PriorityQueue p229)
  4. What is in a list with these steps? (Java interface List p308)   

2.  (Queue) Use a queue to reverse the order of a Stack s0 = { 1, 22, 333, 4444, 55555 }. (Note: 55555 is the top of the stack.)

3. (Recursion) Write a recursive method for the function t(n) and a main() with n = 5.   

               t(1) = 2

               t(n) = 1 + t(n – 1) for n > 1

4.  (List) Write Java statements at the client level that return the position of a given object in the list myList. Assume that the object is in the list. Use the list myList = { Amy, Elias, Bob, Drew, Aaron, Carol }. Find Drew in position i=3.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

2)

import java.util.LinkedList;

import java.util.Stack;

public class Main {

public static void main(String[] args) {

Stack<Integer> st = new Stack<>();

st.add(1);

st.add(22);

st.add(333);

st.add(4444);

st.add(55555);

  

Queue<Integer> q = new LinkedList<>();

  

while(!st.isEmpty()) {

q.add(st.pop());

}

  

while (!q.isEmpty()) {

st.push(q.remove());

}

System.out.println(st);

}

}

3)

public static int recur(int n) {

if (n == 1) {

return 2;

}

return recur(n-1) + 1;

}

  

public static void main(String[] args) {

System.out.println(recur(5));

}

NOTE: As per Chegg policy I am allowed to answer specific number of questions (including sub-parts) on a single post. Kindly post the remaining questions separately and I will try to answer them. Sorry for the inconvenience caused.

Add a comment
Know the answer?
Add Answer to:
1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push(...
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