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
What problem is overcome by using a circular array for a static queue?
The QueueInterface interface is a contract between a Queue ADT's implementer and the programmer who uses it.is it true or false
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
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