Concept explainers
Explanation of Solution
The given program is used to implement queue 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 in the given code:
In the code, for inserting element to queue the statement “rear=new Node(x);” is used. Instead of it, user should use “rear.next=new Node(x)”. Because, when using the statement “rear=new Node(x)” every time, the value of the “rear” is changing instead of adding new element. In the corrected statement, the “rear.next” is set as pointing to a new element (x).
Correct statement:
//creating node containing x, set rear.next to point that node.
rear.next=new Node(x);
Corrected code:
//method enqueue is for adding a new value to the queue.
void enqueue(int x)
{
//if the queue is empty
if (empty())
{
//set rear to pointing to a new node with value x...
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)
- class Queue { private static int front, rear, capacity; private static int queue[]; Queue(int size) { front = rear = 0; capacity = size; queue = new int[capacity]; } // insert an element into the queue static void queueEnqueue(int item) { // check if the queue is full if (capacity == rear) { System.out.printf("\nQueue is full\n"); return; } // insert element at the rear else { queue[rear] = item; rear++; } return; } //remove an element from the queue static void queueDequeue() { // check if queue is empty if (front == rear) { System.out.printf("\nQueue is empty\n"); return; } // shift elements to the right by one place uptil rear else {…arrow_forwardFind the output of the following program code if the following values have been inserted into abcQ: 1 2 4 5arrow_forwardvoid pop()// Pre: size() > 0.// Post: The highest priority item has been removed from the// p_queue. (If several items have the equal priority,// then the implementation may decide which one to remove.)// void p_queue::pop() { cerr << "pop() not implemented yet" << endl; }arrow_forward
- By using this Queue class: public class Queue {private int rear, front;private Object[] elements;Queue(int capacity){elements = new Object[capacity];rear = -1;front = 0;}void enqueue(Object data){if(isFull()){System.out.println("Queue overflow");}else {rear = (rear +1) % elements.length;elements[rear] = data;}}Object dequeue(){if(isEmpty()){System.out.println("Queue empty");return null;}else {Object retData = elements[front];elements[front]= null;front = (front+1) % elements.length;return retData;}}Object peek(){if (isEmpty()){System.out.println("Queue is empty");return null;}else {return elements[front];}}boolean isEmpty(){return elements[front] == null;}boolean isFull(){return (front == (rear +1)% elements.length &&elements[front] != null&& elements[rear] != null);}int size(){if (rear >= front){return rear - front +1;}else if (elements[front] != null) {return elements.length - (front - rear) + 1;}else {return 0;}}}arrow_forwardclass Queue { private static int front, rear, capacity; private static int queue[]; Queue(int c) { front = rear = 0; capacity = c; queue = new int[capacity]; } static void queueEnqueue(int data) { if (capacity == rear) { System.out.printf("\nQueue is full\n"); return; } else { queue[rear] = data; rear++; } return; } static void queueDequeue() { if (front == rear) { System.out.printf("\nQueue is empty\n"); return; } else { for (int i = 0; i < rear - 1; i++) { queue[i] = queue[i + 1]; } if (rear < capacity) queue[rear] = 0; rear--; } return; } static void queueDisplay() { int i; if (front == rear) { System.out.printf("\nQueue is Empty\n"); return; } for (i = front; i < rear; i++) { System.out.printf(" %d <-- ", queue[i]); } return; } static void queueFront() { if (front == rear) { System.out.printf("\nQueue is Empty\n"); return; } System.out.printf("\nFront Element is: %d", queue[front]);…arrow_forwardC++ ProgrammingActivity: Queue Linked List Explain the flow of the code not necessarily every line, as long as you explain what the important parts of the code do. The code is already correct, just explain the flow. SEE ATTACHED PHOTO FOR THE PROBLEM #include "queue.h" #include "linkedlist.h" class SLLQueue : public Queue { LinkedList* list; public: SLLQueue() { list = new LinkedList(); } void enqueue(int e) { list->addTail(e); return; } int dequeue() { int elem; elem = list->removeHead(); return elem; } int first() { int elem; elem = list->get(1); return elem;; } int size() { return list->size(); } bool isEmpty() { return list->isEmpty(); } int collect(int max) { int sum = 0; while(first() != 0) { if(sum + first() <= max) { sum += first();…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_forwardData structure c++ Please solve it fast and correctarrow_forwardIf N represents the number of elements in the queue, then the dequeue method of the LinkedQueue class is O(N). true or falsearrow_forward
- java data structure Queue: Q4: A program performs the following operations on an empty queue Q: Q.enqueue(24) Q.enqueue(74) Q.enqueue(34) Q.first() Q.dequeue() Q.enqueue(12) Q.dequeue() Please show the queue contents at the end of these operations. Clearly show the front of the queue.arrow_forwardQ4. Add a member method RemoveAdd () to QueueType class implemented using Linked structure that will remove the first item from the queue and add it to the end of the queue if it is not empty. void QueueType::RemoveAdd( );arrow_forwardThe essential condition which is checked before deletion in a linked queue is? a) Underflow b) Overflow c) Front value d) Rear valuearrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education