Skip to content

Commit 840a13a

Browse files
authored
Create SimpleQueue.c
1 parent 1a79c1f commit 840a13a

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

src/Chapter_04_Queues/SimpleQueue.c

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
// Copyright (c) 2008 CareerMonk Publications and others.
2+
// #E-Mail : info@careermonk.com
3+
// Creation Date : 2008-01-10 06:15:46
4+
// Created by : Narasimha Karumanchi
5+
// Book Title : Data Structures And Algorithms Made Easy
6+
// Warranty : This software is provided "as is" without any
7+
// warranty; without even the implied warranty of
8+
// merchantability or fitness for a particular purpose.
9+
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <limits.h>
13+
14+
struct Queue {
15+
int front, rear;
16+
int capacity;
17+
int size;
18+
int *array;
19+
};
20+
21+
// Create an empty queue
22+
struct Queue *createQueue(int capacity) {
23+
struct Queue *Q = malloc(sizeof(struct Queue));
24+
if(!Q)
25+
return NULL;
26+
Q->capacity = capacity;
27+
Q->front = Q->rear = -1;
28+
Q->size = 0;
29+
Q->array= malloc(Q->capacity * sizeof(int));
30+
31+
if(!Q->array)
32+
return NULL;
33+
return Q;
34+
}
35+
36+
// Returns queue size
37+
int size(struct Queue *Q) {
38+
return Q->size;
39+
}
40+
41+
// Returns Frnt Element of the Queue
42+
int frontElement(struct Queue *Q) {
43+
return Q->array[Q->front];
44+
}
45+
46+
// Returns the Rear Element of the Queue
47+
int rearElement(struct Queue *Q) {
48+
return Q->array[Q->rear];
49+
}
50+
51+
// Check's if Queue is empty or not
52+
int isEmpty(struct Queue *Q) {
53+
// if the condition is true then 1 is returned else 0 is returned
54+
return (Q->size == 0);
55+
}
56+
57+
// Check's if Queue is full or not
58+
int isFull(struct Queue *Q) {
59+
// if the condition is true then 1 is returned else 0 is returned
60+
return (Q->size == Q->capacity);
61+
}
62+
63+
// Adding elements in Queue
64+
void enqueue(struct Queue *Q, int data) {
65+
if(isFull(Q))
66+
printf("Queue overflow\n");
67+
else {
68+
Q->rear = (Q->rear+1) % Q->capacity;
69+
Q->array[Q->rear]= data;
70+
if(Q->front == -1)
71+
Q->front = Q->rear;
72+
Q->size += 1;
73+
}
74+
}
75+
76+
// Removes an element from front of the queue
77+
int dequeue(struct Queue *Q) {
78+
int data = INT_MIN; //or element which does not exist in Queue
79+
if(isEmpty(Q)){
80+
printf("Queue is empty\n");
81+
return data;
82+
}
83+
data = Q->array[Q->front];
84+
if(Q->front == Q->rear) {
85+
Q->front = Q->rear = -1;
86+
Q->size = 0;
87+
} else {
88+
Q->front = (Q->front+1) % Q->capacity;
89+
Q->size -= 1;
90+
}
91+
return data;
92+
}
93+
94+
void deleteQueue(struct Queue *Q) {
95+
if(Q) {
96+
if(Q->array)
97+
free(Q->array);
98+
free(Q);
99+
}
100+
}
101+
102+
int main() {
103+
// Initializing Queue
104+
struct Queue *Q;
105+
Q = createQueue(4);
106+
// Adding elements in Queue
107+
enqueue(Q, 1);
108+
enqueue(Q, 3);
109+
enqueue(Q, 7);
110+
enqueue(Q, 5);
111+
enqueue(Q, 10);
112+
113+
// Printing size of Queue
114+
printf("\nSize of queue : %d\n", size(Q));
115+
116+
// Printing front and rear element of Queue */
117+
printf("Front element : %d\n", frontElement(Q));
118+
printf("Rear element : %d\n", rearElement(Q));
119+
120+
// Removing Element from Queue
121+
printf("\nDequeued element : %d\n", dequeue(Q));
122+
printf("Dequeued element : %d\n", dequeue(Q));
123+
printf("Dequeued element : %d\n", dequeue(Q));
124+
printf("Dequeued element : %d\n", dequeue(Q));
125+
printf("Dequeued element : %d\n", dequeue(Q));
126+
printf("Dequeued element : %d\n", dequeue(Q));
127+
enqueue(Q, 15);
128+
enqueue(Q, 100);
129+
130+
// Printing size of Queue
131+
printf("\nSize of queue : %d\n", size(Q));
132+
133+
// Printing front and rear element of Queue
134+
printf("Front element : %d\n", frontElement(Q));
135+
printf("Rear element : %d\n", rearElement(Q));
136+
137+
// Removing Queue
138+
deleteQueue(Q);
139+
return 0;
140+
}
141+

0 commit comments

Comments
 (0)