forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DEQU.CPP
200 lines (197 loc) · 3.66 KB
/
DEQU.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* Program of Dqueue*/
/* Data Structures Using C by UDIT AGARWAL */
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
#define MAX 10
void addqatbeg ( int *, int, int *, int * ) ;
void addqatend ( int *, int, int *, int * ) ;
int delqatbeg ( int *, int *, int * ) ;
int delqatend ( int *, int *, int * ) ;
void display ( int * ) ;
int count ( int * ) ;
void disp();
void main( )
{
int arr[MAX] ;
int front, rear, i, n ;
clrscr() ;
/* initialize data members */
front = rear = -1 ;
for ( i=0;i<MAX;i++ )
arr[i] = 0;
int ch, data;
disp();
while (1)
{
printf("\n Enter the Choice");
scanf("%d", &ch);
switch(ch)
{
case 0:
disp();
break;
case 1:
printf("\n Enter the Element");
scanf("%d", &data);
addqatbeg ( arr, data, &front, &rear ) ;
break;
case 2:
printf("\n Enter the element");
scanf("%d", &data);
addqatend ( arr, data, &front, &rear ) ;
break;
case 3:
i = delqatbeg ( arr, &front, &rear ) ;
printf ( "\nItem extracted: %d", i ) ;
break;
case 4:
i = delqatend ( arr, &front, &rear ) ;
printf ( "\nItem extracted: %d", i ) ;
break;
case 5:
n = count ( arr);
printf("Number of elements in dequeue is %d",n);
break;
case 6:
display(arr);
break;
case 7:
exit(0);
default:
printf("Please enter right choice....");
}
}
getch();
}
void disp( )
{
printf("\nRepresentation of Dequeue");
printf("\n\**************************\n");
printf("\n\t 1.Enqueue at front");
printf("\n\t 2.Enqueue at rear");
printf("\n\t 3.Dequeue at front");
printf("\n\t 4.Dequeue at rear");
printf("\n\t 5.Size of the queue");
printf("\n\t 6.view");
printf("\n\t 7.Exit");
}
/* adds an element at the beginning of a deque */
void addqatbeg ( int *arr, int item, int *pfront, int *prear )
{
int i, k, c ;
if ( *pfront == 0 && *prear == MAX - 1 )
{
printf ( "\nDeque 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 addqatend ( 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 element from the *pfront end of deque */
int delqatbeg ( int *arr, int *pfront, int *prear )
{
int item ;
if ( *pfront == -1 )
{
printf ( "\nDeque is empty.\n" ) ;
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 delqatend ( int *arr, int *pfront, int *prear )
{
int item ;
if ( *pfront == -1 )
{
printf ( "\nDeque 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 ;
}