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.
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.
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.
1. a. Stack b. Queue c. Priority Queue d. List - (ADTs) Given the following steps: push(...
Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure. Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data...
e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } } f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex...
I already created a doubly linked list class. I need help doing this and adding the doubly linked list class to this. I need this for Java language if someone can please help me Step 2 Stack and Queue Using the linked list class you created in Step 1 create stack and queue classes. Iwill leave it up to you as to whether to use composition or inheritance but whatever way you choose to go you should be able to...
A priority queue is a collection of items each having a priority. A priority queue supports three fundamental operations. You can ask a priority queue whether it is empty. You can insert an item into the priority queue with a given priority. You can remove the item from the priority queue that has the smallest priority. For example, suppose that you start with an empty priority queue and imagine performing the following steps. Insert item "one" with priority 10. Insert...
1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show the Linked List after the following commands are executed: Stack myStack = new Stack(); myStack.push(20); myStack.push(40); myStack.pop(); myStack.push(60); myStack.push(80); 2)If the same commands were used but the Stack was implemented with an Array with maximum size of 10, show what the array would look like after all these commands are executed. Assume O(1) implementation of push and pop here as well. 3)Given a Queue...
In Java language: 1. The complement of a queue is a stack. It uses first-in, last-out accessing and is often likened to a stack of plates. The first plate put on the table is the last plate used. Create a stack class called Stack that can hold characters. Call the methods that access the stack push( ) and pop( ). Allow the user to specify the size of the stack when it is created. Keep all other members of the...
Using Java You are given a Node class and a List class: public class Node { int data; Node next; } public class List { Node first; } You are also given a Stack class. The following functions are available for use: public class Stack { public boolean isEmpty(){}; public void push(int n){}; public int pop(){};} Write a Java method snglyRevToStck that pushes the data found in a linked list t in reverse order into the stack...
JAVA PROGRAM: public class Stack { private DList mList; public Stack() { setList(new DList<>()); } private int indexOfTop() { return getList().isEmpty() ? -1 : getList().getSize() - 1; } public E peek() { return getList().get(indexOfTop()); } public E pop() { return getList().remove(indexOfTop()); } public Stack push(E pData) { getList().append(pData); return this; } private DList getList() { return mList; } private void setList(DList pList) { mList = pList; } Q1 Because Stack encapsulates an instance variable which is of the class DList,...
Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...
java create java program that make stack with LinkedList and stack is implement iterator. When stack’s iterator call next(), it pop its data. here is the example of output //by user 5 1 2 3 4 5 //then output comes like this 5 4 3 2 1 Stack is empty. here is the code that i'm going to use class Stack<T> implements Iterator<T> { LinkedList<T> list; public Stack() { list = new LinkedList<T>(); } public boolean isEmpty() { return list.isEmpty(); ...