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)
- Queue 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_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
- In 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_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_forwardThe implementation of a queue in an array, as given in this chapter, uses the variable count to determine whether the queue is empty or full. You can also use the variable count to return the number of elements in the queue. On the other hand, class linkedQueueType does not use such a variable to keep track of the number of elements in the queue. Redefine the class linkedQueueType by adding the variable count to keep track of the number of elements in the queue. Modify the definitions of the functions addQueue and deleteQueue as necessary. Add the function queueCount to return the number of elements in the queue. Also, write a program to test various operations of the class you defined.arrow_forward
- This chapter describes the array implementation of queues that use a special array slot, called the reserved slot, to distinguish between an empty and a full queue. Write the definition of the class and the definitions of the function members of this queue design. Also, write a program (in main.cpp) to test various operations on a queue. //Header file QueueAsArray #ifndef H_QueueAsArray #define H_QueueAsArray #include <iostream> #include <cassert> using namespace std; template<class Type> class queueType { public: const queueType<Type>& operator=(const queueType<Type>&); // overload the assignment operator void initializeQueue(); int isEmptyQueue() const; int isFullQueue() const; Type front() const; Type back() const; void addQueue(Type queueElement); void deleteQueue(); queueType(int queueSize = 100); queueType(const queueType<Type>& otherQueue); // copy constructor…arrow_forwardpublic class Queue {public static void main(String[] args) {}public int[] arr;int count=0;int top=0;int size=4;int queue[]=new int[size];Queue(int size){arr=new int[size];}void enqueue(int val){if(count==arr.length){System.out.println("Queue is full");}else{arr[count]=val;count++;}}int dequeue(){if(count==0){System.out.print("stack is empty"); return -1;}else{count--;return arr[top++];}}int peek(){if(count==0){System.out.print("queue is empty"); return -1;}return arr[count-1];} public boolean isempty(){if(count==0){System.out.println("Queue is empty");return true;}return false;}public void display(){for (int i = 0; i < queue.length; i++) {System.out.println("Queue [" + i + "] " + queue[i]);}}} (I need the algorithm of this code)arrow_forwardIn an array based FIFO queue Q, which of the following is correct about the queue after Q.Dequeue( ) is executed? The size of the array is 9 as shown below: 5 (front) 9 8 7 3(rear) 4 front = 1; rear =8 front = 0; rear =7; front = 2; rear =3; front = 0; rear =8;arrow_forward
- Note : addqueue works like Enqueue and deleteQueue works like Dequeue Consider the following statements: (8, 9) queueType queue; int num; Show what is output by the following segment of code num = 7; queue.addQueue (6); queue.addQueue (num); num = queue.front (); queue.deleteQueue(); queue.addQueue (num + 5); queue.addQueue (14); queue.addQueue (num queue.addQueue (25); queue.deleteQueue (); 2); cout <« "Queue elements: "; while (!queue.isEmptyQueue ()) { cout <« queue.front () << " "; queue.deleteQueue(); } cout <« endl; Queue elements: 14 14 4 25 Queue elements: 11 14 4 4 Queue elements: 11 14 4 25 Queue elements: 11 14 25 25arrow_forwardplease convert this into c++ import java.util.Iterator;import java.util.NoSuchElementException; public class Queue<Item> implements Iterable<Item> { private int n; // number of elements on queue private Node first; // beginning of queue private Node last; // end of queue // helper linked list class private class Node { private Item item; private Node next; } /** * Initializes an empty queue. */ public Queue() { first = null; last = null; n = 0; } /** * Returns true if this queue is empty. * * @return {@code true} if this queue is empty; {@code false} otherwise */ public boolean isEmpty() { return first == null; } /** * Returns the number of items in this queue. * * @return the number of items in this queue */ public int size() { return n; } /** * Returns the number of items in this queue. * * @return the…arrow_forwardvoid funi (int iist , int s) { int sum=0; for (int i = 0; iarrow_forwardarrow_back_iosSEE MORE QUESTIONSarrow_forward_ios
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning