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 5FTE

Explanation of Solution

Program purpose:

The given program is used to implement stack 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 #1:

The “dequeue()” function should remove from the front end, not from the rear end.

Correct statement:

//assigning value of at the front to a variable val.

int val=q[front];

/*removing value at front by incrementing the value of front by one*/

front++;

Error #2:

The statement used to wrap the indices “front” and “rear” to “0” is missing. It should be done for saving the memory when the “dequeue()” method is popped all the elements.

Correct statement:

/*checking whether value of front is equal to length of the queue*/

if(front == q.length)

//set front to 0

front=0;

Corrected code:

//An array implementation of a queue

int dequeue()

{

    //checking whether the queue is empty

    if (empty())

/*throws an exception EmptyQueueException indicating that there is no elements available    to remove...

Blurred answer
Students have asked these similar questions
java program to CircularArrayQueue represents an array implementation of a queue in which the indexes for the front and rear of the queue circle back to 0 when they reach the end of the array.
If the following is a circular array based queue of size      23 43 54 76 93 99 17 If rear is at index 1 and front is at index 3, what is the size of the queue?
The 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()
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
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning