Question

In: Computer Science

write a method in java that insert First(Queue<E> s E e)that receives a queue and an...

write a method in java that insert First(Queue<E> s E e)that receives a queue and an elment, it inserts the element at the beginnings of queue

Solutions

Expert Solution

Idea

A queue data structure works on the first-in-first-out (FIFO) logic. It mainly supports 2 methods

  1. push: push the element to the back of the queue.
  2. pop: pop the element from the front of the queue.

Now, since we don't have any method to push or insert an element to the front of the queue, we can use the following idea: Create an empty queue. Add the new element to it's back. Then, push all the elements from the original queue to this new queue by popping them one at a time and push to the new queue. Now, we again pop all the elements from this new queue and push back to the original queue. This way, the new element is now at the front of the original queue.

The code and output are attached below. Helpful comments are also added to the code.

Note: Queue interface is implemented as a LinkedList (or PriorityQueue) in Java. But, the concept remains the same.

Code

import java.util.LinkedList;

public class QueueClass {
  public static void insert(LinkedList<Integer> q, Integer e) {
    // Create an empty queue
    LinkedList<Integer> new_q = new LinkedList<>();
    // Push new element to queue end
    new_q.add(e);
    // Pop all elements from front of original queue and push to end of new queue
    while (q.size() > 0)
      new_q.add(q.removeFirst());
    // Now, pop all elements from front of new queue and push to end original queue
    while (new_q.size() > 0)
      q.add(new_q.removeFirst());
    // Now, the original queue contains the new element at its front.
  }
  public static void main(String[] args) { 
    LinkedList<Integer> queue = new LinkedList<Integer>();
    queue.add(1);
    queue.add(2);
    queue.add(3);
    System.out.println("Elements of queue before insertion " + queue);
    insert(queue, 4);
    System.out.println("Elements of queue after insertion " + queue);
  } 
}

Output


Related Solutions

java method for dequeue write the “dequeue” method for a queue of type double. If the...
java method for dequeue write the “dequeue” method for a queue of type double. If the queue is empty return 0.0. Make sure to change the links properly ensure that the data structure remains a queue. use the code provided below public class Q04 { public class ListNode//Public for testing purposes { public double data; public ListNode link; public ListNode(double aData, ListNode aLink) { data = aData; link = aLink; } } public ListNode head;//Public for testing purposes public ListNode...
Write a non recursive method to insert into an AVL tree in Java
Write a non recursive method to insert into an AVL tree in Java
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
write C program to implement the priority queue with the operation insert
write C program to implement the priority queue with the operation insert
write a method in java for a binary search tree that receives a node as input...
write a method in java for a binary search tree that receives a node as input and returns the successor node.
Write method reverseStack(Stack s) that receives a stack s and reverse the order of its elements....
Write method reverseStack(Stack s) that receives a stack s and reverse the order of its elements. the values inside the stack must be changed, that the top will be the last and so on. please use java code to slove. Thank you.
Write a C program to implement the priority queue with the operations insert and extractmax. Sample...
Write a C program to implement the priority queue with the operations insert and extractmax. Sample : ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 2 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 4 enter any key to go to main menu ====Menu==== insert extractmax display exit Enter your choice: 1 Input a number: 6 enter any key to go to main menu...
Program in Java Write an algorithm to transfer the elements from queue Q1 to queue Q2,...
Program in Java Write an algorithm to transfer the elements from queue Q1 to queue Q2, so that the contents in Q2 will be in reverse order as they are in Q1 (e.g. if your queue Q1 has elements A, B, and C from front to rear, your queue Q2 should have C, B, and A from front to rear). Your algorithm must explicitly use an additional stack to solve the problem. Write your algorithm in pseudo code first, and...
Write a remove(E val) method for DoublyLinkedList class This method remove the first occurrence of the...
Write a remove(E val) method for DoublyLinkedList class This method remove the first occurrence of the node that contains the val. .
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT