i have made the following code...
but i am having problems in accessing it..

//structure for queue
struct queue{
        int items[MAX];
        int front;
        int rear;
};
//decleration of the queues :que_term for the values printed on the terminal
struct queue que_mul2,que_mul3,que_mul5,que_sort,que_write,que_term;

 //initializing the queue
void InitQueue(queue &q)
{
        q.front=0;
        q.rear=0;
}
//checking if queue is empty
int isempty(queue &q)
{
        if(q.rear==q.front && q.front!=0)
                    return 1;
        else
                return 0;
}
//adding an element at the rear of the queue
void join(queue &q,int elem)
{
        q.items[q.rear]=elem;
        q.rear++;
}
//removing an element from the front of the queue
int leave(queue &q)
{
        int elem;

                elem=q.items[q.front];
                q.front++;
                return elem;
//        }
//        else
//                return -1;
}

these are the functions used but when i am calling this function as

join(que_write,1);

then que_write is not getting updated...
i have declared que_write as

struct queue que_write;
as mentioned above

pls tell me where im going wrong?
n is the struct type passed by reference by default like arrays?

Recommended Answers

All 4 Replies

Is this supposed to be C, not C++? I question this because of int isempty(queue &q) -- the parameter looks like a c++ reference.

Is that supposed to be circular queue where the front and back variables wrap around to 0 then the buffer items is filled up? If yes or no you need to add code to the join() function to wrap the front variable when it reached MAX, or not allow any more items in the queue when (front+1) > rear.

You need to firm up your understanding of the front and back variables.
front: This is where the next item will be inserted into the queue. When an item is inserted (see join function) this is the variable that should get incremented.

rear: This is where the next item will be removed from the queue. When an item is removed from the queue (see leave function) this is the variable that will be incremented.

You need checks in join() that front+1 does not exceed rear. And you need checks in leave() that rear+1 does not exceed front.

i am invoking these methods as
in main

int main(int argc,char *argv[])
{
        pthread_t threads[5];
        pthread_attr_t attr;
        int rc;
        void *status;




       //initializing the queues
        InitQueue(que_mul2);
        InitQueue(que_mul3);
        InitQueue(que_mul5);
        InitQueue(que_sort);
        InitQueue(que_write);
        InitQueue(que_term);

        //seed que_write with 1
        join(que_write,1);

}

its a fifo queue so thats why i used front as for deleting and rear for adding..
ya i will incorporate those condition checs..
but the main problem is that im not able to invoke these methods properly..
what shud i declare the queues as?

struct queue *q1;
or
struct queue q1;

coz i use
join(q1);

n this is C.In C++ it wud have been easier cuz u have objects.but here im not able to reflect the changes in the queue.

im getting error:

Hamming.c:97: error: invalid initialization of reference of type 'queue&' from expression of type 'queue*'
Hamming.c:45: error: in passing argument 1 of 'int isempty(queue&)'
Hamming.c:100: error: invalid initialization of reference of type 'queue&' from expression of type 'queue*'
Hamming.c:59: error: in passing argument 1 of 'int leave(queue&)'
Hamming.c:102: error: invalid initialization of reference of type 'queue&' from expression of type 'queue*'
Hamming.c:53: error: in passing argument 1 of 'void join(queue&, int)'

pls help!!

In C languge you do not use the c++ reference & operator, but pointers

void join(queue *q,int elem)
{
        q->items[q->rear]=elem;
        q->rear++;
}

And invoke it like this: join( &que_write,1);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.