Concept explainers
Explanation of Solution
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...
Want to see the full answer?
Check out a sample textbook solutionChapter 20 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- 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;…arrow_forwardAnalyze 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 60arrow_forwardA data structure called a deque is closely related to a queue. The name deque stands for “double-ended queue.” The difference between the two is that with a deque, you can insert, remove, or view from either end of the queue. Implement a deque using arraysarrow_forward
- #include <iostream>using namespace std; class Queue { int *queue, size, front, rear;public: Queue(int size) { queue = new int[size]; this -> size = size; front = rear = -1; } ~Queue() { delete[] queue; } void push(int val) { if (rear == -1) { queue[++rear] = val; front++; } else { if (rear == ((front - 1) % size)) throw "Queue Full!"; if (rear == size - 1) rear = -1; queue[++rear] = val; } } int pop() { if (front == -1) throw "Queue Empty!"; int ret = queue[front]; front = (front + 1) % size;…arrow_forwardvoid 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 *) **arrow_forwardQuestion 9: Draw internal representation of the queue q for each step of the following code: ArrayBoundedQueue<Integer> q = new ArrayBoundedQueue<Integer>(6); q.enqueue(37); q.enqueue(86); q.enqueue(54); q.enqueue(71); q.dequeue(); q.enqueue(97); q.dequeue();arrow_forward
- Assume the Queue is initially empty. class Queue { public: int size () ; bool empty (); chará front () throw (Exception); void enqueue (char& e); void dequeue () throw (Exception); Function call Output Queue Contents (back -> front) enqueue ('A') enqueue ('B') front ( ) size ( ) dequeue () enqueue ('C') empty( ) dequeue ( ) size( ) dequeue () empty ( ) front ( ) enqueue ('D') front () dequeue () dequeue ( )arrow_forwardQueue abcQ = new Queue (); Queue out = new Queue (); //statements to insert value 1, 2, 3, 4, 5 into abcQ for (int i = 0; i < 5; i++) { int a = Integer.parseInt (abcQ.dequeue ().tostring ()); int b = Integer.parseInt (abcQ.dequeue ().tostring ()); abcQ.enqueue (a); abcQ.enqueue (b); out.enqueue (a + b); for (int i = 0; i < 5; i++) System.out.print (abcQ.dequeue () + " "); System.out.println ("\n") ; for (int i = 0; i < 5; i++) System.out.print (out.dequeue () + " "); (- ks)arrow_forwardjava 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.arrow_forward
- In C++ I need to write a recursive function insertEnd that will call a recursive method insertEnd(const ItemType& newEntry, Node<ItemType>* node), to insert newEntry at the end of the linked list I completely stuck and can't figure out where the error is coming from. main #include <iostream> #include "LinkedList.cpp" int main() { Node<int>* first = new Node(1); Node<int>* second = new Node(2); Node<int>* third = new Node(3); first->next = second; second->next = third; cout<<first->data<<endl; cout<<first->next->data<<endl; cout<<first->next->next->data<<endl; LinkedList<int> list; //an empty linked list for(int i =0; i<10; i++){ list.inserEnd(i); list.display(); } return 0; } linkedlist.cpp #include "LinkedList.h" #include "iostream" using namespace std; template <class T> LinkedList<T>::LinkedList(){ head = nullptr; } template <class…arrow_forwardWhat are the disadvantages of Queue's array implementation?arrow_forwardIn Java and C++ create a Generic ArrayList where all elements in the ArrayList must be of the same type. In every node of the linked list the size of the array it contains must be at least double the size of the previous node’s array. Between O(1) time and O(n) time. add(int index, E e) clear() contains(E e) ensureCapacity(int minCapacity) isEmpty() get(int index) remove(int index) size()arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning