Queue (Exercise: 1-3)

Question: 

Exercise 1:

Create Queue (Enqueue)

Create queue allocates a new node for the queue. It first initializes the front and rear pointers to null. If the queue is empty then insert new node in queue and update both the front and rear pointer with the address of the new node and if queue is not empty then update the rear pointer with the address of the new node

Exercise 2:

Delete node from Queue (Dequeue)

It begins by checking to make sure there are data in queue. If there are, it takes the addresses of the data being deleted , adjust the front pointer and remove the allocated memory space.

Exercise 3:

Print Queue

The print function will print the entire queue data.


Soultion: 


#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

#define SIZE 100


unsigned int size = 0;

int front = 0;

int rear = 0;

int queue[SIZE];


void enqueue();

void dequeue();

void displayQueue();

void sizeOfQueue();


int main ()

{

    int choice;


    while(1)

    {

        printf("--------------------------------------------------------\n");

        printf("                         Queue                          \n");

        printf("--------------------------------------------------------\n");

        printf("1. Enqueue\n");

        printf("2. Dequeue\n");

        printf("3. Items in the queue\n");

        printf("4. Size of the queue\n");

        printf("5. Exit\n");

        printf("--------------------------------------------------------\n");


        printf("Select your option: ");

        scanf("%d", &choice);


        switch(choice)

        {

            case 1:

                enqueue();

                break;


            case 2:

                dequeue();

                break;


            case 3:

                displayQueue();

                break;


            case 4:

                sizeOfQueue();

                break;


            case 5:

               printf("\nExit...!!!\n");

               exit(5);


            default:

                printf("Invalid choice!!! Please set between (1-5)!!!");

                break;

        }

        printf("\n\n");

    }

}


void enqueue()

{

    int item;


    if(rear == SIZE)

    {

        printf("Queue is Full!!!\n");

    }

    else if (front == 0)

    {

        printf("\nEnqueue element: ");

        scanf("%d", &item);

        printf("Item added to the queue.\n");


        queue[rear] = item;

        rear = rear + 1;

    }

}


void dequeue()

{

    if (front == rear)

    {

        printf("\nQueue is empty!!!\n");

    }

    else

    {

        printf("\nThe item dequeued from the queue is: %d\n\n", queue[front]);

        front = front + 1;

    }

}


void displayQueue()

{

    if (front == rear)

    {

        printf("\nQueue is empty!!!");

    }

    else

    {

        printf("Items in Array...\n\n");


        for (int i=front; i<rear; i++)

        {

            printf("%d\n", queue[i]);

        }

        printf("\n\n");

    }

}


void sizeOfQueue()

{

    if (front == rear)

    {

        printf("\nQueue is empty!!!");

    }

    else

    {

       printf("Queue size is = %d", rear);

    }


}





Previous Post Next Post