13
13
struct ListNode {
14
14
int data ;
15
15
struct ListNode * next ;
16
- } * front , * rear ;
16
+ };
17
+
18
+ struct Queue {
19
+ struct ListNode * front ;
20
+ struct ListNode * rear ;
21
+ };
17
22
18
23
/* Create an empty queue */
19
- void initialize () {
20
- front = rear = NULL ;
24
+ struct Queue * CreateQueue () {
25
+ struct Queue * Q ;
26
+ struct ListNode * temp ;
27
+ Q = malloc (sizeof (struct Queue ));
28
+ if (!Q )
29
+ return NULL ;
30
+
31
+ temp = malloc (sizeof (struct ListNode ));
32
+ Q -> front = Q -> rear = NULL ;
33
+ return Q ;
21
34
}
22
35
23
36
/* Returns queue size */
24
- int size () {
25
- struct ListNode * temp = front ;
37
+ int size (struct Queue * Q ) {
38
+ struct ListNode * temp = Q -> front ;
26
39
int count = 0 ;
27
40
28
- if (front == NULL && rear == NULL )
41
+ if (Q -> front == NULL && Q -> rear == NULL )
29
42
return 0 ;
30
43
31
- while (temp != rear ){
44
+ while (temp != Q -> rear ){
32
45
count ++ ;
33
46
temp = temp -> next ;
34
47
}
35
- if (temp == rear )
48
+ if (temp == Q -> rear )
36
49
count ++ ;
37
50
38
51
return count ;
39
52
}
40
53
41
54
/* Returns Frnt Element of the Queue */
42
- int frontElement () {
43
- return front -> data ;
55
+ int frontElement (struct Queue * Q ) {
56
+ return Q -> front -> data ;
44
57
}
45
58
46
59
/* Returns the Rear Element of the Queue */
47
- int rearElement () {
48
- return rear -> data ;
60
+ int rearElement (struct Queue * Q ) {
61
+ return Q -> rear -> data ;
49
62
}
50
63
51
64
/*
52
65
Check's if Queue is empty or not
53
66
*/
54
- void isEmpty () {
55
- if (front == NULL && rear == NULL )
67
+ void isEmpty (struct Queue * Q ) {
68
+ if (Q -> front == NULL && Q -> rear == NULL )
56
69
printf ("Empty Queue\n" );
57
70
else
58
71
printf ("Queue is not Empty\n" );
59
72
}
60
73
/*
61
74
Adding elements in Queue
62
75
*/
63
- void enqueue (int num ) {
76
+ void enqueue (struct Queue * Q , int num ) {
64
77
struct ListNode * temp ;
65
78
temp = (struct ListNode * )malloc (sizeof (struct ListNode ));
66
79
temp -> data = num ;
67
80
temp -> next = NULL ;
68
81
69
- if (rear == NULL ) {
70
- front = rear = temp ;
82
+ if (Q -> rear == NULL ) {
83
+ Q -> front = Q -> rear = temp ;
71
84
} else {
72
- rear -> next = temp ;
73
- rear = temp ;
85
+ Q -> rear -> next = temp ;
86
+ Q -> rear = temp ;
74
87
}
75
88
}
76
89
77
90
/*
78
91
Removes an element from front of the queue
79
92
*/
80
- void dequeue () {
93
+ void dequeue (struct Queue * Q ) {
81
94
struct ListNode * temp ;
82
- if (front == NULL ) {
95
+ if (Q -> front == NULL ) {
83
96
printf ("\nQueue is Empty \n" );
84
97
return ;
85
98
} else {
86
- temp = front ;
87
- front = front -> next ;
88
- if (front == NULL ){
89
- rear = NULL ;
99
+ temp = Q -> front ;
100
+ Q -> front = Q -> front -> next ;
101
+ if (Q -> front == NULL ){
102
+ Q -> rear = NULL ;
90
103
}
91
104
printf ("Removed Element : %d\n" , temp -> data );
92
105
free (temp );
@@ -96,10 +109,10 @@ void dequeue() {
96
109
/*
97
110
Print's Queue
98
111
*/
99
- void printQueue () {
100
- struct ListNode * temp = front ;
112
+ void printQueue (struct Queue * Q ) {
113
+ struct ListNode * temp = Q -> front ;
101
114
102
- if ((front == NULL ) && (rear == NULL )) {
115
+ if ((Q -> front == NULL ) && (Q -> rear == NULL )) {
103
116
printf ("Queue is Empty\n" );
104
117
return ;
105
118
}
@@ -112,51 +125,52 @@ void printQueue() {
112
125
}
113
126
}
114
127
115
- void deleteQueue () {
128
+ void deleteQueue (struct Queue * Q ) {
116
129
struct ListNode * temp ;
117
- while (front ) {
118
- temp = front ;
130
+ while (Q -> front ) {
131
+ temp = Q -> front ;
119
132
printf ("Element being deleted: %d\n" , temp -> data );
120
- front = front -> next ;
133
+ Q -> front = Q -> front -> next ;
121
134
free (temp );
122
135
}
123
- free (front );
136
+ free (Q );
124
137
}
125
138
126
139
127
140
int main () {
128
141
/* Initializing Queue */
129
- initialize ();
142
+ struct Queue * Q ;
143
+ Q = CreateQueue ();
130
144
/* Adding elements in Queue */
131
- enqueue (1 );
132
- enqueue (3 );
133
- enqueue (7 );
134
- enqueue (5 );
135
- enqueue (10 );
145
+ enqueue (Q , 1 );
146
+ enqueue (Q , 3 );
147
+ enqueue (Q , 7 );
148
+ enqueue (Q , 5 );
149
+ enqueue (Q , 10 );
136
150
/* Printing Queue */
137
- printQueue ();
151
+ printQueue (Q );
138
152
/* Printing size of Queue */
139
- printf ("\nSize of Queue : %d\n" , size ());
153
+ printf ("\nSize of Queue : %d\n" , size (Q ));
140
154
/* Printing front and rear element of Queue */
141
- printf ("Front Element : %d\n" , frontElement ());
142
- printf ("Rear Element : %d\n" , rearElement ());
155
+ printf ("Front Element : %d\n" , frontElement (Q ));
156
+ printf ("Rear Element : %d\n" , rearElement (Q ));
143
157
/* Removing Element from Queue */
144
- dequeue ();
145
- dequeue ();
146
- dequeue ();
147
- dequeue ();
148
- dequeue ();
149
- dequeue ();
150
- enqueue (15 );
151
- enqueue (100 );
158
+ dequeue (Q );
159
+ dequeue (Q );
160
+ dequeue (Q );
161
+ dequeue (Q );
162
+ dequeue (Q );
163
+ dequeue (Q );
164
+ enqueue (Q , 15 );
165
+ enqueue (Q , 100 );
152
166
/* Printing Queue */
153
- printQueue ();
167
+ printQueue (Q );
154
168
/* Printing size of Queue */
155
- printf ("\nSize of Queue : %d\n" , size ());
169
+ printf ("\nSize of Queue : %d\n" , size (Q ));
156
170
/* Printing front and rear element of Queue */
157
- printf ("Front Element : %d\n" , frontElement ());
158
- printf ("Rear Element : %d\n" , rearElement ());
171
+ printf ("Front Element : %d\n" , frontElement (Q ));
172
+ printf ("Rear Element : %d\n" , rearElement (Q ));
159
173
/* Removing Queue */
160
- deleteQueue ();
174
+ deleteQueue (Q );
161
175
return 0 ;
162
176
}
0 commit comments