Question

Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I HAVE IMPLEMENTED THE CIRCULAR QUEUE WITH THE REQUIRED OPERATIONS DESCRIBED ABOVE.

I HAVE ALSO INCLUDED THE COMMENTS TO DESCRIBE THE OPERATIONS.

ANY FURTHER CLARIFICATION REQUIRED LEAVE A COMMENT:

//program to implement circular queue
#include<stdio.h>
#include<stdlib.h>
#define MAX 3
struct queue
{
   int *ele;
   int capacity,size;
   int head,tail;
};
typedef struct queue QUEUE;
/*INITILIZATION OF QUEUE STRUCTURE*/
void initialize(QUEUE *q)
{
q->capacity=MAX;
q->ele=(int*)calloc(q->capacity,sizeof(int));
q->head=0;
q->tail=0;
q->size=0;
}
/*TO ADD AN ELEMENT TO QUEUE*/
void enqueue(QUEUE *q)
{
int ele;
if(q->size<q->capacity)
{
printf("\n\t....ENTER AN ELEMENT \t");
scanf("%d",&ele);
q->ele[q->tail]=ele;
q->tail=(q->tail+1)%q->capacity;
q->size++;
printf("\n\t....ELEMENT ENQUEUED SUCCESSFULLY \n");
}else
{
/*IF THE QUEUE IS FULL DOUBLE ITS SIZE */
   q->tail=q->capacity;
   q->capacity=q->capacity*2;
   q->ele=(int*)realloc(q->ele,q->capacity*sizeof(int));
   printf("\n\t....ENTER AN ELEMENT \t");
scanf("%d",&ele);
q->ele[q->tail]=ele;
q->tail=(q->tail+1)%q->capacity;
q->size++;
printf("\n\t....ELEMENT ENQUEUED SUCCESSFULLY \n");
}
}
/*TO REMOVE AN ELEMENT TO QUEUE*/
void dequeue(QUEUE *q)
{
if(q->size==0)
{
   printf("\n\t....QUEUE UNDERFLOW\n\t");
   initialize(q);
}
else
{
   printf("\n\t....THE REMOVED ELEMENT IS %d\n\t",q->ele[q->head]);
q->ele[q->head]=0;
q->head=(q->head+1)%q->capacity;
q->size--;
}
}
/*TO PRINT ELEMENTS FROM QUEUE*/
void print(QUEUE *q)
{
   int i=q->head;
   int cnt=q->size;
   printf("\n\t....ELEMENTS IN THE QUEUE ARE\n\t");
   while(cnt!=0)
   {
       printf("\t %d",q->ele[i]);
       i=(i+1)%q->capacity;
       cnt--;
   }
}
void main()
{
   int choice;
   QUEUE q;
   initialize(&q);
   printf("\n\t....CIRCULAR QUEUE PROGRAM USING ARRAYS....\n");
   while(1)
   {

printf("\n\t....1.ENQUEUE\t2.DEQUEUE\t3.PRINT\t4.EXIT\n\t....Enter your choice :");
       scanf("%d",&choice);
       switch(choice)
       {
       case 1:enqueue(&q);break;
       case 2:dequeue(&q);break;
       case 3:print(&q);break;
       case 4:exit(0);
       }
   }
}

OUTPUT:

1 LILLLULUI TUHPL D:\My DS LAB>gcc CIRCULAR_QUEUE_USING_ARRAYS.C D:\My DS LAB>a ....CIRCULAR QUEUE PROGRAM USING ARRAYS... ..

....ELEMENTS IN THE QUEUE ARE 10 20 30 ....1. ENQUEUE 2.DEQUEUE . . . . Enter your choice : 2 40 3.PRINT 4. EXIT .... THE REM

(b)

If we implemented the circular queue using the linked list the time complexity and space complexcity of enqueue is same until the array is full ,when the array is full we need to reallocate the array.The time and space complexity of dequeue is same in both cases
Add a comment
Know the answer?
Add Answer to:
Suppose we want to implement a circular queue using an array that has an initial capacity...
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 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,...

  • // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations // ...

    // =================== Support Code ================= // Queue // // // // - Implement each of the functions to create a working circular queue. // - Do not change any of the function declarations //   - (i.e. queue_t* create_queue(unsigned int _capacity) should not have additional arguments) // - You should not have any 'printf' statements in your queue functions. //   - (You may consider using these printf statements to debug, but they should be removed from your final version) // ==================================================...

  • Study the "Queue as linked list simple example" posted in this module. Rewrite the code to...

    Study the "Queue as linked list simple example" posted in this module. Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. LListQueue.cpp ///--------------------------------------------------------------- /// File: LListQueue.cpp /// Purpose: Implementation file for a demonstration of a queue /// implemented as an array. Data type: Character /// Programming Language: C++ ///--------------------------------------------------------------- #include "LListQueue.h" ///-------------------------------------------- /// Function: LListQueue() ///...

  • (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...

  • QUEUEBOX: Using an Array of initial size of five (5) for storage, start with the following...

    QUEUEBOX: Using an Array of initial size of five (5) for storage, start with the following generic class declaration for QueueBox: public class QueueBox<E> { private E[] elements = (ED))( new Object[5]); private int front_idx = 0; private int rear_idx = 0; private int count = 0; Hint: use the count variable to keep track of how many elements are in the queue (increment count when enqueing and decrement when dequeing). Makes it a lot easier to determine if the...

  • In Array Based Queue. The program calls for implementing a queue using an array such that...

    In Array Based Queue. The program calls for implementing a queue using an array such that the queue wraps around to reuse empty array slots. This means that the Head and Tail of the array can reverse relative positions. If, rather than changing the value of the Head and Tail, a modulus (of the size of the array) was used instead, what boundary condition could eventually be reached?

  • The class pictured below is designed to implement an integer queue using two stacks. Assume the...

    The class pictured below is designed to implement an integer queue using two stacks. Assume the stack methods all work as desired (though their implementations are not shown). .(a) Trace what happens in the following situation, showing intermediate steps (values of variables, what is in the stacks, and what is in the queue at various points in the methods, not just the results of the methods). • Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue twice....

  • 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...

  • Help me solve this in C++ Rewrite the code to use array instead of linked lists....

    Help me solve this in C++ Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...

  • 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...

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