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
package queuedemo; class Queue { int front, rear, size;     int capacity;     int array[];       public Queue(int cap)     {         capacity = cap;         front=size=0;         rear=capacity-1;         array = new int[capacity];     }          // Method to add an item to the queue.     void enqueue(int item)     {         if(isFull()) {             System.out.println("Queue overflow");             return;         }         rear=(rear+1)%capacity;         array[rear]=item;         size=size+1;     }       // Queue is empty when size is 0     boolean isEmpty()     {         return (size == 0);     }       // Queue is full when size becomes equal to the capacity     boolean isFull()     {         return (size == capacity);     }       // Method to remove an item from queue.     int dequeue()     {         if (isEmpty())             System.out.println("Queue Underflow");             return;         }         int item = array[front];         front = (front + 1)% this.capacity;…
Analyze the following code: import java.util.*; public class Test { public static void main(String[] args) { PriorityQueue queue = } } new PriorityQueue( Arrays.asList(60, 10, 50, 30, 40, 20)); for (int i: queue) System.out.print(i + "); The program displays 60 10 50 30 40 20 The program displays 10 20 30 40 50 60 The program displays 60 50 40 30 20 10 There is no guarantee that the program displays 10 20 30 40 50 60
void listEmployees (void) { for (int i=0; i 10000. Make a guess about why the comparison function takes 2 struct Employee parameters (as opposed to struct Employee *) **
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