#include<stdio.h>
#include<conio.h>
#define MAX 10
void dequeaddatbeg(int *,int,int*,int *);
void dequeaddatend(int *,int,int *,int *);
int dequedelatbeg(int *,int *,int *);
int dequedelatend(int *,int *,int *);
void display(int *);
int count(int *);
void main()
{
int arr[MAX];
int front,rear,i,n;
clrscr();
/* Initiates data members */
front=rear=-1;
for(i=0;i<MAX;i++)
arr[i]=0;
dequeaddatend(arr,1,&front,&rear);
dequeaddatbeg(arr,2,&front,&rear);
dequeaddatend(arr,3,&front,&rear);
dequeaddatbeg(arr,4,&front,&rear);
dequeaddatend(arr,5,&front,&rear);
dequeaddatbeg(arr,6,&front,&rear);
dequeaddatend(arr,7,&front,&rear);
dequeaddatbeg(arr,8,&front,&rear);
dequeaddatend(arr,9,&front,&rear);
dequeaddatbeg(arr,10,&front,&rear);
dequeaddatend(arr,11,&front,&rear);
dequeaddatbeg(arr,12,&front,&rear);
// dequeaddatend(arr,13,&front,&rear);
// dequeaddatend(arr,14,&front,&rear);
print("\nElements in a deque: ");
display(arr);
n=count(arr);
printf("\nTotal number of elements in deque : %d",n);
i=dequedelatbeg(arr,&front,&rear);
printf("\n Item Extracted : %d",i);
i=dequedelatbeg(arr,&front,&rear);
printf("\n Item Extracted : %d",i);
i=dequedelatbeg(arr,&front,&rear);
printf("\n Item Extracted : %d",i);
i=dequedelatbeg(arr,&front,&rear);
printf("\n Item Extracted : %d",i);
printf("\nElements in a deque after deletion :");
display(arr);
dequeaddatend(arr,13,&front,&rear);
dequeaddatend(arr,14,&front,&rear);
printf(" Elemnts in the deque after deletion :");
display(arr);
i=dequedelatend(arr,&front,&rear);
printf("\n Item Extracted : %d",i);
i=dequedelatend(arr,&front,&rear);
printf("\n Item Extracted : %d",i);
printf(" \n Elemnts in a deque after deletion :");
display(arr);
n=count(arr);
printf("\n Total number of elements in deque : %d",n);
getch();
}
/* Adds an element at the beginning of a deque */
void dequeaddatbeg(int *arr,int item,int *pfront,int *prear)
{
int i,k,c;
if(*pfront==0 && *prear==MAX-1)
{
printf("\n Deque is FULL.\n");
return;
}
if(*pfront == -1)
{
*pfront=*prear=0;
arr[*pfront]=item;
return;
}
if(*prear != MAX-1)
{
c=count(arr);
k=(*prear)+1;
for(i=1;i<=c;i++)
{
arr[k]=arr[k-1];
k--;
}
arr[k]=item;
*pfront=k;
(*prear)++;
}
else
{
(*pfront)--;
arr[*pfront]=item;
}
}
/* Adds an element at the end of a deque */
void dequeaddatend(int *arr,int item,int *pfront,int *prear)
{
int i,k;
if(*pfront==0 && *prear==MAX-1)
{
printf("\nDeque is FULL.\n");
return;
}
if(*pfront==-1)
{
*prear=*pfront=0;
arr[*prear]=item;
return;
}
if(*prear==MAX-1)
{
k=*pfront-1;
for(i=*pfront-1;i<(*prear);i++)
{
k=i;
if(k == MAX-1)
arr[k]=0;
else
arr[k]=arr[i+1];
}
(*prear)--;
(*pfront)--;
}
(*prear)++;
arr[*prear]=item;
}
/* Removes an elemnt from *pfront end of deque */
int dequedelatbeg(int *arr,int *pfront,int *prear)
{
int item;
if(*pfront==-1)
{
printf("\n Deque is empty ");
return 0;
}
item=arr[*pfront];
arr[*pfront]=0;
if(*pfront==*prear)
*pfront=*prear=-1;
else
(*pfront)++;
return item;
}
/* Removes an element from the *prear end of the deque */
int dequedelatend(int *arr,int *pfront,int *prear)
{
int item;
if(*pfront==-1)
{
printf("\n Deque is empty .\n");
return 0;
}
item=arr[*prear];
arr[*prear]=0;
(*prear)--;
if(*prear==-1)
*pfront=-1;
return item;
}
/* Displays elements of a deque */
void display(int *arr)
{
int i;
printf("\n front ->");
for(i=0;i<MAX;i++)
printf("\t%d",arr[i]);
printf(" <-rear");
}
/* Counts the total number of elements in deque */
int count(int *arr)
{
int c=0,i;
for(i=0;i<MAX;i++)
{
if(arr[i]!=0)
c++;
}
return c;
}
No comments:
Post a Comment