Enqueue

This is a discussion on Enqueue within the C Programming forums, part of the General Programming Boards category; How would i change this to were it puts the elements in priority order? with the highest priority at the ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    6

    Question Enqueue

    How would i change this to were it puts the elements in priority order? with the highest priority at the front. Thanx.


    void ENQUEUE(queue *Q,ElementType X)
    /* insert the element X at the front of queue Q */
    {
    if Q->size == max_size /*check the array is full */
    ERROR("array is full"); /* print warning message */
    else
    { /*insert the element at the front of the queue */
    Q->rear = (Q->rear + 1)%max_size; /*move rear 1 clockwise*/
    Q->entry[Q->rear] = X; /*insert element at rear*/
    Q->size ++;
    }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > void ENQUEUE(queue *Q,ElementType X)
    > /* insert the element X at the front of queue Q */
    > {

    You should check to see that Q actually exists first.

    if( !Q )
    { ERROR("Q is NULL."); return; }

    > if Q->size == max_size /*check the array is full */

    You should have parenthesis around this.

    if( Q->size == max_size )

    This is wrong. Why yould you...

    > ERROR("array is full"); /* print warning message */
    > else
    > { /*insert the element at the front of the queue */
    > Q->rear = (Q->rear + 1)%max_size; /*move rear 1 clockwise*/
    > Q->entry[Q->rear] = X; /*insert element at rear*/
    > Q->size ++;
    > }
    > }
    ...increment the Q size if your array is full?

    { ERROR("Q is full."); return; }

    Q->rear = ( Q->rear + 1 ) % max_size; /* do whatever */
    Q->entcy[Q->rear] = X; /* do whatever */
    Q->size++; /* do whatever */
    }


    You need to really learn how to use parenthesis and braces effectivly.

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with grid problem (dynamic programming)
    By scp89 in forum C Programming
    Replies: 15
    Last Post: 05-07-2009, 01:16 PM
  2. Passing lists to functions
    By Griever in forum C Programming
    Replies: 23
    Last Post: 11-14-2008, 05:26 PM
  3. enqueue and dequeue pointer
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-28-2007, 03:21 PM
  4. enqueue and deuque funct.
    By m0ntana in forum C Programming
    Replies: 5
    Last Post: 04-22-2007, 02:20 PM
  5. Making use of queue.c
    By micmac700 in forum C Programming
    Replies: 4
    Last Post: 09-22-2006, 04:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21