Thread: FIFO Queue

  1. #16
    Registered User
    Join Date
    Apr 2021
    Posts
    139
    Quote Originally Posted by gajya View Post
    @Salem I think I have done

    Code:
    #include<stdio.h>  
    #include<stdlib.h> 
          
    struct Queue {
      int data[10];
      int front;
      int back;
    };
     
     
    void queueInit(struct Queue *q)  // Initialise
    {
        (*q).data[0] = 0;
        (*q).front = 0;
        (*q).back = 0;     
    } 
     
     
    void queueEnqueue(struct Queue *q, int data)  // enqueue a value
    {
        (*q).data[0] = data;   // add 1 into queue 
        (*q).front = 0;
        (*q).back = 0;     
    }
      
    int main() 
    { 
    
    
      struct Queue q;
      queueInit(&q); 
      queueEnqueue(&q, 1);  // enqueue a value
      
        
      return 0; 
    }
    Now I need help in enqueue
    Okay, but ...

    First, let's talk about notation:

    Code:
        (*q).data[0] = data;
     
        q->data[0] = data;
    Those two lines ^^ mean exactly the same thing.

    In C, the -> operator was created specifically to create a shorthand for (*pointer).member expressions. In much the same way that the ++ and -- operators were created as a shorthand for x = x + 1. They're the same under the hood.

    So, first of all, you wrote a completely valid expression, and that statement is totally correct.

    But, second of all, the "idiomatic" way to write that is to use the -> operator, and every C programmer (including me) is going to read that, wince, and go "okay, but ..."

    Next, let's talk about "meanings." You have a queue of your own data type. How do you know when the queue is empty? How do you know when the queue is not empty? How do you know how many values are in the queue? Also, how do you handle an error? If the queue has 10 values in it and I call enqueue, what will happen? If the queue has no values in it and I call dequeue, what will happen?

    (This is not a trick question: these are normal conditions that can be expected to occur. The designer of the library, which in this case is YOU has to make the rules for this. So... what are the rules?)

    Now here are some more questions. Given this code:

    Code:
        typedef struct Queue Queue;
        
        extern bool    q_is_empty(Queue *);
        extern size_t  q_length(Queue *);
        extern error_t q_enqueue(Queue *, value_t data);
    
        struct Queue * q = &(struct Queue){ .data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, .front=0, .back=0 };
    
        printf("%s\n", q_is_empty(q) ? "empy" : "not empty");   // what does this print?
    
        printf("%d\n", q_length(q)); // what does this print?
    
        printf("%d\n", q_enqueue(q, (value_t){0})); // what does this print?
    
        // what does the q structure look like after that last call to enqueue?
    What do the statements print, and what are the new values in the queue structure? (This is all about you making decisions, and understanding how these things are going to be used.)


    [/code]

  2. #17
    Registered User
    Join Date
    Oct 2019
    Posts
    81
    Quote Originally Posted by Salem View Post
    Code:
    #define CAPACITY  10
    #define  True  1
             
    struct Queue {
      int data[CAPACITY];
      int front;
      int back;
      int size;
    };
    Thanks for excellent advice. I have created one function to print enqueue. I want to print enqueue a value

    Code:
    #include<stdio.h>
    
    #define CAPACITY  5
    #define  True  1
              
    struct Queue {
      int data[CAPACITY];
      int front;
      int back;
      int size;
    };
         
         
    void queueInit(struct Queue *q)  // Initialise
    {
        q->data[0] = 0;
        q->front = 0;
        q->back = 0;     
        q->size = 0;
    } 
         
     int queueIsEmpty(struct Queue *q) // is it empty?
     {
         return q->size == 0 ;
     }
         
      int queueIsFull(struct Queue *q)  // is it Full?
     {
         return q->size == CAPACITY; 
     }
    void queueEnqueue(struct Queue *q, int data)  // enqueue a value
    {
        if (!queueIsFull(q))  // it's not full
        {
             q->data[q->front] = data; // store at the front of the queue
             q->front++; // move to next free slot
             q->size++;  // and count the number added
             
        }
    }
    
    
    void ShowEnqueue(struct Queue *q)  // print enqueue a value
    {
        int i = 0;
        
        for ( i = 0; i < 5; i++)
        {
            // what need to print value of Enqueue ?
            // printf(" %d", varaible ) ?
        }
    }
    int main ()
    {
         struct Queue q;
         queueInit(&q); 
         queueEnqueue(&q, 11);  // enqueue a value
         queueEnqueue(&q, 12);  // enqueue a value
         queueEnqueue(&q, 13);  // enqueue a value
         queueEnqueue(&q, 14);  // enqueue a value     
         queueEnqueue(&q, 15);  // enqueue a value
    
           ShowEnqueue(struct Queue &q) 
        return 0;
    }
    what I need to print value of Enqueue in below function ?

    Code:
    void ShowEnqueue(struct Queue *q)  // print enqueue a value
    {
        int i = 0;
        
        for ( i = 0; i < 5; i++)
        {
            // what need to print value of Enqueue ?
            // printf(" %d", varaible ) ?
        }
    }
    Last edited by gajya; 05-15-2021 at 12:48 AM.

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ShowEnqueue(struct Queue &q)
    Look at all the other calls, does this look like a call?

    > // what need to print value of Enqueue ?
    I'm not sure you're even trying.
    No more drip feeding, make an effort.

    16 hours ago, you were closer.
    > printf ("%d", q->data);

    > gajya, Join Date Nov 2019
    18 months is a long time to be stuck on the first page of the book.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User
    Join Date
    Oct 2019
    Posts
    81
    Quote Originally Posted by Salem View Post
    > ShowEnqueue(struct Queue &q)

    > gajya, Join Date Nov 2019
    18 months is a long time to be stuck on the first page of the book.
    There was some misunderstanding

    Code:
    #include<stdio.h>
    
    #define CAPACITY  5
    #define  True  1
              
    struct Queue {
      int data[CAPACITY];
      int front;
      int back;
      int size;
    };
         
         
    void queueInit(struct Queue *q)  // Initialise
    {
        q->data[0] = 0;
        q->front = 0;
        q->back = 0;     
        q->size = 0;
    } 
         
     int queueIsEmpty(struct Queue *q) // is it empty?
     {
         return q->size == 0 ;
     }
         
      int queueIsFull(struct Queue *q)  // is it Full?
     {
         return q->size == CAPACITY; 
     }
    void queueEnqueue(struct Queue *q, int data)  // enqueue a value
    {
        if (!queueIsFull(q))  // it's not full
        {
             q->data[q->front] = data; // store at the front of the queue
             q->front++; // move to next free slot
             q->size++;  // and count the number added
    		 
        }
    }
    
    
    void ShowEnqueue(struct Queue *q)  // print enqueue a value
    {
    	int i = 0;
    	
    	for ( i = 0; i < 5; i++)
    	{
    		
    		printf(" %d", *(q->data + i));
    		q->data + i;
    	}
    }
    int main ()
    {
    	 struct Queue q;
         queueInit(&q); 
         queueEnqueue(&q, 11);  // enqueue a value
    	 queueEnqueue(&q, 12);  // enqueue a value
    	 queueEnqueue(&q, 13);  // enqueue a value
    	 queueEnqueue(&q, 14);  // enqueue a value	 
    	 queueEnqueue(&q, 15);  // enqueue a value
    	 
    	 ShowEnqueue( &q);  // print enqueue a value
    
    
    	return 0;
    }
    result
    11 12 13 14 15

  5. #20
    Registered User
    Join Date
    Oct 2019
    Posts
    81
    I have spent enough time to solve problem but program doesn't give expected output

    Code:
    #include<stdio.h> 
    #define CAPACITY  5
    #define  True  1
               
    struct Queue {
      int data[CAPACITY];
      int front;
      int back;
      int size;
    };
          
          
    void queueInit(struct Queue *q)  // Initialise
    {
        q->data[0] = 0;
        q->front = 0;
        q->back = 0;     
        q->size = 0;
    } 
          
     int queueIsEmpty(struct Queue *q) // is it empty?
     {
         return q->size == 0 ;
     }
          
      int queueIsFull(struct Queue *q)  // is it Full?
     {
         return q->size == CAPACITY; 
     }
    void queueEnqueue(struct Queue *q, int data)  // enqueue a value
    {
        if (!queueIsFull(q))  // it's not full
        {
             q->data[q->front] = data; // store at the front of the queue
             q->front++; // move to next free slot
             q->size++;  // and count the number added
              
        }
    }
    
    
    void queueDequeue(struct Queue *q, int data)  // enqueue a value
    {
        if (queueIsFull(q))  // it's not full
        {
             q->data[q->back] = data; // store at the back of the queue
             q->back++; // move to next free slot
             q->size++;  // and count the number added
              
        }
    }
      
     
    void ShowEnqueue(struct Queue *q)  // print enqueue a value
    {
        int i = 0;
         
        for ( i = 0; i < 5; i++)
        {
             
            printf(" %d", *(q->data + i));
            q->data + i;
        }
    }
    int main ()
    {
         struct Queue q;
         queueInit(&q); 
         queueEnqueue(&q, 11);  // enqueue a value
         queueEnqueue(&q, 12);  // enqueue a value
         queueEnqueue(&q, 13);  // enqueue a value
         queueEnqueue(&q, 14);  // enqueue a value   
         queueEnqueue(&q, 15);  // enqueue a value
          
         ShowEnqueue( &q);  // print enqueue a value
    	 queueDequeue(&q, 16);  // enqueue a value
    	 printf("\n");
    	 ShowEnqueue( &q);  // print enqueue a value
     
     
        return 0;
    }
    Program output
    11 12 13 14 15
    16 12 13 14 15

    expected output
    11 12 13 14 15
    16 15 14 13 12

  6. #21
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Why does dequeue care if the quque is full or not?

  7. #22
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Had a good look at this, and it seems you don't really understand what the dequeue operation should be doing.

    It should remove the oldest item in the queue, and reduce the length of the queue by one.

    If you enqueue the numbers 11 through 15, show the queue, dequeue an item, then show the queue again the expected expected output should be:

    11 12 13 14 15
    12 13 14 15

    Also nothing is stopping 'front' or 'back' from increasing and exceeding the capacity of the data array, and in doing so corrupting memory.
    Last edited by hamster_nz; 05-16-2021 at 03:18 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. FIFO queue vs Link List
    By DeeMan in forum C Programming
    Replies: 8
    Last Post: 02-16-2013, 08:29 PM
  2. Priority queue and FIFO
    By Elysia in forum C++ Programming
    Replies: 12
    Last Post: 02-29-2012, 02:32 PM
  3. Help with FIFO queue
    By Martin_T in forum C Programming
    Replies: 10
    Last Post: 11-08-2009, 10:30 AM
  4. understanding queue FIFO
    By arastoo.s in forum C Programming
    Replies: 6
    Last Post: 08-18-2009, 10:16 AM
  5. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM

Tags for this Thread