-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQueue_with_Array_in_C
62 lines (60 loc) · 1.12 KB
/
Queue_with_Array_in_C
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
#include <iostream>
using namespace std ;
struct queue {
int front , rear ;
int arr[20];
};
// for checking empty
int isEmpty (queue * q) {
if( q->rear==q->front )
return 1;
else
return 0;
}
// for checking full
int isFull (queue * q) {
if (q->rear==19)
return 1;
else
return 0;
}
// for inserting
int enqueue ( queue * q, int data ) {
if (isFull(q))
return 1;
if (isEmpty(q))
q->front = 0;
q->arr[++q->rear] = data;
return 0;
}
// for dequeue
int dequeue ( queue * q, int *data ) {
if ( isEmpty(q) )
return 0;
*data = q->arr[q->rear --] ;
if ( q->rear == q->front ) // in case of only one data
q->front = q->rear = -1 ;
return 0;
}
// for traversing
void traverse ( queue * q) {
queue temp = q->front ;
int i=1;
while ( temp ) {
cout<<i<<" "<< q->arr[q->front] <<endl;
temp = f->front ;
q->front ++ ;
i ++ ;
}
}
// main function
int main () {
queue * q ;
for ( int i=0, d=10; i<10 ; i++, d +=10 ) {
enqueue (q, d) ;
}
traverse (q);
dequeue (q, 10)
traverse (q);
return 0;
}