Create a Queue and do the basic operations using Arrays MC 9217 DATA STRUCTURES LAB Anna University lab manual download - Computer Programming

Computer Programming

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Saturday, January 29, 2011

Create a Queue and do the basic  operations using Arrays MC 9217 DATA STRUCTURES LAB Anna University lab manual download

Aim: To write a program to create a Queue using one dimensional Array

Algorithm:

1. Start
2. Read ch and n value
3. check (ch!=4) and if (rear =n) then read value
4. Assign value to q(rear) and increment the rear, else Display as queue if full
5. if (front=rear) then print front value of queue and increment front
Else Display as queue is empty
1. if (rear=0) then assign front to I and write q[i].
Else Display as queue is empty
1. Go to step 3
2. stop

Logical Description
A collection of items in which only the earliest added item may be accessed. Basic operations are add (to the tail) or enqueue and delete (from the head) or dequeue. Delete returns the item removed. Also known as "first-in, first-out" or FIFO.
Formal Definition: It is convenient to define delete or dequeue in terms of remove and a new operation, front. The operations new(), add(v, Q), front(Q), and remove(Q) may be defined with axiomatic semantics as follows.
1. new() returns a queue
Program Code

#include<stdio.h>
#include<conio.h>
void main()
{
int q[50],i,j,n,op,front,rear,no;
clrscr();
printf("Enter the size of the queue:");
scanf("%d",&n);
printf("QUEUE OPERATIONS:\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter the operation to be performed:");
scanf("%d",&op);
front=0;rear=-1;
while(op!=4){
switch(op)
{
case 1:{
if(rear==n-1)
printf("\nQueue is full!\n");
else
{
printf("Enter the element to be inserted:");
scanf("%d",&no);
q[++rear]=no;
printf("\n%d is inserted!",no);
}
break;  }
case 2: {
if(front==0 && rear==-1)
printf("\nQueue is empty!");
else
{
printf("\n%d is deleted!",q[front]);
q[front]=0;
for(j=front;j<rear;j++)
q[j]=q[j+1];
rear=rear-1;
}
break;  }
case 3: {
printf("\nQueue is:\n");
for(i=front;i<=rear;i++)
printf("%d\t",q[i]);
break;  }
}
printf("\nEnter the operation to be performed:");
scanf("%d",&op);
}
getch(); }