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
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 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...
In java please create a method that will insert a value after a specific node for...
In java please create a method that will insert a value after a specific node for a linked list. public void insertAfter(Node a, int newData){ }
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. .
Write a remove(E val) method for CircularlyLinkedList class This method remove the first occurrence of the...
Write a remove(E val) method for CircularlyLinkedList class This method remove the first occurrence of the node that contains the val.
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO)....
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO). Use the driver to then test each of the methods. Simply download it and place it with the other source files. Create a class GenLLQueue which has the following: Internal class ListNode which contains: Instance variable data of type T Instance variable link of type ListNode Default constructor that sets both instance variables to null Instance Variables head which is of type ListNode which...
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
Write the methods that insert and remove an element at the kth position in Java using...
Write the methods that insert and remove an element at the kth position in Java using recursion (NOT iteration) (Hint for the remove method: we have to recursive position -1 times, and each time we do the recursion, we have to create a method to move the head to the right) public void insertRecursive(int position, int data) { //enter code here } public int removeAtRecursive(int position) { //enter code here } Here is the given class for the Node: public...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT