Sunday, February 16, 2020

Data Structure- 4. Queue Concept

Like Stack data Structure ! Queue is also another type of Data Structure which follows the concept of FIFO ! Thas is the element which comes first will be deleted first!

Concept to code !- We maintain two variables called front and Rear, The Front will always point to the first element in the queue and the Rear will  element will always point to the current element that is just inserted into the queue.
 If you will follow this than lets suppose we are given the following list of element

<5,6,7,8> then in queue it will look like, Initially 

                        F=R=-1

Thus whenever you wish to insert ( Enqueue) an element then simply increment the rear and put the item there ! when you wish to delete ( Dequeue ) the item the you simply increment the front and take out the item from there!

Thus here is the Code to push and pop an element from the queue !
/******************************************************************************
Queue Concept using an Array !               
@Mukesh Mann dated 16-02-2020
*******************************************************************************/
/*
 * C Program to Implement a Queue using an Array
 */
#include <stdio.h>

#define MAX 50

void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
    int choice;
    while (1)
    {
        printf("1.Insert element to queue \n");
        printf("2.Delete element from queue \n");
        printf("3.Display all elements of queue \n");
        printf("4.Quit \n");
        printf("Enter your choice : ");
        scanf("%d", &choice);
        switch (choice)
        {
            case 1:
            insert();
            break;
            case 2:
            delete();
            break;
            case 3:
            display();
            break;
            case 4:
            exit(1);
            default:
            printf("Wrong choice \n");
        } /* End of switch */
    } /* End of while */
} /* End of main() */

void insert()
{
    int add_item;
    if (rear == MAX - 1)
    printf("Queue Overflow \n");
    else
    {
        if (front == - 1)
        /*If queue is initially empty */
        front = 0;
        printf("Inset the element in queue : ");
        scanf("%d", &add_item);
        rear = rear + 1;
        queue_array[rear] = add_item;
    }
} /* End of insert() */

void delete()
{
    if (front == - 1 || front > rear)
    {
        printf("Queue Underflow \n");
        return ;
    }
    else
    {
        printf("Element deleted from queue is : %d\n", queue_array[front]);
        front = front + 1;
    }
} /* End of delete() */

void display()
{
    int i;
    if (front == - 1)
        printf("Queue is empty \n");
    else
    {
        printf("Queue is : \n");
        for (i = front; i <= rear; i++)
            printf("%d ", queue_array[i]);
        printf("\n");
    }
} /* End of display() */




No comments:

Post a Comment

9_Regular Expressions

Regular expressions- Sometimes in HTML, the developer defines more than one class name ( that’s class input has more than one name Here ...