repl.it
@pankajyd/

Implementing queues

C

No description

fork
loading
Files
  • main.c
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

typedef struct Queue{
	int n;
	int* Q;
	int head;
	int tail;
}Queue;

int IsEmpty(Queue* Q){
	if(Q->head == Q->tail)
		return 1;
	return 0;
}

int IsFull(Queue* Q){
	int i = Q->tail;

	if(Q->tail == Q->n - 1)
		i = 0;
	else
		i++;
	
	if(i == Q->head)
		return 1;
	return 0;
}

void Enqueue(Queue* Q, int x){
	if(!IsFull(Q)){
		Q->Q[Q->tail] = x;
	}
	else{
		return;
	}
	if(Q->tail == Q->n - 1){
		Q->tail = 0;
	}
	else{
		Q->tail++;
	}
}

int Dequeue(Queue* Q){
	int x;
	if(!IsEmpty(Q))
		x = Q->Q[Q->head];
	if(Q->head == Q->n - 1)
		Q->head = 0;
	else
		Q->head++;
	return x;
}

void Print_Queue(Queue* Q){
	int i = Q->head;
	while(i != Q->tail){
		printf("%d ", Q->Q[i]);
		i = (i + 1)%Q->n;
	}
}

int main(){
	int N;
	//scan N;
	N = 5;
	int q[N];
	Queue QQ;
	Queue* Q = &QQ;
	Q->n = N;
	Q->Q = q;
	Q->head = 0;
	Q->tail = 0;

	//Do Enqueue
	//Do Dequeue
	//Print Queue
	return 0;
}