Question

In: Computer Science

JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...

JAVA DATA STRUCTURE (Linked Lists/Queue)

public class Node {
   int value;
   Node nextNode;

   Node(int v, Node n){
       value = v;
       nextNode = n;
   }

   Node (int v){
       this(v,null);
   }
}

public class Stack {
   protected Node top;

   Stack(){
       top = null;
   }

   boolean isEmpty(){
       return( top == null);
   }
   void push(int v){
       Node tempPointer;
       tempPointer = new Node(v);
       tempPointer.nextNode = top;
       top = tempPointer;
   }
   int pop(){
       int tempValue;
       tempValue = top.value;
       top = top.nextNode;
       return tempValue;
   }
   void printStack(){
       Node aPointer = top;
       String tempString = "";
       while (aPointer != null)
       {
           tempString = tempString + aPointer.value + "\n";
           aPointer = aPointer.nextNode;
       }

       System.out.println(tempString);
   }
}

public class StackWithLinkedList {
   public static void main(String[] args){
       int popValue;
       Stack myStack = new Stack();
       myStack.push(5);
       myStack.push(7);
       myStack.push(9);
       myStack.printStack();
       popValue = myStack.pop();
       popValue = myStack.pop();
       myStack.printStack();
   }
}

Add the Queue class to the program introduced above. New Class should be CREATED (-->NOT importing<-- ANY TYPE of CLASS from Java Library, just using the provided classes), You should create Queue class like we created the Stack class(see the CODE above). Create the Queue class with the following specifications:

a. It must create a queue(constructor)

b. Insert one end (enqueue)

c. Do extractions at the other end (dequeue)

d. Determine if queue is empty (isEmpty)

e. Print the content of the queue (printQueue)

f. Finally, you must develop a main class QueueWithLinkedList that proves that every method works (just like we did with the StackWithLinkedList class provided in the program above)

Solutions

Expert Solution

Code for Queue is provided below alongwith the output screenshot. Code is Explained in code comments. If need any further clarification please ask in comments.

############################################################################

CODE-->

//node for storing elemnts 
class Node {
   int value; //to store data
   Node nextNode;  //to store next node
   //constructor
   Node(int v, Node n){
       value = v;
       nextNode = n;
   }
   //constructor
   Node (int v){
       this(v,null);
   }
}
//Queue class
class Queue
{
        protected Node front,rear; //Node as data member
        
        Queue() //constructor
        {
                front=null; //empty Queue has fron and rear null
                rear=null;
        }
        //function to check if Queue is empty
        boolean isEmpty()
        {
                return(front==null); //if yes return true
        }
        //add elemnt in queue
        void enqueue(int value)
        {
                Node tempPointer=new Node(value); //maling node
                
                if(isEmpty()) //checking if Queue ise empty
                {
                        front=rear=tempPointer;  //assigning front=rear a newnode
                        return;
                }
                
                rear.nextNode=tempPointer;  //if there are elemnts in Queue then add this node to the last
                rear=tempPointer;
        }
        //function of deltion of element
        int dequeue()
        {
                Node temp=front;
                if(!isEmpty())  //if Queue is not empty
                {
                        front=front.nextNode;  //set fron of node to next of front node
                }
                
                if(front==null)  //if the Queue has become Empty
                        rear=null;  //then set rear null
                
                return temp.value;  //retrurn popped value
        }
        //function to print Queue
        void printQueue(){
               Node aPointer = front;
               String tempString = "";
               while (aPointer != null)
               {
                   tempString = tempString + aPointer.value + "\n";
                   aPointer = aPointer.nextNode;
               }

               System.out.println(tempString);
           }
}

//main class
public class QueueWithLinkedList {

        public static void main(String[] args) 
        {
                Queue queue=new Queue();  //creating object of Queue
                System.out.println("IS Queue Empty: "+queue.isEmpty());  //checking if empty
                queue.enqueue(5);
                queue.enqueue(10); //adding elements
                queue.enqueue(15);
                queue.enqueue(20);
                queue.enqueue(25);
                queue.enqueue(30);
                System.out.println("After Adding Elements, IS Queue Empty: "+queue.isEmpty());
                System.out.println("Queue is : ");
                queue.printQueue(); 
                System.out.println("Deleteing Element from Queue: "+queue.dequeue());  //deleting element
                System.out.println("Queue After Deletion: ");
                queue.printQueue();  //printing Queue

        }

}

####################################################################################

OUTPUT


Related Solutions

Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
class nodeType                    // class used to implement a node { public:         int data;   &n
class nodeType                    // class used to implement a node { public:         int data;                        // data member of node         nodeType * next;        // pointer member of node }; int main() {         int x;         nodeType * head =________ ;                     // initialize head pointer         nodeType * tail = _______ ;                        // initialize tail pointer _______ * p;                                                 // create an auxiliary pointer to a node         for (x = 0; x < 10; ++x)         {                 p =   _________ nodeType; // create a node ___________ = x + 10;                                // store...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node...
Use a priority queue to simulate prioritized jobs Priority Queue class Queue Class Node Class (Node will have a 4 digit job number and a priority (A, B, etc) with A highest priority In the driver Create a priority queue object Add 3 jobs of 'B' priority Add 4 jobs of 'D' priority Add 2 jobs of highest priority Print the queue
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
2.1 Linked Lists Linked lists are an example of an unbound data structure. Whereas the array...
2.1 Linked Lists Linked lists are an example of an unbound data structure. Whereas the array is based around having a fixed size per array creation, there is no logical limit on the ability of a linked list to grow. Physical limitations aside, linked lists are capable of growing largely without any limitations. To achieve this, they trade easier access to their individual elements. This is because linked lists have to be traversed from their root node to any node...
What is a linked data structure? What is a node? What are the benefits of linked...
What is a linked data structure? What is a node? What are the benefits of linked structure? What are the drawbacks of linked structure? What are the differences between singly linked and doubly linked structures? Give examples of when a linked structure could be used.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT