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
What problem is overcome by using a circular array for a static queue?
void stack::do(){ for(int i=0li<=topindex/2;i++){ T temp=entry[i]; entry[i]=entry[topindex-i-1]; entry[topindex-1-i]=temp;} } Assume the stack is array based. What is this method do? a. swap the first item with last item b. doesn't do any thing c. replace each item with next item value d. reverse the stack
#include <iostream> usingnamespace std; class Queue {     int size;     int* queue;         public:     Queue(){         size = 0;         queue = new int[100];     }     void add(int data){         queue[size]= data;         size++;     }     void remove(){         if(size ==0){             cout <<"Queue is empty"<<endl;             return;         }         else{             for(int i =0; i < size -1; i++){                 queue[i]= queue[i +1];             }             size--;         }     }     void print(){         if(size ==0){             cout <<"Queue is empty"<<endl;             return;         }         for(int i =0; i < size; i++){             cout<<queue[i]<<" <- ";         }         cout << endl;     }     //your code goes here     }; int main(){     Queue q1;     q1.add(42); q1.add(2); q1.add(8);  q1.add(1);     Queue q2;     q2.add(3); q2.add(66); q2.add(128);  q2.add(5);     Queue q3 = q1+q2;     q3.print();…
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