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
EBK STARTING OUT W/JAVA:...DATA...
- A 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_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
- 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.arrow_forwardIn 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_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