Question

In: Computer Science

IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...

IN JAVA LANGUAGE

Linked List-Based Queue Implementation

Implement Queue using a Linked List.

  • Use the language library LinkedList
  • Queue methods will call the LinkedList methods
  • You can use string as the object

Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array implementation, you wrote code to manipulate the array. For this linked list implementation, methods already exist.

Before the underlying implementation of queue was array, now the underlying implementation of queue will be Linked list.

Solutions

Expert Solution

Solution:

I have done your code as stated to implement queue using a linked list.

I had attached all my source code and screenshots for your reference.

NOTE: Create all classes separately in a folder then execute the code.

Souce Code:

1. Node.java

public class Node {

   public String dt;
public Node next;

public Node()
{
next = null;
dt = " ";
}
  
public Node(String d,Node n)
{
dt = d;
next = n;
}
  
public void setnext(Node n)
{
next = n;
}
  
public void setdt(String d)
{
dt = d;
}
  
public Node getnext()
{
return next;
}
  
public String getdt()
{
return dt;
}
}

Screenshot:

2. Queue.java

import java.util.NoSuchElementException;

public class Queue {
  
Node fr, rer;
public int size;

  
public Queue()
{
fr = null;
rer = null;
size = 0;
}
  
public int Size()
{
return size;
}

public void enqueue(String data)
{
Node nptr = new Node(data, null);
if (rer == null)
{
fr = nptr;
rer = nptr;
}
else
{
rer.setnext(nptr);
rer = rer.getnext();
}
size++ ;
}

public String dequeue()
{
Node ptr = fr;
fr = ptr.getnext();
if (fr == null)
rer = null;
size-- ;
String s = ptr.getdt();
return s;
}
  
  
public void printQueue()
{
System.out.print("\nQueue = ");
if (size == 0)
{
System.out.print("Empty\n");
return ;
}
Node ptr = fr;
while (ptr != rer.getnext())
{
System.out.print(ptr.getdt()+" ");
ptr = ptr.getnext();
}
System.out.println();
}
}

Screenshot:

3. Test.java

import java.util.Scanner;

public class Test{

   public static void main(String[] args) {
      
       Scanner s = new Scanner(System.in);

Queue l = new Queue();
char ch;   
do
{
System.out.println("\nOperations");
System.out.println("1. Enqueue");
System.out.println("2. Dequeue");
System.out.println("3. Size");
System.out.println("4. Print Queue");
System.out.println("5. Exit\n");
System.out.println("Enter your choice: ");
int choice = s.nextInt();
  
switch (choice)
{
  
   case 1 :
   System.out.println("Enter element to insert");
   l.enqueue(s.next());
   break;   
  
   case 2 :
      try
   {
   System.out.println("Dequeued Element: "+ l.dequeue());
   }
   catch (Exception e)
   {
   System.out.println("Error: "+ e.getMessage());
   }
   break;   
  
   case 3 :
       System.out.println("Size is: "+ l.Size());
   break;
        
   case 4 :
       l.printQueue();
       break;
      
   case 5:
       return;
  
   default :
   System.out.println("Invalid Choice! ");
   break;
}
  
System.out.println("\nWish to continue (y/n): ");
ch = s.next().charAt(0);
  
}while (ch == 'Y'|| ch == 'y');
   }
}

Screenshots:

The code is working fine..

Output:


Related Solutions

IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
One way to implement a queue is to use a circular linked list. In a circular...
One way to implement a queue is to use a circular linked list. In a circular linked list, the last node’s next pointer points at the first node. Assume the list does not contain a header and that we can maintain, at most, one iterator corresponding to a node in the list. For which of the following representations can all basic queue operations be performed in constant worst time? Justify your answers. Maintain an iterator that corresponds to the first...
Give a complete implementation of the queue ADT using a singly linked list that includes a...
Give a complete implementation of the queue ADT using a singly linked list that includes a header sentinel (in Python).
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO)....
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO). Use the driver to then test each of the methods. Simply download it and place it with the other source files. Create a class GenLLQueue which has the following: Internal class ListNode which contains: Instance variable data of type T Instance variable link of type ListNode Default constructor that sets both instance variables to null Instance Variables head which is of type ListNode which...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT