JAVA Need OUTPUT Write a Queue implementation that uses a c

JAVA : Need OUTPUT

Write a Queue implementation that uses a circular linked list, which is the same as a linked list except that no links are null and the value of last.next is first whenever the list is not empty. Keep only one Node instance variable (last).

Solution

class Node
{
   protected String data;
   protected Node link;
  
   public Node()
   {
       data = null;
       link = null;
   }
  
   public Node(String dataValue)
   {
       data = dataValue;
   }
  
   public void setData(String dataValue)
   {
       data = dataValue;
   }
  
   public void setLink(Node linkValue)
   {
       link = linkValue;
   }
  
   public String getData()
   {
       return data;
   }
  
   public Node getLink()
   {
       return link;
   }
}

class CircularQueue
{

   protected Node first;
   protected Node last;
   protected int size;
  
   public CircularQueue()
   {
       first = null;
       last = null;
       size = 0;
   }
  
   public void insert(String data)
   {
       if(size == 0)
       {
           first.setData(data);
           first.setLink(first);
           last = first;
           size++;
       }
       else
       {
           if(size == 1)
           {
               first.setLink(last);
               last.setData(data);
               last.setLink(first);
               size++;
           }
           else
           {
               Node node = new Node(data, first);
               last.setLink(node);
               size++;
           }
       }
   }
  
   public String delete()
   {
       if(size == 0)
       {
           System.out.println(\"\ Queue is empty\");
       }
       else
       {
           if(size == 1)
           {
               first = last;
               last = first;
               size--;
               return first.getData();
           }
           else
           {
               Node node = first;
               first = node.getLink();
               size--;
               last.setLink(node);
               return node.getData();
           }
       }
   }
  
   public void view()
   {
       if(size == 0)
           System.out.println(\"\ Queue is Empty!\");
          
       else
       {
           Node node = first;
           do
           {
               System.out.print(\"\\t node.getData()\");
           }while (node.getLink() ! = first);
       }
   }  
      
}

public class CircularQueueMain
{
   public static void main(String[] args)
   {
       CircularQueue queue = new CircularQueue();
       Scanner scanner = new Scanner(System.in);
       do
       {
           System.out.println(\"\ Queue Operations : \ \ \");
           System.out.println(\"Insert :\ \");
           System.out.println(\"Remove :\ \");
           System.out.println(\"View :\ \");
           System.out.println(\"\ Enter your option :\");
           int choice = scanner.nextInt();
           switch(choice)
           {
               case 1:
                   System.out.println(\"\ Enter element to insert: \");
                   String data = scanner.next();
                   queue.insert(data);
                   queue.view();
                   break;
               case 2:
                   queue.delete();
                   queue.view();
                   break;
               case 3:
                   queue.view();
                   break;
               default:
                   System.out.println(\"\ Enter a valid option\");
                   break;
           }
           System.out.println(\"\ Do you want to continue?\\t<y/n>\ \");
       }while(scanner.next() != \"n\" || scanner.next() != \"N\");
   }
}
      

JAVA : Need OUTPUT Write a Queue implementation that uses a circular linked list, which is the same as a linked list except that no links are null and the value
JAVA : Need OUTPUT Write a Queue implementation that uses a circular linked list, which is the same as a linked list except that no links are null and the value
JAVA : Need OUTPUT Write a Queue implementation that uses a circular linked list, which is the same as a linked list except that no links are null and the value

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site