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
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your...
IN JAVA Language- Singly Linked List Implementation Implement a Linked List in your language. Use your Can class. You need to create a driver that makes several Can objects and places them in alphabetical order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. NOT USE your language's Library List . You will receive zero points. Write a LinkedList class. Include...
Code in Java Create a queue class to store integers and implement following methods: 1) void...
Code in Java Create a queue class to store integers and implement following methods: 1) void enqueue(int num): This method will add an integer to the queue (end of the queue). 2) int dequeue(): This method will return the first item in the queue (First In First Out). 3) void display(): This method will display all items in the queue (First item will be displayed first). 4) Boolean isEmpty(): This method will check the queue and if it is empty,...
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5. Specifically, write a class LinkedListPriorityQ which implements the priority queue methods: boolean isEmpty() void enqueue(int item) int dequeue()...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
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...
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your...
Stack Queue Program Implement a Stack class using the List Class you developed earlier. Test your Stack class by determining if the following strings have matching open and closing ( ) and/or [ ] and/or { }. To test matches, push open (, [, { onto the stack. Input a close char, pop off the stack and see if input matches symbol form stack.   Use the following data to test your code:                               ( )                               [ ] ( )...
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...