You are on page 1of 6

MCA semester II

PROGRAM:

Data Structure

2012-15

11. Perform implementation of queue (using array).


#include<stdio.h> void main() { int q[10]={0},i,front=-1,rear=-1,max=10,n,item; printf("\n\tMENU\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n"); do { printf("\nEnter your choice : "); scanf("%d",&n); switch(n) { case 1: if(rear<max-1) { printf("Enter the element : "); scanf("%d",&item); if(rear==-1) { front=0; rear=0; q[rear]=item; } else q[++rear]=item; } else printf("Overflow\n"); break; case 2: if(front>=0) { printf("The deleted item =%d",q[front]); if(front==rear) { front=-1;

Enrollment no.- 07213704412

MCA semester II
rear=-1; } else front++; } else printf("Underflow\n"); break;

Data Structure

2012-15

case 3: if((front==-1)&&(rear==-1)) printf("The queue is empty\n"); else { printf("The elements of the queue are :"); for(i=front;i<=rear;i++) printf("%d\t",q[i]); } break; case 4: break; default: printf("Invalid choice\n"); break; } } while(n!=4); getch(); }

OUTPUT

Enrollment no.- 07213704412

MCA semester II

Data Structure

2012-15

Enrollment no.- 07213704412

MCA semester II

Data Structure

2012-15

12. Perform implementation of queue (using linked list) . PROGRAM:


#include<stdio.h> voidenq(); voiddeq(); void display(); main() { int n; printf("\tMENU\n1.ENQUEUE\n2.DEQUEUE\n3.DISPLAY\n4.EXIT\n"); do { printf("\nEnter your choice\n"); scanf("%d",&n); switch(n) { case 1: enq(); break; case 2: deq(); break; case 3: display(); break; case 4: break; default: printf("Invalid choice\n"); break; } } while(n!=4); } typedefstruct node { int data;

Enrollment no.- 07213704412

MCA semester II
struct node *link; }n; n *front=NULL; n *rear=NULL; voidenq() { int item; n *temp; printf("Enter the item\n"); scanf("%d",&item); temp=(n*)malloc(sizeof(n)); temp->data=item; temp->link=NULL; if(rear==NULL) { front=temp; rear=temp; } else { rear->link=temp; rear=temp; } }

Data Structure

2012-15

voiddeq() { int item; if(front==NULL) printf("Queue is empty\n"); else { item=front->data; printf("The element deleted = %d\n",item); } if(front==rear) { front=NULL; rear=NULL; }

Enrollment no.- 07213704412

MCA semester II
else front=front->link; }

Data Structure

2012-15

void display() { n *ptr; if(front==NULL) printf("Queue is empty\n"); else { ptr=front; printf("The elements of the queue are :"); while(ptr!=NULL) { printf("%d\t",ptr->data); ptr=ptr->link; } } }

OUTPUT

Enrollment no.- 07213704412

You might also like