Question

public class CharNode{ private CharNode link; private char info; public CharNode(char info) { this.info = info;...

public class CharNode{

private CharNode link;

private char info;

public CharNode(char info)

{

this.info = info;

this.link = null;

}

public CharNode(char c, CharNode link)

{

this.info = info;

this.link = link;

}

public CharNode getLink() {

return link;

}

public void setLink(CharNode link) {

this.link = link;

}

public char getInfo() {

return info;

}

public void setInfo(char info) {

this.info = info;

}

}

//Remember Queue is a first-in-first-out data structure

public class CharQueue {

// front is an element that is put into queue the earliest.

CharNode front;

// the number of element in this queue should not exceed max_size

private int max_size;

public CharQueue(int max_size){

this.front = null;

this.max_size = max_size;

}

// Throws OverflowException if this queue is full;

// otherwise, adds element to the rear of this queue.

public void enqueue(char c){

/* 1. fill your code here */

/* 1. end your code */

}

// Throws UnderflowException if this queue is empty;

// otherwise, removes front element from this queue and returns it.

public char dequeue(){

/* 2. fill your code here */

/* 2. end of your code here */

}

// Returns true if this queue is empty; otherwise, returns false.

public boolean isEmpty(){

/* 3. fill your code here */

/* 3. end your code */

}

// Returns true if current size is equal to max_size; otherwise return false;

public boolean isFull(){

/* 4. fill your code here */

/* 4. end your code */

}

// Returns the number of elements in this queue.

public int size(){

/* 5. fill your code here */

/* 5. fill your code here */

}

// transfer the queue to a string and return it.

// if the queue is 'a', 'b', 'c' from the front to rear, the string will be "abc"

public String toString() {

String result = "";

CharNode pointer = this.front;

while(pointer != null) {

result += pointer.getInfo();

pointer = pointer.getLink();

}

return result;

}

}

PLEASE DO NOT INITIALIZE "REAR" OUTSIDE OF THE BLOCKS. ALSO, PLEASE DO NOT ADD ANY ATTRIBUTES TO THE CLASS.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


ANSWER:-

CODE:-

import java.lang.*;
class CharNode{

   private CharNode link;

   private char info;

   public CharNode(char info)

   {

       this.info = info;

       this.link = null;

   }

   public CharNode(char c, CharNode link)

   {

       this.info = info;

       this.link = link;

   }

   public CharNode getLink() {

       return link;

   }

   public void setLink(CharNode link) {

       this.link = link;

   }

   public char getInfo() {

       return info;

   }

   public void setInfo(char info) {

       this.info = info;

   }

}

//Remember Queue is a first-in-first-out data structure

public class CharQueue {

   // front is an element that is put into queue the earliest.

   CharNode front;

   // the number of element in this queue should not exceed max_size

   private int max_size;

   public CharQueue(int max_size){

       this.front = null;

       this.max_size = max_size;

   }

   // Throws OverflowException if this queue is full;

   // otherwise, adds element to the rear of this queue.

   public void enqueue(char c){

   /* 1. fill your code here */

   /* 1. end your code */
       try{
           if (isFull()) {
               throw new Exception("Queue is full");
           }
           CharNode node = new CharNode(c);
           if(front == null){
               front = node;
               return ;
           }
          
           CharNode temp = front;
           while (temp.getLink()!=null) {
               temp = temp.getLink();
           }
           temp.setLink(node);
       }catch (Exception e) {
           System.out.println(e);
       }
   }

   // Throws UnderflowException if this queue is empty;

   // otherwise, removes front element from this queue and returns it.

   public char dequeue(){

   /* 2. fill your code here */

   /* 2. end of your code here */
       char ch = ' ';
       try{
           if(isEmpty())
               throw new Exception("Queue is empty");
           ch = front.getInfo();
           front = front.getLink();
       }catch (Exception e) {
           System.out.println(e);
       }
       return ch;
   }

   // Returns true if this queue is empty; otherwise, returns false.

   public boolean isEmpty(){

   /* 3. fill your code here */

   /* 3. end your code */
       return front == null;

   }

   // Returns true if current size is equal to max_size; otherwise return false;

   public boolean isFull(){

   /* 4. fill your code here */

   /* 4. end your code */
       return this.size() == this.max_size;
   }

   // Returns the number of elements in this queue.

   public int size(){
       int count = 0;
       CharNode temp = front;
       while (temp!=null) {
           count++;
           temp = temp.getLink();
       }
       return count;
   }

   // transfer the queue to a string and return it.

   // if the queue is 'a', 'b', 'c' from the front to rear, the string will be "abc"

   public String toString() {

       String result = "";

       CharNode pointer = this.front;

       while(pointer != null) {

           result += pointer.getInfo();

           pointer = pointer.getLink();

       }

       return result;

   }

   public static void main(String[] args) {
       CharQueue queue = new CharQueue(4);
       queue.enqueue('a');
       queue.enqueue('b');
       queue.enqueue('c');
       queue.enqueue('d');
       System.out.println(queue);
       System.out.println(queue.dequeue()+" is popped.");
       System.out.println("Now, queue is "+queue);
   }
}

NOTE:- If you need any modifications in the code,please comment below.Please give positive rating.THUMBS UP.

             THANK YOU!!!

OUTPUT:-

Add a comment
Know the answer?
Add Answer to:
public class CharNode{ private CharNode link; private char info; public CharNode(char info) { this.info = info;...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Are based on the following Queue class code segment class QueueFull {/* Empty exception class */};...

    Are based on the following Queue class code segment class QueueFull {/* Empty exception class */}; Class Queue Empty {/* Empty exception class */}; struct Node//Node structure int data;//Holds an integer Node* next;//Pointer to next node in the queue}; Class Queue//Linked node implementation of Queue ADT {Private: Node* front;//Pointer to front node of queue Node* rear;//pointer to last node of queue Public: Queue ()://default constructor initializes queue to be empty -Queue ();//Deallocates all nodes in the queue Void Add (int...

  • Use the following implementation of LinkedListNode to write the Stack4T> classi public class LinkListNodecT>T private T...

    Use the following implementation of LinkedListNode to write the Stack4T> classi public class LinkListNodecT>T private T info; private LinkListNode link; public LinkListNode ( T info) this, info = info; link = null; public T getInfo() return info; public LinkListNode getlink() return link; public void setInfo( T info) this.infoinfo; public void setLink (LinklistNode link) this.link = link; public interface StackInterface <T T top() throws StackUnderflowException; void pop () throws StackUnderflowException; void push (T element);

  • Implement the Unsorted, sorted and indexed lists using Linked List base structure. (LLNode class under support...

    Implement the Unsorted, sorted and indexed lists using Linked List base structure. (LLNode class under support package) Files required: 1. ListInterface.java (given) 2. LLUnsortedList.java (implements ListInterface.java) 3. LLSortedList.java (extends LLUnsortedList.java, implements ListInterface.java) 4. LLIndexedList.java (implements LLIndexedListInterface.java, extends LLUnsortedList.java) 5. Driver ** Maybe create LLIndexedListInterface.java file too even though it is not required file! I need the bolded Java Files (5) to be written separate  classes please! The interfaces can be implemented from below! Thank you zip all your files and submit....

  • Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access...

    Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data...

  • Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access...

    Step 1 Develop the following interface: Interface Name: QueueInterface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Plz help me with the code. And here are the requirement. Thanks!! You are required to...

    Plz help me with the code. And here are the requirement. Thanks!! You are required to design and implement a circular list using provided class and interface. Please filling the blank in CircularList.java. This circular list has a tail node which points to the end of the list and a number indicating how many elements in the list. And fill out the blank of the code below. public class CircularList<T> implements ListInterface<T> { protected CLNode<T> tail; // tail node that...

  • Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info...

    Step 2 Develop the following class: Class Name: QueueNode<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier: private Data type: QueueNode<T> Constructors: Name: QueueNode Access modifier: public Parameters: info (data type T) Task: sets the value of this.info to the value of the info parameter sets the value of link to null Methods Name: setInfo Access modifier: public Parameters: info (data type T) Return type: void Task: sets the...

  • Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty...

    Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: Queue Node<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier:...

  • use intellij idea main java wp the professor. Please make sure to only implement what is...

    use intellij idea main java wp the professor. Please make sure to only implement what is asked for. You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here. Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT