Concept explainers
Explanation of Solution
The given program is used to implement queue using linked list.
Logical error:
Logical error is a mistake in a program’s source code that leads to produce an incorrect output. This error is a type of run time error, as it cannot be identified during the compilation of the program. This is a logical mistakes created by the programmer and it is determined when the program produces wrong output.
Error in the given code:
In the code, for inserting element to queue the statement “rear=new Node(x);” is used. Instead of it, user should use “rear.next=new Node(x)”. Because, when using the statement “rear=new Node(x)” every time, the value of the “rear” is changing instead of adding new element. In the corrected statement, the “rear.next” is set as pointing to a new element (x).
Correct statement:
//creating node containing x, set rear.next to point that node.
rear.next=new Node(x);
Corrected code:
//method enqueue is for adding a new value to the queue.
void enqueue(int x)
{
//if the queue is empty
if (empty())
{
//set rear to pointing to a new node with value x...
Want to see the full answer?
Check out a sample textbook solutionChapter 20 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- Implement a function void reverseQueue(QueueSLL&) that reverses the elements of a parameter queue. The function uses a stack for the reverse.arrow_forwardJava(eclipse) Implement a method concatenate in class LinkedQueue. This method should take all the elements of a queue sourceQ and append them to the end of another queue targetQ. The operation should result in sourceQ being an empty queue. Test this method in the main method of LinkedQueue.arrow_forwardPush, pop, and enqueue operations are supported by a data type known as a stack-ended queue (steque). Give this ADT an API description. Create an implementation based on linked lists.arrow_forward
- In an ideal implementations of a queue, all operations are ______________________ .arrow_forwardWhat is the value of REAR in a circular queue?arrow_forwardThe queue operation that is required when using an array implementation, but is not required when using a linked list implementation, is ___. isFull() isEmpty() peek() dequeue()arrow_forward
- Stacks andQueues, GenericQueue is implemented using composition. Define a new queueclass that extends java.util.LinkedList.arrow_forwardImplement a method concatenate in class LinkedQueue. This method should take all the elements of a queue sourceQ and append them to the end of another queue targetQ. The operation should result in sourceQ being an empty queue. Test this method in the main method of LinkedQueue. package queues;public class LinkedQueue<E> implements Queue<E> { private SinglyLinkedList<E> list = new SinglyLinkedList<>(); // an empty list public LinkedQueue() { } // new queue relies on the initially empty list @Override public int size() { return list.size(); } @Override public boolean isEmpty() { return list.isEmpty(); } @Override public void enqueue(E element) { list.addLast(element); } @Override public E first() { return list.first(); } @Override public E dequeue() { return list.removeFirst(); } public String toString() { return list.toString(); }}arrow_forwardThe implementation of QueueLists makes use of a CircularList. Implement QueueLists in a manner that is efficient in time and space using Node with a head and tail reference.arrow_forward
- Find the error in each of the following code segments // A linked implementation of a queue int dequeue() { if(empty()) throw new EmptyQueueException(); int value = front.value; front ++; return value; }arrow_forwardA stack-ended queue, sometimes known as a steque, is a data type that allows push, pop, and enqueue operations. Make an API for this ADT. Create a linked-list implementation.arrow_forwardWrite a Queue implementation that uses a circular linked list, which is the sameas 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 variablearrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education