Question

In: Computer Science

java code Add the following methods to the LinkedQueue class, and create a test driver for...

java code

    1. Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class.
    1. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each queued element already provides its own reasonable toString method.
    2. voidremove(intcount)removesthefrontcountelementsfromthe queue; throws QueueUnderflowException if less than count elements are in the queue.
    3. boolean swapStart() returns false if less than two elements are in the queue, otherwise reverses the order of the front two elements in the queue and returns true.
    4. booleanswapEnds()returnsfalseiftherearelessthantwoelementsin the queue, otherwise swaps the ?irst and last elements of the queue and returns true.

Solutions

Expert Solution


public class LinkedQueue {
  
   class DoubleLinkedNode {

       public DoubleLinkedNode next;
       public DoubleLinkedNode prev;
       public int element;
      
       public DoubleLinkedNode(int num){
           next = null;
           prev = null;
           element = num;
       }

   }
  
DoubleLinkedNode front, rear;
  
  

public String toString()
{
   DoubleLinkedNode curr=front;
   String s= "";
   while(curr.next!=null)
   {
       s = s + String.valueOf(curr.element)+" ";
       curr=curr.next;
   }
   s = s + String.valueOf(curr.element);
   return s;
}

public void remove(int count)
{
   count--;
   if(front==null)
       new Error("QueueUnderflowException");
   if(count==0) {
       front = front.next;
          return ;
   }
   DoubleLinkedNode curr=front;
   int i=0;
   while(curr!=null && i!=count-1)
   {
       curr = curr.next;
       i++;
   }
     
   if(i!=count-1)
       new Error("QueueUnderflowException");
   if(curr.next.next==null)
       rear=rear.prev;
         
     
   curr.next = curr.next.next;
   if(curr.next!=null)
       curr.next.prev=curr;
}

public boolean swapStart() {
   if(front==null || front.next==null)
       return false;
     
   DoubleLinkedNode curr=front;
   front = front.next;
   front.prev=null;
   if(front.next!=null)
       front.next.prev=curr;
   curr.next=front.next;
   curr.prev=front;
   front.next=curr;
   if(front.next.next==null)
       rear = front.next;
   return true;
     
}

public boolean swapEnds() {
   if(front==null || front.next==null)
       return false;
   if(front.next.next==null)
       return swapStart(); //when Queue contain two element
   rear.next=front.next;
   rear.prev.next=front;
   front.prev=rear.prev;
   front.next.prev=rear;
   front.next=null;
   rear.prev=null;
   DoubleLinkedNode curr;
   curr=front;
   front=rear;
   rear=curr;
   return true;
     
}
  

}


Related Solutions

28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each...
28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the ArrayBoundedStack, not by calling the previously defined public methods of the class. a. String toString()—creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
//Complete the incomplete methods in the java code //You will need to create a driver to...
//Complete the incomplete methods in the java code //You will need to create a driver to test this. public class ManagedArray { private int[] managedIntegerArray; //this is the array that we are managing private int maximumSize; //this will hold the size of the array private int currentSize = 0; //this will keep track of what positions in the array have been used private final int DEFAULT_SIZE = 10; //the default size of the array public ManagedArray()//default constructor initializes array to...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static...
PLEASE CODE THIS IN JAVA Create a driver class Playground that contains the function, public static void main(String[] args) {}. Create 2 SportsCar and 2 Airplane instances using their constructors. (SPORTSCAR AND AIRPLANE CLASSES LISTED BELOW THIS QUESTION. Add all 4 instances into a single array called, “elements.” Create a loop that examines each element in the array, “elements.” If the elements item is a SportsCar, run the sound method and if the item is an Aeroplane, run it’s ChangeSpeed...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following methods to the LinkedStacked class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the LinkedStacked, not by calling the previously defined public methods of the class. - String toString()—creates and returns a string that correctly represents the current stack....
In this class add Comparable interface. In the driver program create a few objects and In...
In this class add Comparable interface. In the driver program create a few objects and In the driver program create a few objects and compare them . then create a list of those objects and sort them .A Quadratic is bigger than another Quadratic if it opens faster package pack2; /** * This is a program for defining a quadratic equation * @author sonik */ public class Quadratic { public int coeffX2 = 0; public int coeffX = 0; public...
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods....
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods. 2. Create another class named ListFlower. This class manages a collection of Flower (may be LinkedList) named a. Implementing some methods for ListFlower: Add: add new item of Flower to a Display: display all items of a sort(): sort as descending by Price and display all items of a search(Flower f): check and return whether f is exists in a or not. delete(int pos):...
Add code (see below for details) to the methods "set" and "get" in the following class,...
Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index] =...
Create a (partial) BST class and a driver program to test it. The tree node will...
Create a (partial) BST class and a driver program to test it. The tree node will store integers as the data/key field (single field). Note that you will need to guarantee there are no duplicates in your insert function (the tree should refuse to insert a duplicate key). Call your files “tree.h”, “tree.cpp” and “main.cpp”. In addition, draw a picture of your tree (see note about random values below) Public methods to include: Constructor Copy Constructor Overloaded Assignment Operator Destructor...
In Java, using the code provided for Class Candle, create a child class that meets the...
In Java, using the code provided for Class Candle, create a child class that meets the following requirements. Also compile and run and show output ------------------------------------------------------------------------ 1. The child class will be named  ScentedCandle 2. The data field for the ScentedCandle class is:    scent 3. It will also have getter and setter methods 4. You will override the parent's setHeight( ) method to set the price of a ScentedCandle object at $3 per inch (Hint:   price = height * PER_INCH) CODE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT