|
| 1 | +#include<stdio.h> |
| 2 | +#include<conio.h> |
| 3 | +#include<stdbool.h> |
| 4 | +#include<stdlib.h> |
| 5 | + |
| 6 | +//All global variables with global scope |
| 7 | + |
| 8 | +int *arr; // Array of size n to store actual content to be stored in stacks |
| 9 | +int *top; // Array of size k to store indexes of top elements of stacks |
| 10 | +int *next; // Array of size n to store next entry in all stacks |
| 11 | + // and free list |
| 12 | +int n, k; |
| 13 | +int fre; // To store beginning index of free list |
| 14 | + |
| 15 | + |
| 16 | +// A utility function to check if there is space available |
| 17 | +bool isFull() |
| 18 | +{ |
| 19 | + return (fre == -1); |
| 20 | +} |
| 21 | + |
| 22 | +// To push an item in stack number 'sn' where sn is from 0 to k-1 |
| 23 | +void push(int item, int sn); |
| 24 | + |
| 25 | +// To pop an from stack number 'sn' where sn is from 0 to k-1 |
| 26 | +int pop(int sn); |
| 27 | + |
| 28 | +// To check whether stack number 'sn' is empty or not |
| 29 | +bool isEmpty(int sn) { return (top[sn] == -1); } |
| 30 | + |
| 31 | + |
| 32 | +//function to create K stacks with an array of size n |
| 33 | +void CreatekStacks(int k1, int n1); |
| 34 | + |
| 35 | +//function to traverse the stack |
| 36 | +void traverseStack(int sn); |
| 37 | + |
| 38 | + |
| 39 | +int main() |
| 40 | +{ |
| 41 | + |
| 42 | + // Let us create 3 stacks in an array of size 10 |
| 43 | + int k = 3, n = 10; |
| 44 | + |
| 45 | + //creating 3 stacks each with size 10 |
| 46 | + CreatekStacks(k, n); |
| 47 | + |
| 48 | + // Let us put some items in stack number 0 |
| 49 | + push(10, 0); |
| 50 | + push(90, 0); |
| 51 | + |
| 52 | + |
| 53 | + // Let us put some items in stack number 1 |
| 54 | + push(15, 1); |
| 55 | + push(23, 1); |
| 56 | + push(44, 1); |
| 57 | + |
| 58 | + |
| 59 | + // Let us put some items in stack number 2 |
| 60 | + push(55, 2); |
| 61 | + push(5, 2); |
| 62 | + |
| 63 | + printf("Popped element from stack 0 is %d\n",pop(0)); |
| 64 | + printf("Popped element from stack 1 is %d\n",pop(1)); |
| 65 | + printf("Popped element from stack 2 is %d\n",pop(2)); |
| 66 | + |
| 67 | + // traverseStack(2); |
| 68 | + |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +void CreatekStacks(int k1, int n1) |
| 75 | +{ |
| 76 | + // Initialize n and k, and allocate memory for all arrays |
| 77 | + k = k1, n = n1; |
| 78 | + |
| 79 | + //creating and allocating memory for the 3 arrays dynamically |
| 80 | + arr = (int*) malloc( n * sizeof(int)); |
| 81 | + top = (int*) malloc(k * sizeof(int)); |
| 82 | + next = (int*) malloc( n * sizeof(int)); |
| 83 | + |
| 84 | + // Initialize all stacks as empty |
| 85 | + for (int i = 0; i < k; i++) |
| 86 | + top[i] = -1; |
| 87 | + |
| 88 | + // Initialize all spaces as free |
| 89 | + fre = 0; |
| 90 | + for (int i=0; i<n-1; i++) |
| 91 | + next[i] = i+1; |
| 92 | + next[n-1] = -1; // -1 is used to indicate end of free list |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +//function to push an item on top of stack |
| 97 | +void push(int item, int sn) |
| 98 | +{ |
| 99 | + // Overflow check |
| 100 | + if (isFull()) |
| 101 | + { |
| 102 | + printf("\nThe Stack is full!\n"); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + int i = fre; // Store index of first free slot |
| 107 | + |
| 108 | + // Update index of free slot to index of next slot in free list |
| 109 | + fre = next[i]; |
| 110 | + |
| 111 | + // Update next of top and then top for stack number 'sn' |
| 112 | + next[i] = top[sn]; |
| 113 | + top[sn] = i; |
| 114 | + |
| 115 | + // Put the item in array |
| 116 | + arr[i] = item; |
| 117 | +} |
| 118 | + |
| 119 | + |
| 120 | +//function to pop an item from stack |
| 121 | +int pop(int sn) |
| 122 | +{ |
| 123 | + // Underflow check |
| 124 | + if (isEmpty(sn)) |
| 125 | + { |
| 126 | + printf("\nThe stack is empty!\n") ; |
| 127 | + return INT_MAX; |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + // Find index of top item in stack number 'sn' |
| 132 | + int i = top[sn]; |
| 133 | + |
| 134 | + top[sn] = next[i]; // Change top to store next of previous top |
| 135 | + |
| 136 | + // Attach the previous top to the beginning of free list |
| 137 | + next[i] = fre; |
| 138 | + fre = i; |
| 139 | + |
| 140 | + // Return the previous top item |
| 141 | + return arr[i]; |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | +void traverseStack(int sn) |
| 146 | +{ |
| 147 | + if (isEmpty(sn)) |
| 148 | + { |
| 149 | + printf("\nStack is Empty!!\n"); |
| 150 | + } |
| 151 | + |
| 152 | + // Finding the size of stack number 'sn' |
| 153 | + //printf("%d\n",size); |
| 154 | + int i = 0 ; |
| 155 | + while(i <= top[sn]) |
| 156 | + { |
| 157 | + printf("%d\n",arr[i]); |
| 158 | + i++; |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | +} |
0 commit comments