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