Data Structure: Queue

Stack VS Queue difference:

 

code:

/*
 * C Program to Implement a Queue using an Array
 */

#include <stdio.h>
#include <conio.h>
#define N 10

int queue[N];
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()*/


insert()
{
    int element;

    if (rear == N - 1)
    printf("Queue Overflow \n");

    else
    {
        if (front == - 1)
        /*If queue is initially empty */
        front = 0;
        
        printf("Inset the element in queue : ");
        scanf("%d", &element);
        rear = rear + 1;
        queue[rear] = element;
    }
} 
 

delete()
{
    if (front == - 1 || front > rear)
    {
        printf("Queue Underflow \n");
        return ;
    }

    else
    {
        printf("Element deleted from queue is : %d\n", queue[front]);
        front = front + 1;
    }
} 


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[i]);
        printf("\n");
    }
} 


Output:-

1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit

Enter your choice : 1
Inset the element in queue : 10

1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit

Enter your choice : 1
Inset the element in queue : 50

1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit

Enter your choice : 1
Inset the element in queue : 30

1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit

Enter your choice : 1
Inset the element in queue : 60

1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit

Enter your choice : 2
Element deleted from queue is : 10

1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit

Enter your choice : 3
Queue is :
50 30 60

1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit

Enter your choice : 4

My implementation:

#include<stdio.h>
#include<stdlib.h>
#define MAX 10
int q[MAX];
int front=-1;
int rear=-1;
void Insert()
{
    int element;
    if(rear==MAX-1)
        printf("\nQueue Overflow\n");
    else
    {
        if(front==-1)
            front=0;
        printf("Insert the element in queue: ");
        scanf("%d",&element);
        rear=rear+1;
        q[rear]=element;
    }
}

void Delete()
{
    if(front==-1||front>rear)
    {
        printf("\nQueue Underflow\n");
        return;
    }
    else
    {
        printf("\nElement deleted from queue is: %d\n",q[front]);
        front=front+1;
    }
}
void Display()
{
    int i;
    if(front==-1)
        printf("\nQueue is empty\n");
    else
    {
        printf("\nQueue is: ");
        for(i=front; i<=rear; i++)
            printf("%d ",q[i]);
        printf("\n");
    }

}

int main()
{


    int choice;
    while(1)
    {

        printf("1. Insert element to queue\n");
        printf("2. Delete elements\n");
        printf("3. Display all elements\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");

        }
    }


    return 0;
}

Another Video:

Code from the video Mycodeschool(Using circular queue):

#include<stdio.h>
using namespace std;
#define MAX_SIZE 101
int A[MAX_SIZE];
int fronta,rear;

bool IsEmpty()
{
    return(fronta==-1 && rear==-1);
}

bool IsFull()
{
   // if(rear==MAX_SIZE-1)
    if((rear+1)%MAX_SIZE==fronta)
        return true;
    else
        return false;
}
void Enqeue(int x)
{
    printf("Enqueuing: %d",x);
    if(IsFull()){
        printf("Error: Queue is full\n");
        return;
    }
    else if(IsEmpty())
    {
        fronta=rear=0;
    }
    else
    {
        rear=(rear+1)%MAX_SIZE;
    }
    A[rear]=x;
}

void Dequeue()
{
    printf("Dequeuing:  ");
    if(IsEmpty())
        return;
    else if(fronta==rear)
        fronta=rear=1;
    else
        fronta=(fronta+1)%MAX_SIZE;
}

int Froonta()
{
    if(fronta==-1)
    {
        printf("Can't return from an empty queue\n");
        return -1;
    }
    return A[fronta];
}

void print()
{
    int counta=(rear+MAX_SIZE-fronta)%MAX_SIZE+1;
    printf(" Queue:  ");
    int i;
    for(i=0; i<counta; i++)
    {
        int index=(fronta+i)%MAX_SIZE;
        printf("%d ",A[index]);  //Index of element while traaversing circulrly from front
    }
    printf("\n");
}

int main()
{
    Enqeue(2);
    print();
    Enqeue(4);
    print();
    Enqeue(6);
    print();
    Enqeue(8);
    print();
    Dequeue();
    print();
    Enqeue(8);
    print();
    return 0;
}

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *