Tuesday, July 2, 2013

C PROGRAM FOR LINEAR QUEUE

/*PROGRAM TO TEST INSERTION & DELETION OPERATION IN LINEAR QUEUE*/
#include<stdio.h>
#include<conio.h>
void main() {
int max=5, n, r=0, f=0, a[5], v, item, i;
clrscr();
do {
printf("\nChoice are:");
printf("\n1: INSERTION");
printf("\n2: DELETION");
printf("\n3: DISPLAY ITEMS");
printf("\n4: EXIT");
printf("\nEnter your choice:");
scanf("%d", &n);
switch(n) {
case 1: {
 if(r==max)
 printf("\nQUEUE REACHED LIMIT\n");
 else {
 printf("\nEnter insertion value:");
 scanf("%d", &v);
 a[r]=v;
 r++; }
 break;
}
case 2: { if(f==r)
 printf("\nQUEUE IS EMPTY");
 else {
 item=a[f];
 f++;
 printf("\nDeleted item is: %d", item); }
 break;
}
case 3: { printf("\nThe items in the QUEUE are:");
 for(i=f;i<r;i++)
 printf(" %d ", a[i]);
 break; }
}
}
while(n!=4);
}
















/*OUTPUT:-

Choice are:
1: INSERTION
2: DELETION
3: DISPLAY ITEMS
4: EXIT
Enter your choice:1

Enter insertion value:1

Choice are:
1: INSERTION
2: DELETION
3: DISPLAY ITEMS
4: EXIT
Enter your choice:1

Enter insertion value:2

Choice are:
1: INSERTION
2: DELETION
3: DISPLAY ITEMS
4: EXIT
Enter your choice:3

The items in the QUEUE are: 1  2
Choice are:
1: INSERTION
2: DELETION
3: DISPLAY ITEMS
4: EXIT
Enter your choice:2

Deleted item is: 1
Choice are:
1: INSERTION
2: DELETION
3: DISPLAY ITEMS
4: EXIT
Enter your choice:3

The items in the QUEUE are: 2
Choice are:
1: INSERTION
2: DELETION
3: DISPLAY ITEMS
4: EXIT
Enter your choice:4   */

C PROGRAM FOR INSERTION, DELETION TESTING IN AN ARRAY USING FUNCTION

/*PROGRAM FOR INSERTION, DELETION TESTING IN AN ARRAY USING FUNCTION*/

#include<stdio.h>
#include<conio.h>
int b,c,d,n,i,j,v,p,m,x;
int a[100];
void insertion();
void deletion();
void main() {
clrscr();
printf("Enter array size:");
scanf("%d", &n);
printf("\nEnter values of array:\n");
for(b=0;b<n;b++) {
scanf("%d", &a[b]);
x=b+1; }
do {
printf("\nChoices are:\n");
printf("1: Insertion\n");
printf("2: Deletion\n");
printf("3: Display\n");
printf("4: Exit\n");
printf("\nEnter Your Choice:\n");
scanf("%d", &c);
switch(c) {
case 1: {
insertion();
break; }
case 2: {
deletion();
break; }
case 3: {
printf("\nThe values in the array are:");
for(d=0;d<x;d++)
printf(" %d ", a[d]);
break; }
}
}
while(c!=4);
getch();
}

void insertion() {
if(x!=n) {
printf("\nEnter insertion position 0 to n:(Enter 0 if array is empty and so on in serial number pattern):");
scanf("%d", &p);
printf("\nEnter insertion value:");
scanf("%d", &v);
for(i=x-1;i>=p;i--)
a[i+1]= a[i];
a[p]= v;
x=x+1; }
else
printf("Array is full"); }

void deletion() {
if(x!=0) {
printf("\nEnter deletion Position 0 to n:");
scanf("%d", &m);
for(j=m;j<n-1;j++)
a[j]= a[j+1];
x=x-1; }
else
printf("Array is empty"); }


/*OUTPUT:
       
Enter array size:3

Enter values of array:
1
2
3

Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
3

The values in the array are: 1  2  3
Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
2

Enter deletion Position 0 to n:1

Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
3

The values in the array are: 1  3
Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
1

Enter insertion position 0 to n:(Enter 0 if array is empty and so on in serial number pattern):1

Enter insertion value:9

Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
3

The values in the array are: 1  9  3
Choices are:
1: Insertion
2: Deletion
3: Display
4: Exit

Enter Your Choice:
4*/

C PROGRAM FOR CIRCULAR QUEUE

/*PROGRAM TO TEST INSERTION & DELETION OPERATION IN CIRCULAR QUEUE*/
#include<stdio.h>
#include<conio.h>
#define max 5
int front,rear,q[max];
void inqueue();
void delqueue();
void qdisplay();
int main() {
int c;
clrscr();
front=rear=-1;
do {
printf("\n1:INSERTION \n2:DELETION\n3:DISPLAY ");
printf("\n4:EXIT\nEnter Your Choice:");
scanf("%d",&c);
switch(c) {
case 1: {inqueue ();
break; }
case 2: {delqueue ();
break; }
case 3: {qdisplay ();
break; }
}
}
while(c!=4);
}

void inqueue() {
int x;
if ((front==0&&rear==max-1)|| (front==rear+1))
{ printf("\nQUEUE OVERFLOW\n");
return; }
if(front==-1)
{ front=rear=0; }
else
{ if(rear==max-1)
{ rear=0; }
else { rear++; } }
printf("\nEnter The Number:");
scanf("%d",&x);
q[rear]=x;
return; }

void delqueue() {
int y;
if(front==-1) {
printf("\nQUEUE IS UNDERFLOW \n");
return; }
y=q[front];
if(front==rear)
{ front=rear=-1; }
else
{ if(front==max-1)
{ front=0; }
else
{ front++; } }
printf("\n%d SUCESSFULLY DELETED \n",y);
return; }

void qdisplay() {
int i,j;
if(front==rear==-1)
{ printf("\nQUEUE IS EMPTY \n");
return; }
printf("\nITEMS ARE:");
for(i=front;i!=rear;i=(i +1)%max)
{ printf(" %d ",q[i]); }
printf(" %d ",q[rear]);
return; }





/************************************************************/
/*OUTPUT:-\

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:1

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:2

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:3

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:4

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:5

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

QUEUE OVERFLOW

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:3

ITEMS ARE: 1  2  3  4  5
1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:2

1 SUCESSFULLY DELETED

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:3

ITEMS ARE: 2  3  4  5
1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:1

Enter The Number:7

1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:3

ITEMS ARE: 2  3  4  5  7
1:INSERTION
2:DELETION
3:DISPLAY
4:EXIT
Enter Your Choice:4 */