In: Computer Science
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):
ABCD (nothing will be outputted) ABCDA* B C D A
ABCD* -> A B C D
ABCD$ peek: A
ABCD#* ->
ABCD!* -> B C D
a The illegal character 'a' was encountered in the input stream.
Programming Notes:
Programming Rules:
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
}
}
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.