Home > Blockchain >  how to display all elements in queue?
how to display all elements in queue?

Time:12-04

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define max 100

char name[max][80], data[80];
int front = 0;
int rear = 0;

int enqueue();
int dequeue();
int peek();
void display();

int main() {
    int value;
    int ch;
    printf("------------------------------\n");
    printf("\tMenu");
    printf("\n------------------------------");
    printf("\n [1] ENQUEUE");
    printf("\n [2] DEQUEUE");
    printf("\n [3] PEEK");
    printf("\n [4] DISPLAY");
    printf("\n------------------------------\n");
    while(1)
    {
        printf("Choice : ");
        scanf("%d", &ch);
        switch(ch) {
            case 1 : // insert
                printf("\nEnter the Name : ");
                scanf("%s",data);
                value = enqueue(name, &rear, data);
                if(value == -1 )
                    printf("\n QUEUE is Full \n");
                else
                    printf("\n'%s' is inserted in QUEUE.\n\n",data);
                break;
            case 2 : // delete
                value = dequeue(name, &front, &rear, data);
                if( value == -1 )
                    printf("\n QUEUE is Empty \n");
                else
                    printf("\n Deleted Name from QUEUE is : %s\n", data);
                printf("\n");
                break;
            case 3:
                value = peek(name, &front, &rear, data);
                if(value != -1)
                {
                    printf("\n The front is: %s\n", data);
                }
                break;
            case 4:
            display();
              break;
            case 5 : exit(0);
            default: printf("Invalid Choice \n");
        }
    }
    return 0;
}


int enqueue(char name[max][80], int *rear, const char data[80]) {
    if(*rear   1 == max)
        return -1;
    strcpy(name[*rear], data);
    (*rear)  ;
    return 1;
}

int dequeue(char name[max][80], int *front, int *rear, char data[80])
{
    if(*front == *rear)
        return -1;

    strcpy(data, name[(*front)  ]);
    return 1;
}

int peek(char name[max][80], int *front, int *rear, char data[80]) {
    if(*front == *rear) {
        printf(" QUEUE IS EMPTY\n");
        return -1;
    }
    strcpy(data, name[*front]);
    return 1;
}

void display(char name[max][80], int *front, int *rear, char data[80])
{
    if(*front == -1 || *front > *rear)
    {
        printf("\n QUEUE IS EMPTY");
    }
    else
    {
        for(int i = *front; i<= *rear; i  )
        {
            printf("\t %s",data[i]);
        }
    }
}

Student here.

I need to display all elements inside the queue, but my program ends whenever I click option number 4, For example, the user inputs four names, "Jennie, Lisa, Jisoo, Rose", when the user selects option number 4, the program should print all 4 names. Even though I only input one name and need to print it, the program just ends. How to fix this?

CodePudding user response:

There seems to be a few issues with the implementation of the display() function. First, the function definition has 4 parameters, but the implementation only has one parameter (data). This means that the values of the name, front, and rear variables are not being passed to the function, which will cause unexpected behavior.

To fix this, the function definition should match the implementation, so you should remove the extra 3 parameters from the function definition. Then, you can access the name, front, and rear variables directly within the function since they are already defined in the global scope.

The next issue is that you are iterating through the data array and printing each element, but the data array only contains the value of the last element that was dequeued. Instead, you should be iterating through the name array and printing the values of each element in that array.

Here is an updated implementation of the display() function that should fix these issues:

void display()
{
    if(front == -1 || front > rear)
    {
        printf("\n QUEUE IS EMPTY");
    }
    else
    {
        for(int i = front; i<= rear; i  )
        {
        printf("\t %s", name[I]);
        }
    }
}

Note that I also removed the unused data parameter from the function definition. You can now call this function from the main() function to display all the elements in the queue.

  • Related