Method to add an element in circular queue, Data Structure & Algorithms

Q. Let us consider a queue is housed in an array in circular fashion or trend. It is required to add new items to the queue. Write down a method ENQ to achieve this also check whether the queue is full. Write down another procedure DQ to delete an element after checking queue empty status.  

Ans.

The Method To Add an element in Circular Queue

# define  MAXQUEUE 100 struct queue{

int items[MAXQUEUE];

int front, rear;

}

struct queue q;

q.front=q.rear=MAXQUEUE -1;

void ENQ(struct queue *pq, int x)

{

/* make room for new element*/

if(pq ->rear = MAXQUEUE - 1)

pq-> rear = 0;

else

(pq->rear)++;

/* check for overflow */

if(pq ->rear==pq->front)

{

printf("queue overflow);

exit(1);

}

pq->items[pq->rear]=x;

return;

}/* end of ENQ*/

A Method to Delete an element from Circular Queue

int DQ(struct queue *pq)

{

if(pq-> rear == pq-> front)

{

printf("queue underflow");

exit(1);

}/*end if*/

if(pq->front = = MAXQUEUE-1)

pq->front=0;

else

(pq->front)++;

return(pq->items[pq->front]);

}/*end DQ*/

Posted Date: 7/12/2012 9:16:50 AM | Location : United States







Related Discussions:- Method to add an element in circular queue, Assignment Help, Ask Question on Method to add an element in circular queue, Get Answer, Expert's Help, Method to add an element in circular queue Discussions

Write discussion on Method to add an element in circular queue
Your posts are moderated
Related Questions
Q. Explain that how do we implement two stacks in one array A[1..n] in such a way that neither the stack overflows unless the total number of elements in both stacks together is n.

An AVL tree is a binary search tree that has the given properties: The sub-tree of each of the node differs in height through at most one. Each sub tree will be an AVL tre

Define Wire-frame Model This skeletal view is called a Wire-frame Model. Although not a realistic representation  of the object, it is still very useful in the early stages of

What do we mean by algorithm? What are the characteristics of a good and relevant algorithm? An algorithm is "a step-by-step procedure for finishing some task'' An algorithm c

what happen''s in my computer when i input any passage

A significant aspect of Abstract Data Types is that they explain the properties of a data structure without specifying the details of its implementation. The properties might be im

Differentiate between Nonpersistent and 1-persistent Nonpersistent: If the medium is idle, transmit; if the medium is busy, wait an amount of time drawn from a probability dist

C compiler does not verify the bounds of arrays. It is your job to do the essential work for checking boundaries wherever required. One of the most common arrays is a string tha

Painter's Algorithm As the name suggests, the algorithm follows the standard practice of a painter, who  would paint the background (such as a backdrop) first, then the major d

Program segment for deletion of any element from the queue delete() { int delvalue = 0; if (front == NULL) printf("Queue Empty"); { delvalue = front->value;