Wednesday 10 September 2014

C Program to implement a Queue using array

Queue 
 A queue is a a linear collection of objects, where objects are inserted and removed according to the first-in first-out (FIFO) principle. An example of a queue is a line of people in a ticket counter. New additions to the line is made to the back of the queue, while removal (or serving) happens in the front. In a queue only two operations are allowed enqueue/insert and dequeue/delete. Enqueue means to insert an item into the back of the queue, dequeue means deleting the front item. 






Following is the implementation of a Queue in C programming language using array. It has three functions for insert, delete and display. 

Insert function takes the value to be inserted as an argument and inserts it to the end of the queue. If the queue is already full, it fails to insert the value and returns -1 as an error code otherwise it inserts the value and returns 0 

Delete function deletes the value from the front of the queue and returns the value. If the queue is already empty, it cannot delete the value from the queue. In this case the function returns -1 as an error code.

Display function is used to display the content of the queue. 

Source Code

    /*
     * C Program to implement a Queue using an array
     */

    #include <stdio.h>

    #define MAX 5

    // Defines the structure MyQueue
    struct MyQueue {   
        int data[MAX];
        int rear;
    };
   
    // Declares a variable of type MyQueue
    struct MyQueue queue1;
   
   
   
    int main() {
        int ch;
        int value;
       
        // Initialise the rear of queue with -1 (for empty queue)       
        queue1.rear = -1;
   
        while (1) {
              printf("\nQUEUE \n");
              printf("1.Insert\n");
              printf("2.Delete\n");
              printf("3.Display\n");
              printf("4.Exit \n");

              printf("Enter your choice :");
              scanf("%d", &ch);

              switch (ch) {
                     case 1:
                          printf("Value to inset in to queue:");
                          scanf("%d", &value);
                          if (insert(value)==-1)
                             printf("Queue is full!");
                          break;
                     case 2:
                          value = delete();
                          if (value==-1)
                             printf("Queue is empty!");
                          else
                              printf("Deleted value: %d", value);
                          break;
                     case 3:
                          display();
                          break;
                    case 4:
                         exit(1);
                    default:
                            printf("Invalid choice \n");
            }
        }
    }


    // insert function takes the value to be inserted to queue as argument and
    // returns -1 if queue is already full and returns 0  if the value is inserted
    int insert(int value) {
        int retval;
       
        // Check whether the queue is full
        if (queue1.rear == MAX - 1)
           retval = -1;
        else {       
            queue1.rear++;
            queue1.data[queue1.rear] = value;
           
            retval = 0;
        }       
         return retval;
    }

    
     // delete function returns -1 if the queue is empty otherwise returns the deleted value
     int delete() {
         int i;
         int retval;
        
         // Check whether the queue is empty
         if (queue1.rear == - 1)  {
            retval = -1;
         }
         else {
             retval=queue1.data[0]; 
             // Shifts the values in the queue to left (front) by one         
             for (i=0;i<queue1.rear;i++)
                 queue1.data[i] = queue1.data[i+1];
            
             queue1.rear --;                        
        }
       
        return retval;       
    }

    // display function displays the content of the queue
    int display() {
        int i;
        if (queue1.rear == - 1)
            printf("Queue is empty \n");
        else {
            printf("Queue is : \n");
            for (i = 0 ; i <= queue1.rear; i++)
                printf("%d ", queue1.data[i]);
            printf("\n");
        }
    }

Output 

QUEUE 
1.Insert 
2.Delete 
3.Display 
4.Exit 
Enter your choice :1 
Value to inset in to queue:100 

QUEUE 
1.Insert 
2.Delete 
3.Display 
4.Exit 
Enter your choice :1 
Value to inset in to queue:200 

QUEUE 
1.Insert 
2.Delete 
3.Display 
4.Exit 
Enter your choice :1 
Value to inset in to queue:300

QUEUE 
1.Insert 
2.Delete 
3.Display 
4.Exit 
Enter your choice :1 
Value to inset in to queue:400

QUEUE 
1.Insert 
2.Delete 
3.Display 
4.Exit 
Enter your choice :1 
Value to inset in to queue:500 


QUEUE 
1.Insert 
2.Delete 
3.Display 
4.Exit 
Enter your choice :1 
Value to inset in to queue:600 
Queue is full!

QUEUE 
1.Insert 
2.Delete 
3.Display 
4.Exit 
Enter your choice :3
Queue is : 100 200 300 400 500 
QUEUE 
1.Insert 
2.Delete 
3.Display 
4.Exit 
Enter your choice :2
Deleted value: 100
QUEUE 
1.Insert 
2.Delete 
3.Display 
4.Exit 
Enter your choice :2
Deleted value: 200

QUEUE 
1.Insert 
2.Delete 
3.Display 
4.Exit 
Enter your choice :2
Deleted value: 300
QUEUE 
1.Insert 
2.Delete 
3.Display 
4.Exit 
Enter your choice :2
Deleted value: 400
QUEUE 
1.Insert 
2.Delete 
3.Display 
4.Exit 
Enter your choice :2
Deleted value: 500
QUEUE 
1.Insert 
2.Delete 
3.Display 
4.Exit 
Enter your choice :2
Queue is empty! 

No comments:

Post a Comment