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;
}
/* Function to set link to next Node */
public void setLink(Node n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(int d)
{
data = d;
}
/* Function to get link to next node */
public Node getLink()
{
return link;
}
/* Function to get data from current Node */
public Object getData()
{
return data;
}
}
/* Class linkedQueue */
class linkedQueue
{
protected Node front, rear;
public int size;
/* Constructor */
public linkedQueue()
{
front = null;
rear = null;
size = 0;
}
/* Function to check if queue is empty */
public boolean isEmpty()
{
return front == null;
}
/* Function to get the size of the queue */
public int getSize()
{
return size;
}
/* Function to insert an element to the queue */
public void insert(Object data)
{
Node nptr = new Node(data, null);
if (rear == null)
{
front = nptr;
rear = nptr;
}
else
{
rear.setLink(nptr);
rear = rear.getLink();
}
size++ ;
}
/* Function to remove front element from the queue */
public Object remove()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
Node ptr = front;
front = ptr.getLink();
if (front == null)
rear = null;
size-- ;
return ptr.getData();
}
/* Function to check the front element of the queue */
public Object peek()
{
if (isEmpty() )
throw new NoSuchElementException("Underflow Exception");
return front.getData();
}
/* Function to display the status of the queue */
public void display()
{
System.out.print("\nQueue = ");
if (size == 0)
{
System.out.print("Empty\n");
return ;
}
Node ptr = front;
while (ptr != rear.getLink() )
{
System.out.print((String)ptr.getData()+" ");
ptr = ptr.getLink();
}
System.out.println();
}
}
/* Class LinkedQueueImplement */
public class LinkedQueueImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedQueue */
linkedQueue lq = new linkedQueue();
/* Perform Queue Operations */
System.out.println("Linked Queue Test\n");
lq.insert("java");
lq.insert("python");
lq.insert("html");
lq.insert("CSS");
System.out.println("Removed Element = "+ lq.remove());
System.out.println("Peek Element = "+ lq.peek());
System.out.println("Empty status = "+ lq.isEmpty());
System.out.println("Size = "+ lq.getSize());
/* display queue */
lq.display();
}
}
public MyQueue() {
data = new
Object[DEFAULT_SIZE];
}
public MyQueue(int size) {
data = new Object[size];
}
public boolean isEmpty() {
return index == 0;
}
public boolean isFull()
{
if(index == data.length - 1)
return true;
else
return false;
}
public void enqueue(Object obj) throws Exception
{
if (isFull()) {
throw new
Exception("Queue is full. Dequeue some objects");
}
System.out.print("\nEnqueue: "+(String)obj);
this.data[index] = obj;
this.index++;
}
public Object dequeue() throws Exception {
if (isEmpty())
throw new
Exception("Queue is empty");
Object obj = this.data[0];
for (int i = 0; i < this.index -
1; i++) {
data[i] = data[i
+ 1];
}
System.out.print("\nDequeue: "+data[index]);
this.index--;
return obj;
}
Object peek()
{
if(!isEmpty())
return this.data[0];
return null;
}
int getSize()
{
return this.index;
}
void print()
{
if(isEmpty())
{
System.out.println("\nEmpty Queue!");
return;
}
System.out.print("\nQueue Data: ");
for(int i=0; i<index; i++)
{
System.out.print(" "+this.data[i]);
}
}
}
class test
{
//main driver method
public static void main(String[] args) throws Exception
{
MyQueue queue = new MyQueue();
queue.enqueue("java");
System.out.println(queue.dequeue());
queue.enqueue("python");
queue.enqueue("xml");
queue.enqueue("css");
System.out.println(queue.dequeue());
queue.enqueue("lisp");
queue.print();
queue.dequeue();
queue.print();
queue.enqueue("jason");
queue.print();
}
}
code 1
public class MyQueue { //create class MyQueue
public static final int DEFAULT_SIZE = 10; //declare a variable and fix its value to 10
private Object data[]; //create an object array with Object type
private int index; //integer variable declare
code 2
package test;
import java.util.*;
/* Class Node */
class Node
{
protected Object data; //declare variable data of type Object
protected Node link; //declare variable link of type Node class
/* Constructor */
public Node() //default constructor
{
link = null; //assigning values to variables
data = 0;
}
/* Constructor */
public Node(Object d,Node n) //parameterized constructor
{
data = d; //assigning values to variables
link = n;
}
/* Function to set link to next Node */
public void setLink(Node n) //mutator to set link
{
link = n;
}
/* Function to set data to current Node */
public void setData(int d) //mutator to set data
{
data = d;
}
/* Function to get link to next node */
public Node getLink() //Accessor to get link
{
return link;
}
/* Function to get data from current Node */
public Object getData() //Accessor to get data
{
return data;
}
}
/* Class linkedQueue */
class linkedQueue //create class
{
protected Node front, rear; //declare objects of class Node
public int size; //declare size variable
/* Constructor */
public linkedQueue() //default constructor
{
front = null; //initialize variables
rear = null;
size = 0;
}
/* Function to check if queue is empty */
public boolean isEmpty()
{
return front == null; //return either true or false
}
/* Function to get the size of the queue */
public int getSize()
{
return size; //return size
}
/* Function to insert an element to the queue */
public void insert(Object data)
{
Node nptr = new Node(data, null); //create object of class Node and passing values
if (rear == null) //check either its first insertion
{
front = nptr; //assign front and rear to first insertion
rear = nptr;
}
else //otherwise
{
rear.setLink(nptr); //insert at rear
rear = rear.getLink(); //get the link
}
size++ ; //increase the size of queue
}
/* Function to remove front element from the queue */
public Object remove()
{
if (isEmpty() ) //check queue is empty
throw new NoSuchElementException("Underflow Exception");
Node ptr = front; //assign front to ptr
front = ptr.getLink(); //get link of front
if (front == null) // if there is only one element to delete
rear = null; //make rear null
size-- ; //decrease size
return ptr.getData(); //return deleted data
}
/* Function to check the front element of the queue */
public Object peek()
{
if (isEmpty() ) //check queue is empty
throw new NoSuchElementException("Underflow Exception");
return front.getData(); //return front of queue
}
/* Function to display the status of the queue */
public void display()
{
System.out.print("\nQueue = "); //display queue
if (size == 0) //if size is zero
{
System.out.print("Empty\n"); //queue is empty
return ;
}
Node ptr = front; //assign front to ptr
while (ptr != rear.getLink() ) //while link is not null
{
System.out.print((String)ptr.getData()+" "); //display data
ptr = ptr.getLink(); //move to next element
}
System.out.println(); //create a line gap
}
}
/* Class LinkedQueueImplement */
public class LinkedQueueImplement //create a class
{
public static void main(String[] args) //main method
{
Scanner scan = new Scanner(System.in); //Scanner for taking input from user
/* Creating object of class linkedQueue */
linkedQueue lq = new linkedQueue(); // create object
/* Perform Queue Operations */
System.out.println("Linked Queue Test\n"); //display message
lq.insert("java"); //call insert method with string data
lq.insert("python");
lq.insert("html");
lq.insert("CSS");
System.out.println("Removed Element = "+ lq.remove()); //call remove element method from front
System.out.println("Peek Element = "+ lq.peek()); //display front element
System.out.println("Empty status = "+ lq.isEmpty()); //display queue is empty
System.out.println("Size = "+ lq.getSize()); //display size
/* display queue */
lq.display();
}
}
public MyQueue() { //constructor of MyQueue
data = new Object[DEFAULT_SIZE]; //assign value to data
}
public MyQueue(int size) { //parameterized constructor
data = new Object[size]; //assign value to data
}
public boolean isEmpty() { //check queue is empty
return index == 0; //return true or false
}
public boolean isFull() //check queue is full
{
if(index == data.length – 1) //if index is equal to length of data
return true; //return true
else
return false; //return false
}
public void enqueue(Object obj) throws Exception { //insert element in queue
if (isFull()) {
throw new Exception("Queue is full. Dequeue some objects"); //if full then throw exception
}
System.out.print("\nEnqueue: "+(String)obj); //display data
this.data[index] = obj; //assign value
this.index++; // increase index
}
public Object dequeue() throws Exception { //delete element from queue
if (isEmpty())
throw new Exception("Queue is empty"); //throw exception
Object obj = this.data[0]; //remove element from front
for (int i = 0; i < this.index - 1; i++) {
data[i] = data[i + 1]; //shifting elements a position ahead
}
System.out.print("\nDequeue: "+data[index]); //display deleted element
this.index--; //decrease size
return obj;
}
Object peek() //get front of queue
{
if(!isEmpty()) //check queue is not empty
return this.data[0]; //return front element
return null; //return link null
}
int getSize() //get size
{
return this.index; //return size
}
void print() //display queue
{
if(isEmpty()) //check is empty
{
System.out.println("\nEmpty Queue!"); //display queue is empty
return;
}
System.out.print("\nQueue Data: "); //display message
for(int i=0; i<index; i++) //loop to display values
{
System.out.print(" "+this.data[i]); //display data
}
}
}
class test //test class
{
//main driver method
public static void main(String[] args) throws Exception //main method which can throw exception
{
MyQueue queue = new MyQueue(); //create pbject
queue.enqueue("java"); //insert data
System.out.println(queue.dequeue()); //delete data and display it
queue.enqueue("python"); //insert data
queue.enqueue("xml");
queue.enqueue("css");
System.out.println(queue.dequeue()); //delete data and display it
queue.enqueue("lisp"); //insert data
queue.print(); //print queue
queue.dequeue(); //remove element from queue
queue.print(); //print queue
queue.enqueue("jason"); //insert queue
queue.print(); //print queue
}
}
Get Answers For Free
Most questions answered within 1 hours.