Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
3rd Edition
ISBN: 9780134038179
Author: Tony Gaddis, Godfrey Muganda
Publisher: PEARSON
bartleby

Concept explainers

Expert Solution & Answer
Book Icon
Chapter 21, 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
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
class Queue {       private static int front, rear, capacity;       private static int queue[];           Queue(int size)        {               front = rear = 0;               capacity = size;               queue = new int[capacity];           }        // insert an element into the queue      static void queueEnqueue(int item)    {           // check if the queue is full          if (capacity == rear)         {               System.out.printf("\nQueue is full\n");               return;           }                // insert element at the rear           else {               queue[rear] = item;               rear++;           }           return;       }            //remove an element from the queue      static void queueDequeue()      {           // check if queue is empty           if (front == rear)        {               System.out.printf("\nQueue is empty\n");               return;           }                // shift elements to the right by one place uptil rear           else {…
Find the output of the following program code if the following values have been inserted into abcQ: 1 2 4 5
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