You are on page 1of 2

IDE-11 Data Structures

Department of Electrical Engineering

Queue (Array Implementation)


#include<stdio.h> #define CAPACITY 50 int queue_arr[CAPACITY]; int rear = -1; int front = -1; //Function to enqueue (insert) a new element void enqueue() { int element; if(rear==CAPACITY-1) { printf("\nQueue Overflow\n"); } else { if(front== -1)//If queue is empty front = 0; printf("\nEnter the element to enqueue(insert):"); scanf("%d",&element); rear=rear+1; queue_arr[rear]=element; } }//end of insert() //Function to dequeue(delete) an element of queue void dequeue() { if(front == -1 || front > rear) { printf("\nQueue Underflow\n"); } else { printf("\nElement dequeued is: %d\n",queue_arr[front]); front=front+1; } }//end of dequeue() //Function to display the contents of queue void display() { M. Usman Aslam (usmanaslam_rcet@hotmail.com) Lecturer , RCET Gujranwala 1/2

IDE-11 Data Structures int i; if(front == -1 || front > rear) { printf("\nQueue is empty\n"); } else { printf("\nQueue contains: "); for(i=front;i<=rear;i++) printf("%d ",queue_arr[i]); printf("\n"); } }//end of display()

Department of Electrical Engineering

int main() { int choice; while(1) { printf("\n\n1.ENQUEUE(INSERT)"); printf("\n2.DEQUEUE(DELETE)"); printf("\n3.DISPLAY"); printf("\n2.QUIT"); printf("\nEnter your choice:"); scanf("%d",&choice); switch(choice) { case 1: enqueue(); break; case 2: dequeue(); break; case 3: display(); break; case 4: return 0; default: printf("\nYou entered a wrong choice\n"); }//end of switch }//end of while }//end of main() M. Usman Aslam (usmanaslam_rcet@hotmail.com) Lecturer , RCET Gujranwala 2/2

You might also like