Question

In: Computer Science

In java please, thank you :) For this assignment, you will continue your practice with dynamic...

In java please, thank you :) For this assignment, you will continue your practice with dynamic structures. Specifically, you will be implementing a different version of a linked-list, called a Queue. Queues are very important and useful linear structures that are used in many applications. Follow the link below for a detailed description:

tutorialspoint - Queue (Links to an external site.)

The queue description above uses an array implementation for examples. As usual you will be not be using arrays, you will be using linked-lists. Specifically a singly-linked list.

Our job will be to implement the four basic queue methods which are enqueue(), dequeue(), peek() and isEmpty() as described in the tutorial mentioned above. You will not need to implement the isFull() method since we will be using linked-lists and linked-lists are theoretically never full.

For this assignment we will need to implement an uncommon version of enqueue(). In our version, if the element that is being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue.

Additionally, you will be implementing a special kind of queue called a circular queue. A circular queue is a queue where the last position is connected back to the first position to make a circle. Therefore it does not have the traditional front and back data elements. It only has one data element, called rear that keeps track of the last element in the queue.

This structurally similar to a circular singly-linked list described here:

tutorialspoint - Circular Linked List (Links to an external site.)

The program's input will a single string that consists of single-character elements. You program will process each character in the string according the descriptions below. Where "->" signifies the front of the queue (blue designates input, red designates output):

  • 'A' - 'Z': Puts the character on the queue.
    ABCD
    (nothing will be outputted)
    
    ABCDA* 
    B C D A
  • '*': displays the elements of the queue separated by a space.
    ABCD*
    -> A B C D
    
  • '$': prints the element at the front of the queue.
    ABCD$
    peek: A 
    
  • '#': empties out the queue.
    ABCD#*
    -> 
    
  • '!': deletes an element from the queue.
    ABCD!*
    -> B C D
    
  • all other characters: prints an error message for each element, does not put the element in the queue and clears the queue.
    a
    The illegal character 'a' was encountered in the input stream. 

Programming Notes:

  1. You can not delete any code in the Queue class.
  2. If you need additional methods you must add them to the MyQueue class.
  3. If you do not need any additional methods then you can delete the myQueue class.
  4. You must use a singly-linked list with only a rear pointer.
  5. Do not assume any maximum length for any input string.
  6. You need not test for the null or empty string ("") cases.

Programming Rules:

  1. You are not allowed to add any arrays or ArrayLists or any Java built-in (ADTs), such as Lists, Sets, Maps, Stacks, Queues, Deques, Trees, Graphs, Heaps, etc. Or add any class that inherits any of those ADTs.
  2. For your node and list class you can use the code that was used in the book, video and lecture notes related to the node and lists class examples.
  3. You are not allowed to use Java Generics.
  4. If hard-coding detected in any part of your solution your score will be zero for the whole assignment.  

Please use started code:

import java.util.Scanner; // Import the Scanner class

public class Homework7 {
  
public static void main(String[] args) {
// place your solution here
}

}

class SLLNode {
  
public char data;
public SLLNode next;

public SLLNode(char c) {
data = c;
next = null;
}

}

class Queue {
  
public SLLNode rear;
  
public Queue() {
// place your solution here
}
  
public void enqueue(char c) {
// place your solution here
}
  
public SLLNode dequeue() {
// place your solution here
}
  
public char peek() {
// place your solution here
}
  
public boolean isEmpty() {
// place your solution here
}

}
  

Solutions

Expert Solution

The java code to implement the above functionality is :

Queue.java

class Queue {
  
   public SLLNode rear;
  
     public Queue() {
           rear = null;
     }
  
     public void enqueue(char c) {
         SLLNode s = new SLLNode(c); 
         if(rear==null){
                 rear=s;
                 rear.next=rear;
         }else{
           s.next = rear.next;
           rear.next=s;
           rear = s;
         } 
     }
  
     public SLLNode dequeue() {
         SLLNode delete=null;
         if(isEmpty())
                 System.out.println("No elements in Queue to delete");
         else{
                 delete = rear.next;
                 rear.next = delete.next;
         }
         return delete;
     }
  
     public char peek() {
          return rear.next.data;
     }
  
     public boolean isEmpty() {
        if(rear==null)
                return true;
        return false;
     }
     
     public void display(){
         SLLNode s = rear;
         if(isEmpty()){
                 System.out.println("Queue is empty");
         }else{
                 System.out.print("->");
         do{
                 s=s.next;  
                 System.out.print(" "+s.data);                                  
         }while(s!= rear);
         System.out.println();
        }
     }
}

Homework7.java

import java.util.Scanner;


public class Homework7 { 
                public static void main(String[] args) {
                        Scanner sc = new Scanner(System.in);
                        System.out.println("Enter String");
                        String input = sc.nextLine();
                        
                        Queue q = new Queue();
                        for(int i=0;i<input.length();i++){
                                char c = input.charAt(i);
                                if(Character.isLetter(c))
                                        q.enqueue(c);
                                else if(c=='*')
                                        q.display();
                                else if(c=='$')
                                        System.out.println(q.peek());
                                else if(c=='#')
                                        q.rear=null;
                                else if(c=='!')
                                        q.dequeue();
                                else
                                        System.out.println("You have provided invalid input");                  
                                
                                }
                                
                        }
                        
                        
}

The ouput screenshot is :

If you have any queries reagrding this answer, please reach out through the comment section.


Related Solutions

In Java please. Thank you! Recursion For this assignment you are going to write six different...
In Java please. Thank you! Recursion For this assignment you are going to write six different methods. Each method is to be written recursively. Any method that is written iteratively will not receive any credit, even if it is correct and produces the same results or output. You will be given a starter file. You are not allowed to change the signatures of any of the given methods. You are not allowed to add any methods to your solutions. Write...
Question: Can I get the code in Java for this assignment to compare? Please and thank you....
Question: Can I get the code in Java for this assignment to compare? Please and thank you. Can I get the code in Java for this assignment to compare? Please and thank you. Description Write a Java program to read data from a text file (file name given on command line), process the text file by performing the following: Print the total number of words in the file. Print the total number of unique words (case sensitive) in the file. Print...
For this lab you will continue your dynamic array by completing the class called MyDynamicArray. The...
For this lab you will continue your dynamic array by completing the class called MyDynamicArray. The MyDynamicArray class should manage the storage of an array that can grow and shrink. The public methods of your class should already be the following: MyDynamicArray(); Default Constructor. The array should be of capacity 2. MyDynamicArray(int s); For this constructor the array should be of capacity and size s. int& operator[](int i); Traditional [] operator. Should print a message if i is out of...
For this assignment, you will continue researching your chosen company. Compose a paper on the financials...
For this assignment, you will continue researching your chosen company. Compose a paper on the financials of your chosen firm; you will choose two ratios from each category (liquidity, profitability, and solvency) and conduct an analysis as well as highlight key areas/trends of the income statement and balance sheet. Be sure to answer the following questions in your response: • What do the ratios reflect? • Does the company appear healthy and headed in the right direction? Explain. • How...
in Java please For this assignment you are to write a class that supports the addition...
in Java please For this assignment you are to write a class that supports the addition of extra long integers, by using linked-lists. Longer than what is supported by Java's built-in data type, called long. Your program will take in two strings, consisting of only digits, covert each of them to a linked-list that represents an integer version on that string. Then it will create a third linked-list that represents the sum of both of the linked lists. Lastly, it...
COSC 1436 Programming Assignment : Skill Practice Assignment 3 Your skill practice assignment this week will...
COSC 1436 Programming Assignment : Skill Practice Assignment 3 Your skill practice assignment this week will be programming challenge #6 on page 374 - Kinetic Energy In it you are asked to write a programing that returns the amount of kinetic energy the object has. You will ask the user to enter values for mass and velocity. You will need to create a function named kinetic Energy that accepts an object's mass (in kilograms) and velocity (in meters per second)...
Hi, please could someone be of assistance with this assignment for me. Thank you The objective...
Hi, please could someone be of assistance with this assignment for me. Thank you The objective of this assignment is to practice the use of control objects in GUI design. You are required to create a 4- function calculator. See pic below. Calculator.png The calculator should have buttons for all 10 numbers, the decimal point, the four operations (+,-,/,*), an equal “=” button, a "main display" (TextBox or RichTextBot), a backspace button and a clear “C” button. When you create...
Please answer this two part question. Thank you! For this assignment you must write the following...
Please answer this two part question. Thank you! For this assignment you must write the following functions in Scheme: 2.1 Write a recursive function called eval-poly that takes a list of numbers representing the coefficients of a polynomial and a value for ? and evaluates the polynomial for the given value of ?. The list of coefficients should start with the term of lowest degree and end with the term of highest degree. If any term of intermediate degree is...
Please do this in java program. In this assignment you are required to implement the Producer...
Please do this in java program. In this assignment you are required to implement the Producer Consumer Problem . Assume that there is only one Producer and there is only one Consumer. 1. The problem you will be solving is the bounded-buffer producer-consumer problem. You are required to implement this assignment in Java This buffer can hold a fixed number of items. This buffer needs to be a first-in first-out (FIFO) buffer. You should implement this as a Circular Buffer...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT