|
| 1 | +/*Copyright (c) 2016 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 Stack { |
| 15 | + int top; |
| 16 | + int capacity; |
| 17 | + int *array; |
| 18 | +}; |
| 19 | + |
| 20 | +struct Stack *createStack(int capacity) { |
| 21 | + struct Stack *S = malloc(sizeof(struct Stack)); |
| 22 | + if(!S) |
| 23 | + return NULL; |
| 24 | + S->capacity = capacity; |
| 25 | + S->top = -1; |
| 26 | + S->array= malloc(S->capacity * sizeof(int)); |
| 27 | + if(!S->array) |
| 28 | + return NULL; |
| 29 | + return S; |
| 30 | +} |
| 31 | + |
| 32 | +int isEmpty(struct Stack *S) { |
| 33 | + return (S->top == -1); // if the condition is true then 1 is returned else 0 is returned |
| 34 | +} |
| 35 | + |
| 36 | +int size(struct Stack *S) { |
| 37 | + return (S->top + 1); |
| 38 | +} |
| 39 | + |
| 40 | +int isFull(struct Stack *S){ |
| 41 | + //if the condition is true then 1 is returned else 0 is returned |
| 42 | + return (S->top == S->capacity - 1); |
| 43 | +} |
| 44 | + |
| 45 | +void doubleStack(struct Stack *S){ |
| 46 | + S->capacity *= 2; |
| 47 | + S->array = realloc(S->array, S->capacity * sizeof(int)); |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +void push(struct Stack *S, int data){ |
| 52 | + if(isFull(S)) |
| 53 | + doubleStack(S); |
| 54 | + S->array[++S->top] = data; |
| 55 | +} |
| 56 | + |
| 57 | +int pop(struct Stack *S){ |
| 58 | + /* S->top == - 1 indicates empty stack*/ |
| 59 | + if(isEmpty(S)){ |
| 60 | + printf("Stack is Empty\n"); |
| 61 | + return INT_MIN; |
| 62 | + } |
| 63 | + else /* Removing element from ‘top’ of the array and reducing ‘top’ by 1*/ |
| 64 | + return (S->array[S->top--]); |
| 65 | +} |
| 66 | + |
| 67 | +int peek(struct Stack *S){ |
| 68 | + /* S->top == - 1 indicates empty stack*/ |
| 69 | + if(isEmpty(S)){ |
| 70 | + printf("Stack is Empty"); |
| 71 | + return INT_MIN;; |
| 72 | + } |
| 73 | + else |
| 74 | + return (S->array[S->top]); |
| 75 | +} |
| 76 | + |
| 77 | +void deleteStack(struct Stack *S){ |
| 78 | + if(S) { |
| 79 | + if(S->array) |
| 80 | + free(S->array); |
| 81 | + free(S); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +struct Queue { |
| 86 | + struct Stack *S1; // for enQueue |
| 87 | + struct Stack *S2; // for deQueue |
| 88 | +}; |
| 89 | + |
| 90 | +int queueSize(struct Queue *Q) { |
| 91 | + return size(Q->S1); |
| 92 | +} |
| 93 | + |
| 94 | +struct Queue *createQueue(int capacity) { |
| 95 | + struct Stack *S1 = createStack(capacity); |
| 96 | + struct Stack *S2 = createStack(capacity); |
| 97 | + struct Queue *Q = malloc(sizeof(struct Queue)); |
| 98 | + if(!Q) |
| 99 | + return NULL; |
| 100 | + Q->S1 = S1; |
| 101 | + Q->S2 = S2; |
| 102 | + return Q; |
| 103 | +} |
| 104 | + |
| 105 | +void enQueue(struct Queue *Q, int data) { |
| 106 | + push(Q->S1, data); |
| 107 | +} |
| 108 | + |
| 109 | +int deQueue(struct Queue *Q) { |
| 110 | + if(!isEmpty(Q->S2)) |
| 111 | + return pop(Q->S2); |
| 112 | + else { |
| 113 | + while( !isEmpty(Q->S1) ) |
| 114 | + push(Q->S2, pop(Q->S1)); |
| 115 | + return pop(Q->S2); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +void deleteQueue(struct Queue *Q){ |
| 120 | + if(Q) { |
| 121 | + if(Q->S1) |
| 122 | + free(Q->S1); |
| 123 | + if(Q->S2) |
| 124 | + free(Q->S2); |
| 125 | + free(Q); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +int main(){ |
| 130 | + // Initializing Queue |
| 131 | + struct Queue *Q; |
| 132 | + Q = createQueue(6); |
| 133 | + |
| 134 | + // Adding elements in Queue |
| 135 | + enQueue(Q, 1); |
| 136 | + enQueue(Q, 3); |
| 137 | + enQueue(Q, 7); |
| 138 | + enQueue(Q, 5); |
| 139 | + enQueue(Q, 10); |
| 140 | + enQueue(Q, 19); |
| 141 | + |
| 142 | + // Printing size of Queue |
| 143 | + printf("\nSize of queue : %d\n", queueSize(Q)); |
| 144 | + |
| 145 | + // Removing Element from Queue |
| 146 | + printf("\nDequeued element : %d\n", deQueue(Q)); |
| 147 | + printf("Dequeued element : %d\n", deQueue(Q)); |
| 148 | + printf("Dequeued element : %d\n", deQueue(Q)); |
| 149 | + printf("Dequeued element : %d\n", deQueue(Q)); |
| 150 | + printf("Dequeued element : %d\n", deQueue(Q)); |
| 151 | + printf("Dequeued element : %d\n", deQueue(Q)); |
| 152 | + |
| 153 | + // Removing Queue |
| 154 | + deleteQueue(Q); |
| 155 | + return 0; |
| 156 | +} |
0 commit comments