-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathQueueDynamic.h
61 lines (53 loc) · 1.11 KB
/
QueueDynamic.h
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
#ifndef __QUEUE_DYNAMIC_H__
#define __QUEUE_DYNAMIC_H__
#include <cstdlib>
int choice;
struct Node {
int data;
struct Node* next;
};
struct Queue {
struct Node *head, *tail;
};
struct Node * NewNode() {
struct Node *temp = (struct Node*) malloc (sizeof(struct Node));
temp->next=NULL;
return temp;
}
struct Queue * NewQueue() {
struct Queue *Q = (struct Queue*) malloc (sizeof(struct Queue));
Q->head=NULL;
Q->tail=NULL;
return Q;
}
void EnQueue(struct Queue *Q, int data) {
struct Node * temp= NewNode();
temp->data=data;
if(Q->head==NULL && Q->tail==NULL) {
Q->head=temp;
Q->tail=temp;
return;
}
Q->tail->next=temp;
Q->tail=temp;
}
int DeQueue(struct Queue *Q) {
if(Q->head==NULL && Q->tail==NULL) {
return -1;
}
struct Node *temp = Q->head;
int data = temp->data;
Q->head=Q->head->next;
if(Q->head==NULL)
Q->tail=NULL;
free (temp);
return data;
}
void EmptyQueue(struct Queue *Q) {
while(Q->head!=NULL)
DeQueue(Q);
}
bool IsEmpty(struct Queue *Q){
return (Q->head==NULL) ? true : false ;
}
#endif //__QUEUE_DYNAMIC_H__