Question

Suppose we have an array-based queue (circular buffer) of size 6: int data[6]; int front =...

Suppose we have an array-based queue (circular buffer) of size 6:

int data[6];
int front = 0, back = 0;

void enqueue(int x) {
  data[back] = x;
  back = (back + 1) % 6;
}

void dequeue() {
  front = (front + 1) % 6;
}

and we perform the following series of queue operations:

enqueue(1);
dequeue();
enqueue(2);
dequeue();
enqueue(7);
enqueue(3);
enqueue(5);
dequeue();
dequeue();
enqueue(4);
enqueue(6);

Write the state of the queue array after each operation, and at the end, write the values of the front and back variables.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
enqueue(1);
queue: [1]
front: 0  
back: 1

dequeue();
Deleted value 1 from queue
queue: []
front: 1  
back: 1

enqueue(2);
queue: [2]
front: 1  
back: 2

dequeue();
Deleted value 2 from queue
queue: []
front: 2  
back: 2

enqueue(7);
queue: [7]
front: 2  
back: 3

enqueue(3);
queue: [7, 3]
front: 2  
back: 4

enqueue(5);
queue: [7, 3, 5]
front: 2  
back: 5

dequeue();
Deleted value 7 from queue
queue: [3, 5]
front: 3  
back: 5

dequeue();
Deleted value 3 from queue
queue: [5]
front: 4  
back: 5

enqueue(4);
queue: [5, 4]
front: 4  
back: 0

enqueue(6);
queue: [5, 4, 6]
front: 4  
back: 1

Final queue: [5, 4, 6]
Add a comment
Know the answer?
Add Answer to:
Suppose we have an array-based queue (circular buffer) of size 6: int data[6]; int front =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

  • (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed...

    (C++) (VISUAL STUDIO) Circular Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle. In a normal Queue, we can insert elements until queue becomes full. But once queue becomes full, we cannot insert the next element even if there is a space in front of queue. Efficiently implement a queue class using a circular...

  • Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds value...

    Array-based Queue Lecture 6 Two Class Exercises | Class Exercise #1 - Create an array-based queue that holds values of double data type. 1.) Create a program that produces the following output OUTPUT: Q Quit Enter your choice: e Enter an item: 1.1 E Enqueue D Dequeue s-show queue ← showMenuO function called in main) OQuit // screen clears-.. continue enqueuing.screen clearing with each iteration Enter your choice: e Queue is full. E Enqueue D Dequeue s Show queue 0...

  • Balment a la medicul Quoc that speciala a circular que has the following private data members...

    Balment a la medicul Quoc that speciala a circular que has the following private data members and public member functions. The circular que simplemented using an atay. Your submission should consist of four separate files the three source code file header file.implementation file and main program or routine and the program otput. When making the submission, please do not submit it as a file Private data members: int the tray int current size oprema prinete the first met of the...

  • 2. Consider a circular array based Queue as we have discussed in the lectures (class definition...

    2. Consider a circular array based Queue as we have discussed in the lectures (class definition given below for reference) public class CircArrayQueue<E> implements Queue<E private EI Q private int front-0 indicates front of queue l indicates position after end of queue private int end-0: public CircArrayQueue( public int getSize (.. public boolean isEmpty ( public void enqueue (E e)... public E dequeue ) throws EmptyQueueException... Il constructor We are interested in implementing a Stack class based on the above...

  • please write them in parts and not the whole code Page 3 of 5 based queue...

    please write them in parts and not the whole code Page 3 of 5 based queue - coding question rray-based fine an array-bag ray-based queue template class ArrQueue that uses a one-dimensional circular array esent the queue. The class consists of member variables: items, front, back, count. Wher functions: enqueue, dequeue, is Empty, peek Front. to represent the queue ement/ write code for enqueue member function ment/ write code for dequeue member function

  • A limited-sized Queue ADT is an abstract data type that has a limit on the length...

    A limited-sized Queue ADT is an abstract data type that has a limit on the length of the queue. It can be created with a static array of size N, where N is the maximum length of the array. In C, this structure can be defined as follows: typedef struct {int * data;//array of the data on the queue//you may add other attributes here but use as few as possible} queue_t; Write an (efficient) pseudocode for the implementation of each...

  • uppose that Q is an initially empty array-based queue of size 5. Show he values of...

    uppose that Q is an initially empty array-based queue of size 5. Show he values of the data members front and back after each statement has een executed. Indicate and errors that might occur. Queue< char Q 5 Q.enqueue( 'A'); Q. enqueue( 'B Q.enqueue(C; char c = Q.dequeue( ); Q. enqueue( 'A; front = front » front - front = front = front = back back = back - back back =-- back =

  • Write C++ program that implements the queue data structure using an array and perform the following...

    Write C++ program that implements the queue data structure using an array and perform the following operations: i) Enqueue ii) Dequeue iii) Display

  • (ii) [6 marks] Assume that we have an empty stack S and an empty queue Q....

    (ii) [6 marks] Assume that we have an empty stack S and an empty queue Q. Given a series of stack operations on S as below: Push(S,10), Push(S, 7), Push(S, 23), Pop(S), Push(S, 9), Pop(S), Pop(S) Output the element returned by each Pop operation. Given a series of queue operations on Q as below: Enqueue(0,10),Enqueue(Q,20), Enqueue(0,33), Dequeue(Q), Enqueue(Q,55), Dequeue(Q), Dequeue(Q) Output the element returned by each Dequeue operation. (iii) [8 marks] Given an empty binary search tree T, draw the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT