|
| 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 | +void resizeQueue(struct Queue *Q) { |
| 64 | + int size = Q->capacity; |
| 65 | + Q->capacity = Q->capacity*2; |
| 66 | + Q->array = realloc (Q->array, sizeof(int) * Q->capacity); |
| 67 | + if(!Q->array) { |
| 68 | + printf("Memory Error"); |
| 69 | + return; |
| 70 | + } |
| 71 | + if(Q->front > Q->rear ) { |
| 72 | + for(int i=0; i < Q->front; i++) { |
| 73 | + Q->array[i+size] =Q->array[i]; |
| 74 | + } |
| 75 | + Q->rear = Q->rear + size; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +// Adding elements in Queue |
| 81 | +void enqueue(struct Queue *Q, int data) { |
| 82 | + if(isFull(Q)) |
| 83 | + resizeQueue(Q); |
| 84 | + Q->rear = (Q->rear+1) % Q->capacity; |
| 85 | + Q->array[Q->rear]= data; |
| 86 | + if(Q->front == -1) |
| 87 | + Q->front = Q->rear; |
| 88 | + Q->size += 1; |
| 89 | +} |
| 90 | + |
| 91 | +// Removes an element from front of the queue |
| 92 | +int dequeue(struct Queue *Q) { |
| 93 | + int data = INT_MIN; //or element which does not exist in Queue |
| 94 | + if(isEmpty(Q)){ |
| 95 | + printf("Queue is empty\n"); |
| 96 | + return data; |
| 97 | + } |
| 98 | + data = Q->array[Q->front]; |
| 99 | + if(Q->front == Q->rear) { |
| 100 | + Q->front = Q->rear = -1; |
| 101 | + Q->size = 0; |
| 102 | + } else { |
| 103 | + Q->front = (Q->front+1) % Q->capacity; |
| 104 | + Q->size -= 1; |
| 105 | + } |
| 106 | + return data; |
| 107 | +} |
| 108 | + |
| 109 | +void deleteQueue(struct Queue *Q) { |
| 110 | + if(Q) { |
| 111 | + if(Q->array) |
| 112 | + free(Q->array); |
| 113 | + free(Q); |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +int main() { |
| 118 | + // Initializing Queue |
| 119 | + struct Queue *Q; |
| 120 | + Q = createQueue(3); |
| 121 | + |
| 122 | + // Adding elements in Queue |
| 123 | + enqueue(Q, 1); |
| 124 | + enqueue(Q, 3); |
| 125 | + enqueue(Q, 7); |
| 126 | + enqueue(Q, 5); |
| 127 | + enqueue(Q, 10); |
| 128 | + enqueue(Q, 19); |
| 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 Element from Queue |
| 138 | + printf("\nDequeued element : %d\n", dequeue(Q)); |
| 139 | + printf("Dequeued element : %d\n", dequeue(Q)); |
| 140 | + printf("Dequeued element : %d\n", dequeue(Q)); |
| 141 | + printf("Dequeued element : %d\n", dequeue(Q)); |
| 142 | + printf("Dequeued element : %d\n", dequeue(Q)); |
| 143 | + printf("Dequeued element : %d\n", dequeue(Q)); |
| 144 | + enqueue(Q, 15); |
| 145 | + enqueue(Q, 100); |
| 146 | + |
| 147 | + // Printing size of Queue |
| 148 | + printf("\nSize of queue : %d\n", size(Q)); |
| 149 | + |
| 150 | + // Printing front and rear element of Queue |
| 151 | + printf("Front element : %d\n", frontElement(Q)); |
| 152 | + printf("Rear element : %d\n", rearElement(Q)); |
| 153 | + |
| 154 | + // Removing Queue |
| 155 | + deleteQueue(Q); |
| 156 | + return 0; |
| 157 | +} |
0 commit comments