C PROGRAM - Queue Implementation using Array

 

// Queue Implementation using Array

 

#include<stdio.h>

int rear,front,queue[20],n;

void create()

  {

     rear=0,front=0;

     printf("Enter the capacity of the queue: ");

     scanf("%d",&n);

     printf("Queue is created \n");

  }

 

  void insert()

  {

     if(rear >= n)

            printf("Queue is full\n");

     else

            printf("Enter the element to insert: ");

            scanf("%d",&queue[rear]);

            rear++;

  }

 

  void del()

  {

     if(front == rear)

            printf("Queue is empty\n");

     else

            printf("%d is deleted from Queue\n", queue[front]);

            front++;

  }

 

  void list()

  {

     int i;

     if(front == rear)

            printf("Queue is empty\n");

     else

     {

            printf("Element of the Queue is\n");

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

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

     }

  }

 

 

  void modify()

  {

     int key;

     printf("Enter the element to be modified: ");

     scanf("%d",&key);

     queue[rear-1] = key;

     printf("Modification is done\n");

  }

 

void main()

{

  int ch;

  clrscr();

 

  printf("Queue Implementation using Array\n");

  printf(“-------------------------------------------\n”);

  printf("\t Main Menu \n");

  printf("\t ********* \n");

  printf("\t 1. Creation \n");

  printf("\t 2. Insertion \n");

  printf("\t 3. Deletion \n");

  printf("\t 4. Modification \n");

  printf("\t 5. Listing of elements \n");

  printf("\t 6. Exit\n");

 

  do

  {

    printf("\nEnter your choice[1...6]: ");

    scanf("%d",&ch);

    switch(ch)

    {

            case 1: create(); break;

            case 2: insert(); break;

            case 3: del(); break;

            case 4: modify(); break;

            case 5: list(); break;

            case 6: end(); break;

            default: printf("Enter your option correctly \n");

    }

  }while(ch!=6);

}

 

 

 

 

 

OUTPUT:

 

Queue Implementation using Array

-------------------------------------------

         Main Menu

         *********

         1. Creation

         2. Insertion

         3. Deletion

         4. Modification

         5. Listing of elements

         6. Exit

 

Enter your choice[1...6]: 1

Enter the capacity of the queue: 5

Queue is created

 

Enter your choice[1...6]: 2

Enter the element to insert: 10

 

Enter your choice[1...6]: 2

Enter the element to insert: 20

 

Enter your choice[1...6]: 2

Enter the element to insert: 30

 

Enter your choice[1...6]: 5

Element of the Queue is

10      20      30

 

Enter your choice[1...6]: 4

Enter the element to be modified: 40

Modification is done

 

Enter your choice[1...6]: 5

Element of the Queue is

10      20      40

Enter your choice[1...6]: 3

10 is deleted from Queue

 

Enter your choice[1...6]: 3

20 is deleted from Queue

 

Enter your choice[1...6]: 3

40 is deleted from Queue

 

Enter your choice[1...6]: 3

Queue is empty

 

Enter your choice[1...6]:

Comments

Popular posts from this blog

Backtracking - N-Queens Problem, Sum of Subsets, Graph Colouring, Hamiltonian Cycle

Divide and Conquer Technique - Binary Search, Quick Sort, Merge Sort

GRAPH THEORY