简体   繁体   中英

Queue implementation program in C

Ive just started C and I'm trying to do this queue implementation program from my book but it isn't working. Its giving the error that function qfull and qempty are undefined. Even after properly declaring the function its still giving more errors.

#include <stdio.h>
#include <process.h>
#include <conio.h>
#define QUEUE_SIZE 5

void main()
{
    void insert_rear(int, int *, int *);
    void delete_front(int *, int *, int *);
    void display(int *, int, int);
    int choice, item, f, r, q[10];
    /* Queue is empty */
    f = 0; /* Front end of queue*/
    r = -1; /* Rear end of queue*/
    for (;;)
    {
        clrscr();
        printf("\t\t\t Ordinary Queue Operation\n\n");
        printf("\t\t\t 1 …. Push / Insert\n");
        printf("\t\t\t 2 …. Pop / Delete\n");
      printf("\t\t\t 3 …. View / Display\n");
        printf("\t\t\t 4 …. Exit\n\n\n");
        printf("\t\t\t Enter the choice : "); scanf("%d", &choice);
        switch (choice)
        {
            case 1: // push into the queue
                printf("Enter the item to be inserted : "); scanf("%d", &item);
                insert_rear(item, q, &r);
                continue;
         case 2: // pop from the queue
                delete_front(q, &f, &r);
                break;
            case 3: // display queue
                display(q, f, r);
                break;
            case 4:
                exit(0);
         default:
                printf("\t\t\tInvalid Input – Try Again");
        } // end of switch
        getch();
    }// end of for
} // end of main



/*******************/



void insert_rear(int item, int q[], int *r)
{
    if (qfull(*r)) /* Is queue full ? */
    {
        printf("\t\t\tQueue overflow\n");
        return;
    }
    /* Queue is not full */
    q[++(*r)] = item; /* Update rear pointer and insert a item */
}



/*—————————————————————– */



void delete_front(int q[], int *f, int *r)
{
    if (qempty(*f, *r))
    {
        printf("\t\t\tQueue underflow\n");
        return;
    }
    printf(" Pop Successfull, element deleted = %d ",q[(*f)++]);
    if(*f> *r)
    {
        *f=0,*r=-1;
    }
}



/*********************************************************/



void display(int q[], int f, int r)
{
    int i;
    if (qempty(f,r))
    {
        printf("Queue is empty\n");
        return;
    }
    printf("\t\t\t Queue Container\n\n");
    for(i=f;i<=r; i++)
        printf("\t\t\t| %5d |\n",q[i]);
}



/**********************************************/



int qempty(int f, int r)
{
    return (f>r)?1:0;
    /* returns true if queue is empty otherwise returns false */
}



/**********************************************/



int qfull(int r)
{ /* returns true if queue is full otherwise false */
    return (r==QUEUE_SIZE-1)?1:0;
}

You must declare functions before calling them. Add the prototypes at the beginning of your program, such as:

int qempty( int f, int r );
int qfull( int r );

You should put these declarations:

void insert_rear(int, int *, int *);
void delete_front(int *, int *, int *);
void display(int *, int, int);

outside of main (and above it). You also need to declare qfull and and qempty by these declarations. If a function isn't defined in a file above where it is called, then it has to be declared above where it has been called. Otherwise, the compiler can't 'see' them.

The thing is the functions are called before they are defined. Try moving them to the top of the program before main.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM