Question

I'm trying to learn about C using codeblock, part of my assignment is to use queue as an input and output method.

#include <stdio.h>
#include <stdlib.h>

typedef struct QUEUE {
    int head, tail;
    int list[4];
} queue;

void create( queue *q ) {
    q->head = 0;
    q->tail = 0;
}

int empty( queue *q ) {
    if ( q->tail == 0 )
        return 1;
    else
        return 0;
}

int full( queue *q ) {
    if ( q->tail == 4 )
        return 1;
    else
        return 0;
}

void enqueue( queue *q ) {
    int i, data;
    if ( full( q ) == 1 )
        printf( "\nQueue is Full" );
    else {
        printf( "\nEnqueue Integer : " );
        scanf( "%d", &data );
        q->list[q->tail] = data;
        q->tail++;
    }
}

void dequeue( queue *q ) {
    int i;
    if ( empty( q ) == 1 )
        printf( "\n Queue is Empty" );
    else
        q->head++;
}

void main( ) {
    int select;
    queue q;

    create( &q );

    while ( 1 ) {
        printf( "\n1->Enqueue \t 2->Dequeue \t 3->Exit: " );
        scanf( "%d", &select );

        switch ( select ) {
        case 1:     enqueue( &q );
                    break;
        case 2:     dequeue( &q );
                    break;
        default:    goto out;
        }
    }
out:
    exit( EXIT_SUCCESS );
}

Thats my code, the dequeue function returns queue is empty at first, but after I enqueue an integer, it wont dequeue at all. Thanks for your help. I'm using codeblock to compile my program.

Was it helpful?

Solution

Create a new variable size inside the struct QUEUE, which is initialized to 0 when the queue is created and refers to the amount of data stored in you queue.

typedef struct QUEUE {
    int head, tail,size;
    int list[4];
} queue;

void create( queue *q ) {
    q->head = 0;
    q->tail = -1;
    q->size=0;
}

Hence size 0 would mean no data is present in our queue and size 4 will mean the queue is full.

int empty( queue *q ) {
    if ( q->size==0 )
        return 1;
    else
        return 0;
}

int full( queue *q ) {
    if ( q->size==4)
        return 1;
    else
        return 0;
}

When you enqueue a data, size is increased by 1 and when you dequeue a data, size is decreased by 1.

void enqueue( queue *q ) {
    int i, data;
    if ( full( q ) == 1 )
        printf( "\nQueue is Full" );
    else {
        printf( "\nEnqueue Integer : " );
        scanf( "%d", &data );
        q->tail++;
        q->size++;
        if(q->tail==4){
            q->tail=0;
        }
        q->list[q->tail] = data;
        if(tail==4){
            q->tail==0;
        }
    }
}

void dequeue( queue *q ) {
    int i;
    if ( empty( q ) == 1 )
        printf( "\n Queue is Empty" );
    else{
        q->head++;
        q->size--;
    }
}

NOTE-Your code lacked the circular flow of queue. When tail has crossed the last block of memory allocated, it should get to the first block.

So, we added this inside the enqueue function:

if(tail==4){
                q->tail==0;
            }

OTHER TIPS

Okay, as per your comment, you seem to want to have your queue get reset when you pass its address into the dequeue function. This will be a kind of a superficial answer, but why don't you just reset the q->tail back to 0 inside the dequeue function whenever necessary?

void dequeue( queue *q ) {
    int i;
    if ( ... )
        printf( ... );
    else
        q->tail = 0;    // <-- changed this
}

The head property inside the structure seems to have no use at all, at least for now.

A queue is a FIFO (First In, First Out), and you have a fixed sized FIFO (4 integers) along with a "pointer" to the head and tail.

Looking at your code, tail points to the next available space, while head points to the first filled space.

When you enqueue you add the value and increment the value in tail.

When you dequeue you increment the value in head.

This is okay, except that head and tail are supposed to be indices of an array. At no point to you decrement the values stored inside them. This means that once you have added 4 values to the queue, you can never add any more.

You also assume that the list is empty when tail == 0, but what if head == tail and head == 1, according to your code, the list is empty in this case, but the empty function won't know that it's empty.

Since you are not using dynamic memory, I would remove the head variable altogether.

When you dequeue, I would move the values in the internal array into the previous index. So list[1] gets put into list[0]. And then decrement tail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top