Tuesday, 16 June 2009

De_queue

Description:-
One of the famous program in the field of data structure is De_queue. In this program a simple de_queue is constructed using an array.
Normally in several de_queue programmes a lot of functions involved to manage the input & output operations but in my de_queue all the operations are performed with switch case command.
Another version of De_queue using single linked list will be introduced shortly.

Process:-
1. Copy the entire source code(Bold in format).
2. Paste that in your c or c++ compiler.
3. Save that with a suitable name.
4. Compile that program.
5. Finally run that program.

Error handling:-
Due to inappropiate html coding done by this web page I can't give the header file
name like stdio.h, conio.h, stdlib.h, iostream.h, fstream.h, iomanip.h etc.
If u get an error report after compiling the program
"CALL TO UNDEFINED FUNCTION 'PRINTF(OR ANYTHING)' IN FUNCTION MAIN()"
please add the above mentioned header file after #include
like #include.

Disclaimer:-
If you have any other problem regarding the program please mail me.
My mail address is showing on my blog page.



Source code:-
/*Complete program of de_queue with array*/
#include
#include
#include
#define MAX 5
int arr[MAX];
static int rear=-1;
void push();
void pop();
void display();
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1.Push into the queue.");
printf("\n2.Pop from queue.");
printf("\n3.Display the queue.");
printf("\n4.Exit.");
printf("\nEnter your choice=>");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nWrong choice!");
break;
}
}
}
void push()
{
int ch1,ch2,i,val;
if(rear!=MAX)
{
printf("\nEnter the value=>");
scanf("%d",&val);
printf("\n1.Input restricted.");
printf("\n2.Output restricted.");
printf("\nEnter your choice=>");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
arr[++rear]=val;
break;
case 2:
printf("\n1.Input into first.");
printf("\n2.Input into last.");
printf("\nEnter your choice=>");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
for(i=rear;i>=0;i--)
arr[i+1]=arr[i];
rear++;
arr[0]=val;
break;
case 2:
arr[++rear]=val;
break;
default:
printf("\nWrong choice!");
break;
}
break;
default:
printf("\nWrong choice!");
break;
}
}
else
printf("\nQueue overflow!");
}
void display()
{
int i;
if(rear==-1)
printf("\nNo element in the queue!");
else
{
printf("\nQueue element=>\n");
for(i=0;i<=rear;i++)
printf("%d ",arr[i]);
}
}
void pop()
{
int ch1,ch2,i;
if(rear!=-1)
{
printf("\n1.Input restricted.");
printf("\n2.Output restricted.");
printf("\nEnter your choice=>");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("\n1.Pop from first.");
printf("\n2.Pop from last.");
printf("\nEnter your choice=>");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
for(i=1;i<=rear;i++)
arr[i-1]=arr[i];
rear--;
return;
case 2:
rear--;
return;
default:
printf("\nWrong choice!");
break;
}
case 2:
rear--;
return;
default:
printf("\nWrong choice!");
break;
}
}
else
printf("\nNo element to pop!");
}

0 comments: