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 21 Solutions
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
- 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 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_forward
- Question 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_forwardAssume 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_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
- determine if the statement is true or false If N represents the number of elements in the queue, then the size method of the ArrayBoundedQueue class is O(N).arrow_forwardNonearrow_forwardQUESTION 9 Write a complete Java program to implement a QUEUE in a bank using LinkedList class as follows: • Provide the menu as shown below; use an infinite loop; stop when user opts 4. 1. Add a customer 2. Remove a customer 3. Show the queue 4. Exit • Add a customer should add an int number at the end of queue using the method addLast(). • Remove a customer should delete the int from front using the method removeFirst(). Show the queue should display the numbers in queue using the method System.out.printIn(). For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac).arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning