Ad:
 
  • C Discussion Thread
  • Marked Solved
  • Views: 1278
  • C RSS
Mar 27th, 2009
0

how to implement queues in C?URGENT!!!!!

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


  1.  
  2. //structure for queue
  3. struct queue{
  4. int items[MAX];
  5. int front;
  6. int rear;
  7. };
  8. //decleration of the queues :que_term for the values printed on the terminal
  9. struct queue que_mul2,que_mul3,que_mul5,que_sort,que_write,que_term;
  10.  
  11. //initializing the queue
  12. void InitQueue(queue &q)
  13. {
  14. q.front=0;
  15. q.rear=0;
  16. }
  17. //checking if queue is empty
  18. int isempty(queue &q)
  19. {
  20. if(q.rear==q.front && q.front!=0)
  21. return 1;
  22. else
  23. return 0;
  24. }
  25. //adding an element at the rear of the queue
  26. void join(queue &q,int elem)
  27. {
  28. q.items[q.rear]=elem;
  29. q.rear++;
  30. }
  31. //removing an element from the front of the queue
  32. int leave(queue &q)
  33. {
  34. int elem;
  35.  
  36. elem=q.items[q.front];
  37. q.front++;
  38. return elem;
  39. // }
  40. // else
  41. // return -1;
  42. }

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vartikachandra is offline Offline
18 posts
since Jan 2009
Mar 27th, 2009
0

Re: how to implement queues in C?URGENT!!!!!

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.
Last edited by Ancient Dragon; Mar 27th, 2009 at 5:04 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 4835
Solved Threads: 1778
Still Learning
Ancient Dragon is offline Offline
17,874 posts
since Aug 2005
Mar 27th, 2009
0

Re: how to implement queues in C?URGENT!!!!!

i am invoking these methods as
in main

  1.  
  2. int main(int argc,char *argv[])
  3. {
  4. pthread_t threads[5];
  5. pthread_attr_t attr;
  6. int rc;
  7. void *status;
  8.  
  9.  
  10.  
  11.  
  12. //initializing the queues
  13. InitQueue(que_mul2);
  14. InitQueue(que_mul3);
  15. InitQueue(que_mul5);
  16. InitQueue(que_sort);
  17. InitQueue(que_write);
  18. InitQueue(que_term);
  19.  
  20. //seed que_write with 1
  21. join(que_write,1);
  22.  
  23. }

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:

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

pls help!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vartikachandra is offline Offline
18 posts
since Jan 2009
Mar 27th, 2009
0

Re: how to implement queues in C?URGENT!!!!!

In C languge you do not use the c++ reference & operator, but pointers
  1. void join(queue *q,int elem)
  2. {
  3. q->items[q->rear]=elem;
  4. q->rear++;
  5. }

And invoke it like this:
join( &que_write,1);
Last edited by Ancient Dragon; Mar 27th, 2009 at 5:36 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 4835
Solved Threads: 1778
Still Learning
Ancient Dragon is offline Offline
17,874 posts
since Aug 2005
Mar 27th, 2009
0

Re: how to implement queues in C?URGENT!!!!!

Thanks!!this really helped!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vartikachandra is offline Offline
18 posts
since Jan 2009
This thread is solved. Perhaps start a new thread instead?
This thread is more than three months old. Perhaps start a new thread instead?
Message:
Previous Thread in C Forum Timeline: SetTimer -- CallBack not called
Next Thread in C Forum Timeline: easy ? regarding area of a triangle





About Us | Contact Us | Advertise | Acceptable Use Policy
Build Custom RSS Feed


Follow us on Twitter


© 2010 DaniWeb® LLC