Question

The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...

The language is Java.

Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element 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 must implement a circular queue. The program's input will be a single string that consists of single-character elements. You program will process each character in the string according the descriptions below. "->" signifies the front of the queue.

1) 'A' - 'Z': Puts the character on the queue.

Input: ABCD Output: nothing will be outputted

Input: ABCDA* Output: B C D A

2) '*': displays the elements of the queue separated by a space

Input: ABCD* Output: -> A B C D

3) '$': prints the element at the front of the queue

Input: ABCD$ Output: peek: A

4) '#': empties out the queue

Input: ABCD#* Output: ->

5) '!': deletes an element from the queue

Input: ABCD!* Output: -> B C D

6) all other characters: prints an error message for each element, does not put the element in the queue and clears the queue.

Input: a Output: The illegal character 'a' was encountered in the input stream.

Rules:

  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.
  7. 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.
  8. You are not allowed to use Java Generics.

Starter code which must be used, can only be changed if rules stated above allow it:

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

public class Homework1 {
  
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
}

}
  
class MyQueue extends Queue {
// place any additional code you need in this class   
}

Homework Answers

Answer #1

Complete code in java:-

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

public class Homework1 {
  
   public static void main(String[] args) {
       // place your solution here
       // Create Scanner object to take user input.
       Scanner sc = new Scanner(System.in);
       System.out.print("Input: ");
       String input = sc.nextLine();
       // Create empty queue.
       Queue queue = new Queue();
       System.out.print("Output: ");
       // This for loop will parse the input string
       // And will take appropriate action for each character
       for(int i = 0; i < input.length(); i++) {
           char c = input.charAt(i);
           // If Upper case character, push into the queue.
           if(c >= 'A' && c <= 'Z') {
               queue.enqueue(c);
           }
           // Print all elements of the queue.
           else if(c == '*') {
               SLLNode temp = queue.rear;
               do {
                   if(temp == null) {
                       break;
                   }
                   System.out.print(temp.data+" ");
                   temp = temp.next;
               }while(temp != queue.rear);
           }
           // Printing peek element of queue.
           else if(c == '$') {
               System.out.print(queue.peek());
           }
           // Delete queue.
           else if(c == '#') {
               queue.rear = null;
           }
           // Pop out an element from queue.
           else if(c == '!') {
               queue.dequeue();
           }
           else {
               System.out.println("\nThe illegal character "+c+" was encountered in the input stream.");
               break;
           }
       }
       System.out.println();
   }
}

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
       // Initializing rear as 'null', Empty queue.
       this.rear = null;
   }
  
   public void enqueue(char c) {
       // place your solution here
       // If currently queue is empty.
       if(this.rear == null) {
           this.rear = new SLLNode(c);
           this.rear.next = this.rear;
       }
       // If queue is not empty.
       else {
           SLLNode temp = this.rear;
           SLLNode prev = null;
           // If character is already present in queue, remove it.
           do{
               if(temp.data == c) {
                   // If character is same present on top of the queue,
                   // Just put it back into the queue.
                   if(prev == null) {
                       this.rear = this.rear.next;
                       return;
                   }
                   // Delete it, from the queue.
                   else {
                       prev.next = temp.next;
                   }
                   break;
               }
               prev = temp;
               temp = temp.next;
           }while(temp != this.rear);

           // Add this character again in queue, at back.
           temp = new SLLNode(c);
           temp.next = this.rear;
           prev = this.rear;
           while(prev.next != this.rear) {
               prev = prev.next;
           }
           prev.next = temp;
       }
   }
  
   public SLLNode dequeue() {
       // place your solution here
       // If queue size is 1
       // Make rear point to 'null'.
       if(this.isEmpty()) {
           return null;
       }
       SLLNode temp = this.rear;
       if(this.rear.next == this.rear) {
           this.rear = null;
       }
       // Else remove element from the top of the queue.
       else {
           this.rear = this.rear.next;
           SLLNode temp1 = this.rear;
           while(temp1.next != temp){
               temp1 = temp1.next;
           }
           temp1.next = this.rear;
       }
       return temp;
   }
  
   public char peek() {
       // place your solution here
       return this.rear.data;
   }
  
   public boolean isEmpty() {
       // place your solution here
       return (this.rear == null);
   }
}

Screenshot of output:-

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
A Queue is a linked list with pointers to both the head of the list as...
A Queue is a linked list with pointers to both the head of the list as well as the tail (or the last element) of the list. Given a queue Q, Q.head gives the head of the queue and Q.tail gives the tail of the queue. Give O(1) time algorithms for the following tasks. Enqueue • Input: A queue Q of distinct integers and a queue element a not in Q. 1 • Output: Q with the element a added...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
You are required to write a program in JAVA based on the problem description given. Read...
You are required to write a program in JAVA based on the problem description given. Read the problem description and write a complete program with necessary useful comment for good documentation. Compile and execute the program. ASSIGNMENT OBJECTIVES: • To introduce queue data structure. DESCRIPTIONS OF PROBLEM: Exercise : Write a program to reverse element of a stack. For any given word (from input), insert every character (from the word) into a stack. The output from the stack should be...
In JAVA: Write a program that reads an integer, a list of words, and a character....
In JAVA: Write a program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Assume that the list of words will always contain fewer than 20 words. Ex: If the input is: 4 hello zoo sleep drizzle...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty queue,...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
Write code in java Implement a method to build an AVL tree out of a sorted...
Write code in java Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity (sit, walk, jog, bike, swim) and duration in minutes (integer), and returns the estimated calories expended (double). Calories per minute for a 150 lb person (source): sit: 1.4 walk: 5.4 run: 13.0 bike: 6.8 swim: 8.7 If the input is sit 2, the output is 2.8 Hints: Use an if-else statement to determine the calories per minute for the given activity. Return the calories...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT