-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqueue_1.c
More file actions
76 lines (66 loc) · 1.52 KB
/
queue_1.c
File metadata and controls
76 lines (66 loc) · 1.52 KB
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
/*
* queue.c
*
* Created on: Jul 3, 2012
* Author: vikram
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define FAILURE -999
#define SUCCESS 0
struct queue_ds {
int piles[20];
int head;
int tail;
};
typedef struct queue_ds queue;
//Instead of efficiency focus on correctness
int is_queue_full(queue **q_ref) { //if you are updating pointer it is advisable use double pointer
//However,if you just updating values like index which are not
//type of pointer use single pointer
if((*q_ref)->head == 20) {
if ((*q_ref)->head == (*q_ref)->tail) {
//update head =>0 and tail =>0
(*q_ref)->head = 0;
(*q_ref)->tail = 0;
}
printf("queue is full \n");
return FAILURE;
}
return SUCCESS;
}
int is_queue_empty(queue *q_ptr) {
if(q_ptr->tail == q_ptr->head) {
printf("queue is empty \n");
return FAILURE; //stack is empty
}
return SUCCESS;
}
void enqueue(queue **q_ref,int payload){
//Insert element
if(is_queue_full(q_ref)== SUCCESS) {
(*q_ref)->piles[(*q_ref)->head] = payload;
(*q_ref)->head += 1;
printf("enqueued element = %d\n",payload);
}
}
int dequeue(queue** q_ref) {
//remove element
if(is_queue_empty(*q_ref) == SUCCESS) {
int payload = (*q_ref)->piles[(*q_ref)->tail];
(*q_ref)->tail += 1;
printf("dequeued element = %d\n",payload);
return (payload);
}
}
int main(int argc,char** argv){
queue *q_ptr = (queue *) malloc(sizeof(queue));
memset(q_ptr,0,sizeof(queue));
int i = 0;
for(i=0;i<10;i++) {
enqueue(&q_ptr,i);
}
dequeue(&q_ptr);
return(0);
}