Code for this including the IndexError exception A \"CircularQueue\" is an abstr
ID: 3747979 • Letter: C
Question
Code for this including the IndexError exception
A "CircularQueue" is an abstract data type for storing a collection of items, where items are added to one end and removed from the opposite end of the collection. The methods for adding and removing items from a queue are traditionally called "enqueue and "dequeue()" respectively Complete and test a CircularQueue implementation. Exceptions are raised when preconditions are violated. For example, an IndexError will be raised if a client program attempt to dequeue from an empty queue or to enqueue from a full queue. Implement the dequeue(), enqueue0, peek() methods of the CircularQueue class to raise an IndexError in the above error situations You should include the entire CircularQueue class definition in your answer to this question. Use the implementation of the CircularQueue discussed in lecturers as a basis for your implementation For example Test Result try: q-CircularQueue(5) q.enqueue (2) print(q.dequeue()) except IndexError as err: print (err) try: The queue is empty qCircularQueue (10) print(q.dequeue)) except IndexError as err: print (err) try: The queue is full q = CircularQueue(5) q.enqueue (12) q.enqueue (17) q.enqueue (25) q.enqueue (11) q.enqueue (9) q.enqueue (3) print(q.dequeue )) except IndexError as err: print (err) Answer: (penalty regime: 0 %) 1 class CircularQueue:Explanation / Answer
SIMPLY CIRCULAR QUEUE IS A METHOD OF QUEUE IN FIRST IN FIRST OUT (FIFO) LOGIC LIKE IN OUR EVERYDAY QUEUE INFORNT OF SHOPS AND ALL BUT IN CASE OF CIRCULAR QUEUE THE THERE WILL BE A FRONT MAN AND A LAST MAN STANDING BESIDE EACH OTHER WHERE AFTER FRONT MAN DONE HIS JOB THE QUEUE IS -1 AND SO ON WHEN QUEUE IS EMPTY MEANS WHEN THERE IS NO ONE ON THERE WHICH IS FRONT VALUE (0) IS EQUAL TO LAST PERSON VALUE WHICH IS 0 THEN CIRCULAR QUEUE IS EMPTY BELOW IS IMPLEMENTATION IN EASY TERMS THAT YOU COULD UNDERSTAND WHERE ENQUEUE IS ADDELEMENT AND DEQUEUE IS DELETEING ELEMENT IN THE BELOW PROGRAM YOU CAN UNDERSTAND BETTER WITH THIS LOGIC
//what is problem with this for thumps down???? just change peek with last etc...
import java.io.*;
import java.util.*;
class circular
{
public static void main(String args[])
{
int n;
queue obj = new queue();
System.out.println("Enter size of queue");
Scanner s=new Scanner(System.in);
n=s.nextInt();
System.out.println("Enter elements");
Scanner t=new Scanner(System.in);
for(int i=0;i<n;i++)
{
obj.addele(t.nextInt(),n);
obj.delete();
}
}
}
class queue
{
int rear,front;
int x,n;
int[] q=new int[5];
queue()
{
rear=-1;
front=-1;
}
void addele(int x,int n)
{
if((rear==4)&&(front==-1))
System.out.println("queue full");
else
{
rear=(rear+1)%n;
q[rear]=x;
System.out.println("entered value is" +x);
}
}
void delete()
{
if(front==rear)
System.out.println("queue empty");
else
{
front=(front+1)%n;
x=q[front];
System.out.println("popped value is" +x);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.