Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
4th Edition
ISBN: 9780134787961
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 20, Problem 2FTE

Explanation of Solution

Program purpose:

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...

Blurred answer
Students have asked these similar questions
Queue abcQ = new Queue (); Queue out = new Queue (); //statements to insert value 1, 2, 3, 4, 5 into abcQ for (int i = 0; i < 5; i++) { int a = Integer.parseInt (abcQ.dequeue ().tostring ()); int b = Integer.parseInt (abcQ.dequeue ().tostring ()); abcQ.enqueue (a); abcQ.enqueue (b); out.enqueue (a + b); for (int i = 0; i < 5; i++) System.out.print (abcQ.dequeue () + " "); System.out.println ("\n") ; for (int i = 0; i < 5; i++) System.out.print (out.dequeue () + " "); (- ks)
In linked list implementation of a queue, the important condition for a queue to be empty is? a) FRONT is null  b) REAR is null  c) LINK is empty  d) FRONT == REAR-1
data structures in java
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education