├── .gitignore ├── README.md ├── adt ├── bintree.c ├── circularq.c ├── hash_table.c ├── linked_list.c ├── postfix_prefix.c ├── queue.h ├── stack.c ├── stack.h ├── stack_user.c └── user_circularq.c ├── arrays_intro.c ├── arrays_pointers.c ├── bubble_sort.c ├── insertion_sort.c ├── merge_sort.c ├── multidim_array.c ├── pointer_arithmetic.c ├── pointers_intro.c ├── quick_sort.c ├── searching_array.c └── selection_sort.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # csl102_ds_iiitn 2 | This repository has all the source code required for Data Structures course CSL 102 @ IIIT Nagpur 3 | -------------------------------------------------------------------------------- /adt/bintree.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define EMPTYNODE 0 5 | #define SPACE_GAP_LEVEL 10 6 | #define FALSE 0 7 | #define TRUE 1 8 | 9 | struct node { 10 | short data; 11 | struct node* left; 12 | struct node* right; 13 | }; 14 | 15 | typedef struct node nodeQ_t; 16 | 17 | short trackerArr[2048] = {0}; 18 | short trackerIdx = 0; 19 | 20 | nodeQ_t* createNode(short data) 21 | { 22 | nodeQ_t* node = (nodeQ_t*)malloc(sizeof(nodeQ_t)); 23 | node->data = data; 24 | 25 | node->left = EMPTYNODE; 26 | node->right = EMPTYNODE; 27 | return node; 28 | } 29 | 30 | void printTree(nodeQ_t* root, short space) 31 | { 32 | if (root == NULL) 33 | return; 34 | 35 | space += SPACE_GAP_LEVEL; 36 | 37 | printTree(root->right, space); 38 | 39 | printf("\n"); 40 | for (int i = SPACE_GAP_LEVEL; i < space; i++) 41 | printf(" "); 42 | printf("%d\n", root->data); 43 | 44 | printTree(root->left, space); 45 | } 46 | 47 | short isPresentOnPath(nodeQ_t* node, short val) 48 | { 49 | if (!node) // EMPTYNODE == node 50 | return FALSE; 51 | 52 | trackerArr[trackerIdx] = node->data; 53 | trackerIdx++; // push() 54 | 55 | if (node->data == val) 56 | return TRUE; 57 | 58 | if (isPresentOnPath(node->left, val) || 59 | isPresentOnPath(node->right, val)) 60 | return TRUE; 61 | 62 | trackerIdx--; // pop() 63 | return FALSE; 64 | } 65 | 66 | void printPath(nodeQ_t* root, short val) 67 | { 68 | if (isPresentOnPath(root, val)) 69 | { 70 | for (int i=0; i < trackerIdx; i++){ 71 | printf("%d -> ", trackerArr[i]); 72 | } 73 | printf("\n"); 74 | } 75 | 76 | else 77 | printf("Value not present on path\n"); 78 | } 79 | 80 | 81 | void printInorder(nodeQ_t* node){ 82 | if(!node) 83 | return; 84 | 85 | printInorder(node->left); // left 86 | printf("%d -> ", node->data); // root 87 | printInorder(node->right); // right 88 | 89 | } 90 | 91 | void printPreorder(nodeQ_t* node){ 92 | if(!node) 93 | return; 94 | 95 | printf("%d -> ", node->data); // root 96 | printPreorder(node->left); // left 97 | printPreorder(node->right); // right 98 | 99 | } 100 | 101 | void printPostorder(nodeQ_t* node){ 102 | if(!node) 103 | return; 104 | 105 | printPostorder(node->left); // left 106 | printPostorder(node->right); // right 107 | printf("%d -> ", node->data); // root 108 | 109 | } 110 | 111 | nodeQ_t* getParentNodeBST(nodeQ_t* node, short val){ 112 | 113 | while(EMPTYNODE != node){ 114 | if ((val > node->data) && (node->right != EMPTYNODE)) 115 | node = node->right; 116 | else if ((val > node->data) && (node->right == EMPTYNODE)) 117 | return node; 118 | else if ((val < node->data) && (node->left != EMPTYNODE)) 119 | node = node->left; 120 | else if ((val < node->data) && (node->left == EMPTYNODE)) 121 | return node; 122 | else 123 | return EMPTYNODE; 124 | } 125 | 126 | return EMPTYNODE; 127 | } 128 | 129 | 130 | void insertNodeInBST(nodeQ_t* root, nodeQ_t* newNode){ 131 | 132 | if(EMPTYNODE == root || EMPTYNODE == newNode) 133 | return; 134 | 135 | nodeQ_t* parent = getParentNodeBST(root, newNode->data); 136 | if(EMPTYNODE == parent) 137 | return; 138 | 139 | if(newNode->data > parent->data) 140 | parent->right = newNode; 141 | if(newNode->data < parent->data) 142 | parent->left = newNode; 143 | } 144 | 145 | 146 | nodeQ_t* minNode(nodeQ_t* node){ 147 | if(EMPTYNODE == node) 148 | return EMPTYNODE; 149 | 150 | nodeQ_t* temp = node; 151 | while(EMPTYNODE != temp->left){ 152 | temp = temp->left; 153 | } 154 | 155 | return temp; 156 | } 157 | 158 | 159 | 160 | nodeQ_t* deleteNode(nodeQ_t* root, int key) 161 | { 162 | if (root == EMPTYNODE) 163 | return root; 164 | 165 | if (key < root->data) 166 | root->left = deleteNode(root->left, key); 167 | 168 | else if (key > root->data) 169 | root->right = deleteNode(root->right, key); 170 | 171 | else { 172 | // key == root->data is TRUE 173 | // node with only one child or no child 174 | if (root->left == EMPTYNODE) { 175 | nodeQ_t* temp = root->right; 176 | free(root); 177 | return temp; 178 | } 179 | else if (root->right == EMPTYNODE) { 180 | nodeQ_t* temp = root->left; 181 | free(root); 182 | return temp; 183 | } 184 | 185 | // both children for the node are present 186 | nodeQ_t* mNode = minNode(root->right); 187 | 188 | if(EMPTYNODE != mNode) 189 | { 190 | root->data = mNode->data; 191 | root->right = deleteNode(root->right, mNode->data); 192 | } 193 | } 194 | return root; 195 | } 196 | 197 | int main() 198 | { 199 | /*create root*/ 200 | /*struct node* root = createNode(11); 201 | 202 | root->left = createNode(22); 203 | root->right = createNode(33); 204 | 205 | root->left->left = createNode(144); 206 | root->left->right = createNode(55); 207 | 208 | root->right->left = createNode(66); 209 | root->right->right = createNode(222); 210 | 211 | root->right->left->left = createNode(88); 212 | root->right->right->right = createNode(99); 213 | 214 | root->right->left->left = createNode(111); 215 | root->right->right->right = createNode(77); 216 | 217 | root->left->left->left = createNode(-45); 218 | root->left->right->right = createNode(-9);*/ 219 | 220 | /*printf("%d\n", root->data); 221 | printf("%d\n", root->left->data); 222 | printf("%d\n", root->right->data); 223 | printf("%d\n", root->left->left->data); 224 | printf("%d\n", root->left->right->data); 225 | printf("%d\n", root->right->left->data); 226 | printf("%d\n", root->right->right->data);*/ 227 | 228 | //printTree(root, 0); 229 | 230 | /*trackerIdx = 0; 231 | printPath(root, 55); 232 | trackerIdx = 0; 233 | printPath(root, 56); 234 | trackerIdx = 0; 235 | printPath(root, 77); 236 | trackerIdx = 0; 237 | printPath(root, 222);*/ 238 | 239 | /* 240 | printf("Inorder: "); 241 | printInorder(root); 242 | printf("\n"); 243 | printf("Preorder: "); 244 | printPreorder(root); 245 | printf("\n"); 246 | printf("Postorder: "); 247 | printPostorder(root); 248 | printf("\n"); 249 | */ 250 | 251 | nodeQ_t* root = createNode(100); 252 | insertNodeInBST(root, createNode(100)); 253 | insertNodeInBST(root, createNode(104)); 254 | insertNodeInBST(root, createNode(56)); 255 | insertNodeInBST(root, createNode(23)); 256 | insertNodeInBST(root, createNode(23)); 257 | insertNodeInBST(root, createNode(200)); 258 | insertNodeInBST(root, createNode(292)); 259 | insertNodeInBST(root, createNode(912)); 260 | insertNodeInBST(root, createNode(5)); 261 | insertNodeInBST(root, createNode(17)); 262 | insertNodeInBST(root, createNode(211)); 263 | insertNodeInBST(root, createNode(211)); 264 | insertNodeInBST(root, createNode(101)); 265 | insertNodeInBST(root, createNode(-95)); 266 | 267 | printTree(root, 0); 268 | 269 | nodeQ_t *mNode = minNode(root); 270 | printf("min node = %d\n", mNode->data); 271 | 272 | root = deleteNode(root, 1000); 273 | printTree(root, 0); 274 | 275 | 276 | /*nodeQ_t* rootDegn = createNode(100); 277 | insertNodeInBST(rootDegn, createNode(101)); 278 | insertNodeInBST(rootDegn, createNode(102)); 279 | insertNodeInBST(rootDegn, createNode(105)); 280 | insertNodeInBST(rootDegn, createNode(123)); 281 | insertNodeInBST(rootDegn, createNode(134)); 282 | insertNodeInBST(rootDegn, createNode(200)); 283 | 284 | printTree(rootDegn, 0); 285 | */ 286 | 287 | return 0; 288 | } 289 | -------------------------------------------------------------------------------- /adt/circularq.c: -------------------------------------------------------------------------------- 1 | #include "queue.h" 2 | #include 3 | #include 4 | 5 | void destroy(); 6 | 7 | int* elementsQueue = NULL; 8 | short frontIdx = -1; 9 | short backIdx = -1; 10 | 11 | void create(){ 12 | destroy(); 13 | elementsQueue = (int*)malloc(MAX_SIZE_QUEUE * sizeof(int)); 14 | frontIdx = -1; 15 | backIdx = -1; 16 | return; 17 | } 18 | 19 | void destroy(){ 20 | if(NULL != elementsQueue){ 21 | free(elementsQueue); 22 | elementsQueue = NULL; 23 | } 24 | return; 25 | } 26 | 27 | status enqueue(int val){ 28 | 29 | if(QUEUE_FALSE == isFull()){ 30 | 31 | if (- 1 == frontIdx) 32 | frontIdx = 0; 33 | 34 | backIdx = (backIdx + 1) % MAX_SIZE_QUEUE; 35 | elementsQueue[backIdx] = val; 36 | 37 | return QUEUE_TRUE; 38 | } 39 | return QUEUE_FALSE; 40 | } 41 | 42 | status dequeue(int* ptrVal){ 43 | 44 | if(QUEUE_FALSE == isEmpty()){ 45 | 46 | *ptrVal = elementsQueue[frontIdx]; 47 | if (frontIdx == backIdx) { 48 | frontIdx = -1; 49 | backIdx = -1; 50 | } 51 | else { 52 | frontIdx = (frontIdx + 1) % MAX_SIZE_QUEUE; 53 | } 54 | return QUEUE_TRUE; 55 | } 56 | return QUEUE_FALSE; 57 | } 58 | 59 | status front(int* ptrVal){ 60 | if(QUEUE_FALSE == isEmpty()){ 61 | *ptrVal = elementsQueue[frontIdx % MAX_SIZE_QUEUE]; 62 | return QUEUE_TRUE; 63 | } 64 | return QUEUE_FALSE; 65 | } 66 | 67 | status back(int* ptrVal){ 68 | if(QUEUE_FALSE == isEmpty()){ 69 | *ptrVal = elementsQueue[backIdx % MAX_SIZE_QUEUE]; 70 | return QUEUE_TRUE; 71 | } 72 | return QUEUE_FALSE; 73 | } 74 | 75 | unsigned long size(){ 76 | return (unsigned long)(abs(backIdx - frontIdx) + 1); 77 | } 78 | 79 | status isEmpty(){ 80 | //printf("frontIdx = %d\tbackIdx = %d\n", frontIdx, backIdx); 81 | status s; 82 | s = (-1 == frontIdx) ? QUEUE_TRUE : QUEUE_FALSE; 83 | return s; 84 | } 85 | 86 | status isFull(){ 87 | status s; 88 | s = ((backIdx + 1 == frontIdx) || (0 == frontIdx && MAX_SIZE_QUEUE - 1 == backIdx)) ? QUEUE_TRUE : QUEUE_FALSE; 89 | return s; 90 | } 91 | 92 | void print(){ 93 | for(int i=0; i 2 | #include 3 | #include 4 | 5 | #define SIZE 255 6 | 7 | struct keyval_item { 8 | int data; 9 | int key; 10 | struct keyval_item* next; 11 | }; 12 | 13 | struct keyval_item* dataBucket[SIZE] = {NULL}; 14 | 15 | int hashFunc(int key) { 16 | return key % SIZE; 17 | } 18 | 19 | struct keyval_item* search(int key) { 20 | //get the hash 21 | int hashIndex = hashFunc(key); 22 | struct keyval_item* head_item = dataBucket[hashIndex]; 23 | 24 | while(NULL != head_item){ 25 | if(head_item->key == key){ 26 | return head_item; 27 | } 28 | head_item = head_item->next; 29 | } 30 | 31 | return NULL; 32 | } 33 | 34 | void insert(int key,int data) { 35 | struct keyval_item *item = (struct keyval_item*) malloc(sizeof(struct keyval_item)); 36 | item->data = data; 37 | item->key = key; 38 | 39 | //get the hash 40 | int hashIndex = hashFunc(key); 41 | if(NULL != dataBucket[hashIndex]){ 42 | // insert before the current and create a new head 43 | item->next = dataBucket[hashIndex]; 44 | } 45 | dataBucket[hashIndex] = item; 46 | } 47 | 48 | void delete(struct keyval_item* item) { 49 | int key = item->key; 50 | 51 | //get the hash 52 | int hashIndex = hashFunc(key); 53 | free(dataBucket[hashIndex]); 54 | dataBucket[hashIndex] = NULL; 55 | } 56 | 57 | void display() { 58 | int i = 0; 59 | 60 | for(i = 0; ikey,dataBucket[i]->data, i); 63 | } 64 | 65 | printf("\n"); 66 | } 67 | 68 | int main() { 69 | 70 | 71 | insert(1, 20); 72 | insert(2, 70); 73 | insert(42, 80); 74 | insert(4, 25); 75 | insert(12, 44); 76 | insert(14, 32); 77 | insert(17, 11); 78 | insert(13, 78); 79 | insert(37, 97); 80 | 81 | display(); 82 | struct keyval_item* item = search(37); 83 | 84 | if(item != NULL) { 85 | printf("Element found: %d\n", item->data); 86 | } else { 87 | printf("Element not found\n"); 88 | } 89 | 90 | delete(item); 91 | item = search(37); 92 | 93 | if(item != NULL) { 94 | printf("Element found: %d\n", item->data); 95 | } else { 96 | printf("Element not found\n"); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /adt/linked_list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define EMPTYNODE 0 5 | 6 | struct nodeQ{ 7 | short data; 8 | struct nodeQ* next; 9 | }; 10 | 11 | typedef struct nodeQ nodeQ_t; 12 | 13 | typedef enum{ 14 | LIST_FALSE = 0, 15 | LIST_TRUE = 1, 16 | } status_t; 17 | 18 | nodeQ_t* createNode(short val){ 19 | nodeQ_t* ptr = (nodeQ_t*)malloc(sizeof(nodeQ_t)); 20 | ptr->data = val; 21 | ptr->next = EMPTYNODE; // NULL 22 | return ptr; 23 | } 24 | 25 | status_t appendNode(nodeQ_t* tail, nodeQ_t* newNode){ 26 | 27 | if(EMPTYNODE == tail || EMPTYNODE == newNode) return LIST_FALSE; 28 | 29 | tail->next = newNode; 30 | return LIST_TRUE; 31 | } 32 | 33 | status_t prependNode(nodeQ_t* head, nodeQ_t* newNode){ 34 | 35 | if(EMPTYNODE == head || EMPTYNODE == newNode) return LIST_FALSE; 36 | 37 | newNode->next = head; 38 | return LIST_TRUE; 39 | } 40 | 41 | void printList(nodeQ_t* head){ 42 | if(EMPTYNODE == head) return; 43 | 44 | nodeQ_t* node = head; 45 | while(node){ 46 | printf("%d --> ", node->data); 47 | node = node->next; 48 | } 49 | printf("EMPTYNODE\n"); 50 | } 51 | 52 | void destroyList(nodeQ_t* ptr){ 53 | if(EMPTYNODE == ptr) return; 54 | 55 | while(ptr){ 56 | nodeQ_t* current = ptr; 57 | printList(current); 58 | ptr = ptr->next; 59 | free(current); 60 | } 61 | 62 | printf("EMPTYNODE\n"); 63 | } 64 | 65 | status_t isDataPresent(nodeQ_t* ptr, short data){ 66 | if(EMPTYNODE == ptr) return LIST_FALSE; 67 | 68 | while(ptr){ 69 | if(data == ptr->data) return LIST_TRUE; 70 | ptr = ptr->next; 71 | } 72 | 73 | return LIST_FALSE; 74 | } 75 | 76 | status_t insertNode(nodeQ_t* head, nodeQ_t* newNode, unsigned short index){ 77 | if(EMPTYNODE == head || EMPTYNODE == newNode) return LIST_FALSE; 78 | 79 | if(0 == index) return prependNode(head, newNode); 80 | 81 | nodeQ_t* curr = head; 82 | 83 | unsigned short counter = 0; 84 | while(curr){ 85 | counter++; 86 | if(index == counter){ 87 | // 56 88 | // 14 -> 16 -> 17 89 | newNode->next = curr->next; 90 | // 56 ->16 91 | curr->next = newNode; 92 | // 14->56->16>17 93 | return LIST_TRUE; 94 | } 95 | curr = curr->next; 96 | } 97 | 98 | return LIST_FALSE; 99 | } 100 | 101 | status_t removeNode(nodeQ_t* head, nodeQ_t** newHead, unsigned short index){ 102 | if(EMPTYNODE == head) return LIST_FALSE; 103 | 104 | if(0 == index){ 105 | *newHead = head->next; 106 | free(head); 107 | return LIST_TRUE; 108 | } 109 | 110 | nodeQ_t* prev = head; 111 | nodeQ_t* curr = head->next; 112 | 113 | // 15 -> EMPTYNODE 114 | unsigned short counter = 0; 115 | while(curr){ 116 | counter++; 117 | if(index == counter){ 118 | 119 | // 15 = prev 120 | // 16 = curr 121 | // 17 = curr->next 122 | prev->next = curr->next; 123 | free(curr); 124 | return LIST_TRUE; 125 | } 126 | 127 | // 15->16->17->18 128 | // 16 = prev 129 | // curr = 17 130 | prev = curr; 131 | curr = curr->next; 132 | } 133 | return LIST_FALSE; 134 | } 135 | 136 | nodeQ_t* reverseList(nodeQ_t* head){ 137 | if(EMPTYNODE == head) return EMPTYNODE; 138 | 139 | nodeQ_t* prev = head; 140 | nodeQ_t* tmp = EMPTYNODE; 141 | nodeQ_t* curr = head->next; 142 | head->next = EMPTYNODE; 143 | 144 | while(curr){ 145 | // 15 -> 16 -> 17 -> 18 -> 19 146 | // store curr->next into a tmp variable 147 | tmp = curr->next; 148 | // tmp = 17 149 | 150 | // update next for the curr node 151 | curr->next = prev; 152 | // curr = 16 153 | // curr->next = 15 154 | // 15 <- 16 <- 17 ??? 18 -> 19 155 | 156 | prev = curr; 157 | // prev = 16 158 | curr = tmp; 159 | // curr = 17 160 | } 161 | 162 | return prev; 163 | } 164 | 165 | 166 | int main(){ 167 | 168 | nodeQ_t* head = EMPTYNODE; 169 | nodeQ_t* tail = EMPTYNODE; 170 | 171 | head = createNode(20); 172 | printf("%p\n", head); 173 | 174 | tail = head; 175 | 176 | for(short i=1; i < 6; i++){ 177 | nodeQ_t* newNode = createNode(20+i); 178 | if(appendNode(tail, newNode)) 179 | tail = newNode; 180 | } 181 | 182 | for(short i=1; i < 6; i++){ 183 | nodeQ_t* newNode = createNode(20-i); 184 | if(prependNode(head, newNode)) 185 | head = newNode; 186 | } 187 | 188 | #if 0 189 | printf("%d\t\t%p\t\t%p\n", head->data, head, head->next); 190 | printf("%d\t\t%p\t\t%p\n", head->next->data, head->next, head->next->next); 191 | printf("%d\t\t%p\t\t%p\n", head->next->next->data, head->next->next, head->next->next->next); 192 | printf("%d\t\t%p\t\t%p\n", tail->data, tail, tail->next); 193 | #endif 194 | 195 | printList(head); 196 | 197 | if(LIST_TRUE == insertNode(head, createNode(100), 5)){ 198 | printList(head); 199 | } 200 | 201 | if(LIST_TRUE == insertNode(head, createNode(101), 10)){ 202 | printList(head); 203 | } 204 | 205 | if(LIST_TRUE == insertNode(head, createNode(450),20)){ 206 | printList(head); 207 | } 208 | 209 | if(LIST_TRUE == removeNode(head, &head, 8)){ 210 | printList(head); 211 | } 212 | 213 | if(LIST_TRUE == removeNode(head, &head, 0)){ 214 | printList(head); 215 | } 216 | 217 | if(LIST_TRUE == removeNode(head, &head, 800)){ 218 | printList(head); 219 | } 220 | 221 | head = reverseList(head); 222 | printList(head); 223 | 224 | //printf("dataPresent = %d \n", isDataPresent(head, 225)); 225 | 226 | //destroyList(head); 227 | //head = EMPTYNODE; 228 | 229 | //printf("%d\n", head->data); 230 | 231 | return 0; 232 | } 233 | -------------------------------------------------------------------------------- /adt/postfix_prefix.c: -------------------------------------------------------------------------------- 1 | #include "stack.h" 2 | #include 3 | #include 4 | 5 | int char2int(char c){ 6 | return c - '0'; 7 | } 8 | 9 | 10 | 11 | void evaluate(char ch, unsigned short which){ 12 | short stage = -1; 13 | 14 | if(isdigit(ch)){ 15 | if (STACK_FALSE == push(char2int(ch))) 16 | printf("Could not push %c\n", ch); 17 | 18 | //printf("Stage %d:\n", ++stage); 19 | //printStack(); 20 | } 21 | else{ 22 | int left; 23 | int right; 24 | if(0 == which){ 25 | pop(&right); 26 | pop(&left); 27 | } 28 | else{ 29 | pop(&left); 30 | pop(&right); 31 | } 32 | 33 | //printf("Stage %d:\n", ++stage); 34 | //printStack(); 35 | 36 | switch(ch){ 37 | case '+': 38 | push(left + right); 39 | break; 40 | case '-': 41 | push(left - right); 42 | break; 43 | case '*': 44 | push(left * right); 45 | break; 46 | case '/': 47 | push(left/right); 48 | break; 49 | } 50 | } 51 | } 52 | 53 | int evaluatePrefix(char expr[], short sz){ 54 | 55 | for(short i=sz-1; i > -1; i--) 56 | evaluate(expr[i], 1); 57 | 58 | int ret; 59 | 60 | //printf("Final stage:\n"); 61 | //printStack(); 62 | 63 | pop(&ret); 64 | return ret; 65 | 66 | } 67 | 68 | int evaluatePostfix(char expr[], short sz){ 69 | 70 | for(short i=0; i < sz; i++) 71 | evaluate(expr[i], 0); 72 | 73 | int ret; 74 | 75 | //printf("Final stage:\n"); 76 | //printStack(); 77 | 78 | pop(&ret); 79 | return ret; 80 | } 81 | 82 | int main(){ 83 | 84 | // Infix: 4 + 6 + 7 + 9 - 2 85 | // Postfix: 46+7+9+2- 86 | char expr0_postfix[] = {'4', '6', '+', '7', '+', '9', '+', '2', '-'}; 87 | // Prefix: +4 + 6 + 7 - 9 2 88 | char expr0_prefix[] = {'+', '4', '+', '6', '+', '7', '-', '9', '2'}; 89 | 90 | // Infix: 2 + 3 * 4 + 7 91 | // Postfix: 234*+7+ 92 | char expr1_postfix[] = {'2', '3', '4', '*', '+', '7', '+'}; 93 | // Prefix: +2+*347 94 | char expr1_prefix[] = {'+', '2', '+', '*', '3', '4', '7'}; 95 | 96 | // Infix: 9/3 + 8/2 97 | // Postfix: 93/82/+ 98 | char expr2_postfix[] = {'9', '3', '/', '8', '2', '/', '+'}; 99 | // Prefix: +/93/82 100 | char expr2_prefix[] = {'+', '/', '9', '3', '/', '8', '2'}; 101 | 102 | 103 | // Infix: (5 + 6) * 2 - 3 104 | // Postfix: 56+2*3- 105 | char expr3_postfix[] = {'5', '6', '+', '2', '*', '3', '-'}; 106 | // Prefix: -*+5623 107 | char expr3_prefix[] = {'-', '*', '+', '5', '6', '2', '3'}; 108 | 109 | // Infix: (2 - 4 + 5) * (6 / 3 + 1) 110 | // Postfix: 254+-631+/* 111 | char expr4_postfix[] = {'2', '4', '-' , '5', '+', '6', '3', '/' ,'1', '+', '*'}; 112 | // Prefix: *+-245+/631 113 | char expr4_prefix[] = {'*', '+', '-' , '2', '4', '5', '+', '/' ,'6', '3', '1'}; 114 | 115 | create(); 116 | printf("4 + 6 + 7 + 9 - 2 = %d\n", evaluatePostfix(expr0_postfix, sizeof(expr0_postfix)/sizeof(char))); 117 | 118 | create(); 119 | printf("4 + 6 + 7 + 9 - 2 = %d\n", evaluatePrefix(expr0_prefix, sizeof(expr0_prefix)/sizeof(char))); 120 | 121 | ////////// 122 | create(); 123 | printf(" 2 + 3 * 4 + 7 = %d\n", evaluatePostfix(expr1_postfix, sizeof(expr1_postfix)/sizeof(char))); 124 | 125 | create(); 126 | printf(" 2 + 3 * 4 + 7 = %d\n", evaluatePrefix(expr1_prefix, sizeof(expr1_prefix)/sizeof(char))); 127 | 128 | ////////// 129 | create(); 130 | printf("9/3 + 8/2 = %d\n", evaluatePostfix(expr2_postfix, sizeof(expr2_postfix)/sizeof(char))); 131 | 132 | create(); 133 | printf("9/3 + 8/2 = %d\n", evaluatePrefix(expr2_prefix, sizeof(expr2_prefix)/sizeof(char))); 134 | 135 | ////////// 136 | create(); 137 | printf("(5 + 6) * 2 - 3 = %d\n", evaluatePostfix(expr3_postfix, sizeof(expr3_postfix)/sizeof(char))); 138 | 139 | create(); 140 | printf("(5 + 6) * 2 - 3 = %d\n", evaluatePrefix(expr3_prefix, sizeof(expr3_prefix)/sizeof(char))); 141 | 142 | ////////// 143 | create(); 144 | printf("(2 - 4 + 5) * (6 / 3 + 1) = %d\n", evaluatePostfix(expr4_postfix, sizeof(expr4_postfix)/sizeof(char))); 145 | 146 | create(); 147 | printf("(2 - 4 + 5) * (6 / 3 + 1) = %d\n", evaluatePrefix(expr4_prefix, sizeof(expr4_prefix)/sizeof(char))); 148 | 149 | return 0; 150 | } 151 | -------------------------------------------------------------------------------- /adt/queue.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Purpose: 3 | * Author: 4 | * */ 5 | 6 | #define MAX_SIZE_QUEUE 5 7 | 8 | /** 9 | * 10 | * */ 11 | typedef enum{ 12 | QUEUE_TRUE = 0, 13 | QUEUE_FALSE = 1 14 | } status; 15 | 16 | /** 17 | * 18 | * */ 19 | void create(); 20 | 21 | /** 22 | * 23 | * */ 24 | status enqueue(int); 25 | 26 | /** 27 | * 28 | * */ 29 | status dequeue(int*); 30 | 31 | /** 32 | * 33 | * */ 34 | status front(int*); 35 | 36 | /** 37 | * 38 | * */ 39 | status back(int*); 40 | 41 | /** 42 | * 43 | * */ 44 | status isEmpty(); 45 | 46 | /** 47 | * 48 | * */ 49 | status isFull(); 50 | 51 | /** 52 | * 53 | * */ 54 | unsigned long size(); 55 | 56 | /** 57 | * 58 | * */ 59 | void print(); -------------------------------------------------------------------------------- /adt/stack.c: -------------------------------------------------------------------------------- 1 | #include "stack.h" 2 | #include 3 | #include 4 | 5 | // forward declaration 6 | unsigned long size(); 7 | status isEmpty(); 8 | status isFull(); 9 | void destroy(); 10 | 11 | 12 | int* elementsStack = NULL; 13 | short topOfStack = -1; 14 | 15 | void create(){ 16 | destroy(); 17 | elementsStack = (int*)malloc(MAX_SIZE_STACK * sizeof(int)); 18 | topOfStack = -1; 19 | return; 20 | } 21 | 22 | void destroy(){ 23 | if(NULL != elementsStack){ 24 | free(elementsStack); 25 | elementsStack = NULL; 26 | } 27 | return; 28 | } 29 | 30 | status push(int val){ 31 | if(STACK_FALSE == isFull()){ 32 | topOfStack++; 33 | elementsStack[topOfStack] = val; 34 | return STACK_TRUE; 35 | } 36 | return STACK_FALSE; 37 | } 38 | 39 | status pop(int* ptrVal){ 40 | if(STACK_FALSE == isEmpty()){ 41 | *ptrVal = elementsStack[topOfStack]; 42 | topOfStack--; 43 | return STACK_TRUE; 44 | } 45 | return STACK_FALSE; 46 | } 47 | 48 | status peek(int* ptrVal){ 49 | if(STACK_FALSE == isEmpty()){ 50 | *ptrVal = elementsStack[topOfStack]; 51 | //topOfStack--; 52 | return STACK_TRUE; 53 | } 54 | return STACK_FALSE; 55 | } 56 | 57 | unsigned long size(){ 58 | return topOfStack + 1; 59 | } 60 | 61 | status isEmpty(){ 62 | if(-1 == topOfStack) 63 | return STACK_TRUE; 64 | 65 | return STACK_FALSE; 66 | } 67 | 68 | status isFull(){ 69 | if(MAX_SIZE_STACK == topOfStack + 1) 70 | return STACK_TRUE; 71 | 72 | return STACK_FALSE; 73 | } 74 | 75 | void printStack(){ 76 | if(STACK_TRUE == isEmpty()) 77 | return; 78 | 79 | printf("++++++++++++\n"); 80 | for(short i=topOfStack; i > -1; i--){ 81 | printf("Element %d: %d\n", i, elementsStack[i]); 82 | } 83 | printf("-----------\n"); 84 | } 85 | -------------------------------------------------------------------------------- /adt/stack.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Purpose: 3 | * Author: 4 | * */ 5 | 6 | #define MAX_SIZE_STACK 50 7 | 8 | /** 9 | * 10 | * */ 11 | typedef enum{ 12 | STACK_TRUE = 0, 13 | STACK_FALSE = 1 14 | } status; 15 | 16 | /** 17 | * This function will create and initiliaze the stack ADT in memory 18 | * */ 19 | void create(); 20 | 21 | /** 22 | * 23 | * */ 24 | status push(int); 25 | 26 | /** 27 | * 28 | * */ 29 | status pop(int*); 30 | 31 | /** 32 | * 33 | * */ 34 | status peek(int*); 35 | 36 | /** 37 | * 38 | * */ 39 | void printStack(); 40 | 41 | -------------------------------------------------------------------------------- /adt/stack_user.c: -------------------------------------------------------------------------------- 1 | #include "stack.h" 2 | #include 3 | 4 | int main(){ 5 | 6 | create(); 7 | 8 | push(45); 9 | push(55); 10 | push(65); 11 | push(75); 12 | push(85); 13 | push(95); 14 | 15 | int val = 0; 16 | 17 | if(STACK_TRUE == pop(&val)){ 18 | printf("%d\n", val); 19 | } 20 | 21 | if(STACK_TRUE == pop(&val)){ 22 | printf("%d\n", val); 23 | } 24 | 25 | if(STACK_TRUE == peek(&val)){ 26 | printf("%d\n", val); 27 | } 28 | 29 | if(STACK_TRUE == peek(&val)){ 30 | printf("%d\n", val); 31 | } 32 | 33 | if(STACK_TRUE == pop(&val)){ 34 | printf("%d\n", val); 35 | } 36 | 37 | if(STACK_TRUE == peek(&val)){ 38 | printf("%d\n", val); 39 | } 40 | 41 | destroy(); 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /adt/user_circularq.c: -------------------------------------------------------------------------------- 1 | #include "queue.h" 2 | #include 3 | 4 | int main(){ 5 | 6 | create(); 7 | 8 | enqueue(30); 9 | enqueue(31); 10 | enqueue(32); 11 | enqueue(33); 12 | enqueue(34); 13 | enqueue(40); 14 | enqueue(41); 15 | enqueue(42); 16 | enqueue(43); 17 | enqueue(44); 18 | 19 | print(); 20 | 21 | 22 | int val; 23 | 24 | for(int i=0; i<5; i++){ 25 | if(QUEUE_TRUE == front(&val)) 26 | printf("front = %d\n", val); 27 | 28 | if(QUEUE_TRUE == back(&val)) 29 | printf("back = %d\n", val); 30 | 31 | if (QUEUE_TRUE == dequeue(&val)) 32 | printf("dequeue = %d\n\n", val); 33 | } 34 | 35 | //return 0; 36 | print(); 37 | 38 | printf("\n\n\n\n"); 39 | 40 | enqueue(50); 41 | enqueue(51); 42 | enqueue(52); 43 | enqueue(53); 44 | enqueue(54); 45 | 46 | print(); 47 | 48 | for(int i=0; i<5; i++){ 49 | if(QUEUE_TRUE == front(&val)) 50 | printf("front = %d\n", val); 51 | 52 | if(QUEUE_TRUE == back(&val)) 53 | printf("back = %d\n", val); 54 | 55 | if (QUEUE_TRUE == dequeue(&val)) 56 | printf("dequeue = %d\n\n", val); 57 | } 58 | 59 | print(); 60 | printf("\n\n\n\n"); 61 | 62 | enqueue(60); 63 | enqueue(61); 64 | enqueue(62); 65 | enqueue(63); 66 | enqueue(64); 67 | print(); 68 | 69 | for(int i=0; i<5; i++){ 70 | if(QUEUE_TRUE == front(&val)) 71 | printf("front = %d\n", val); 72 | 73 | if(QUEUE_TRUE == back(&val)) 74 | printf("back = %d\n", val); 75 | 76 | if (QUEUE_TRUE == dequeue(&val)) 77 | printf("dequeue = %d\n\n", val); 78 | } 79 | 80 | return 0; 81 | } -------------------------------------------------------------------------------- /arrays_intro.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | /*Two ways to initialize array*/ 5 | int intArr[10] = {0}; /*initialize all elements to ZERO*/ 6 | double doubleArr[10] = {0}; /*initialize all elements to ZERO*/ 7 | 8 | /*initialize array with multiple values*/ 9 | int intArrInitialized[10] = {1,2,3,4,5,6,7,8,9,10}; 10 | 11 | /*Rewrite values in an array using for loop*/ 12 | for(unsigned short idx = 0; idx < 10; idx++){ 13 | intArr[idx] = 5 * idx; 14 | doubleArr[idx] = -2.0 * idx; 15 | } 16 | 17 | /*print values in an array using for loop*/ 18 | for(unsigned short idx = 0; idx < 10; idx++){ 19 | printf("intArr[%d] = %d, doubleArr[%d] = %f, intArrInitialized[%d] = %d\n\n",\ 20 | idx, intArr[idx], idx, doubleArr[idx], idx, intArrInitialized[idx]); 21 | } 22 | 23 | /*print address of elements in array using for loop*/ 24 | for(unsigned short idx = 0; idx < 9; idx++){ 25 | printf("&intArr[%d] = %p, &doubleArr[%d] = %p,\n\ 26 | Bytes between indices for intArr = %u,\n\ 27 | Bytes between indices for doubleArr = %u\n\n",\ 28 | idx, &intArr[idx], idx, &doubleArr[idx],\ 29 | (unsigned int)&intArr[idx + 1] - (unsigned int)&intArr[idx],\ 30 | (unsigned int)&doubleArr[idx + 1] - (unsigned int)&doubleArr[idx]); 31 | } 32 | 33 | /*Strings in C are created as char array*/ 34 | /*There are two ways to initialze char arrays for processing as strings*/ 35 | char charArr[6] = {'H','e','l','l','o','\0'}; 36 | char string[] = "World"; /**/ 37 | 38 | printf("Message: %s, %s\n", charArr, string); 39 | } 40 | -------------------------------------------------------------------------------- /arrays_pointers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | /*Two ways to initialize array*/ 5 | int intArr[10] = {0}; /*initialize all elements to ZERO*/ 6 | double doubleArr[10] = {0}; /*initialize all elements to ZERO*/ 7 | 8 | /*Rewrite values in an array using for loop*/ 9 | for(unsigned short idx = 0; idx < 10; idx++){ 10 | intArr[idx] = 5 * idx; 11 | doubleArr[idx] = -2.0 * idx; 12 | } 13 | 14 | /*get the starting address of array*/ 15 | int *ptrArrInt = intArr; 16 | double *ptrArrDouble = doubleArr; 17 | 18 | /*print values in an array using for loop*/ 19 | for(unsigned short idx = 0; idx < 10; idx++){ 20 | printf("intArr[%d] = %d\n", idx, *(ptrArrInt + idx)); 21 | printf("doubleArr[%d] = %f\n", idx, *(ptrArrDouble + idx)); 22 | } 23 | 24 | /*print values in an array using for loop*/ 25 | for(unsigned short idx = 0; idx < 10; idx++){ 26 | printf("intArr[%d] = %d\n", idx, *ptrArrInt); 27 | printf("doubleArr[%d] = %f\n", idx, *ptrArrDouble); 28 | ++ptrArrInt; 29 | ++ptrArrDouble; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /bubble_sort.c: -------------------------------------------------------------------------------- 1 | // C program for implementation of Bubble sort 2 | #include 3 | 4 | void swap(int *a, int *b){ 5 | int temp = *a; 6 | *a = *b; 7 | *b = temp; 8 | } 9 | 10 | void bubbleSort(int arr[], int n){ 11 | for (int i = 0; i < n-1; i++){ 12 | for (int j = 0; j < n-i-1; j++){ 13 | if (arr[j] > arr[j+1]){ 14 | swap(&arr[j], &arr[j+1]); 15 | } 16 | } 17 | } 18 | } 19 | 20 | int main(){ 21 | int arr[] = {3,32,35,7,1,79,53,89,634,789,6}; 22 | int n = sizeof(arr)/sizeof(arr[0]); 23 | bubbleSort(arr, n); 24 | printf("Sorted array: \n"); 25 | for (int i=0; i < n; i++) 26 | printf("%d ", arr[i]); 27 | printf("\n"); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /insertion_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void insertionSort(int arr[], int n){ 5 | int key = 0; 6 | int j = 0; 7 | for (int i = 1; i < n; i++) { 8 | key = arr[i]; 9 | j = i - 1; 10 | 11 | while (j >= 0 && arr[j] > key) { 12 | arr[j + 1] = arr[j]; 13 | j = j - 1; 14 | } 15 | arr[j + 1] = key; 16 | } 17 | } 18 | 19 | int main(){ 20 | int arr[] = {3,32,35,7,1,79,53,89,634,789,6}; 21 | int n = sizeof(arr) / sizeof(arr[0]); 22 | 23 | insertionSort(arr, n); 24 | printf("Sorted array: \n"); 25 | for (int i=0; i < n; i++) 26 | printf("%d ", arr[i]); 27 | printf("\n"); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /merge_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /** 5 | Merges two subarrays of arr[]. 6 | First subarray is arr[left_index..middle_index] 7 | Second subarray is arr[middle_index+1..right_index] 8 | **/ 9 | void merge(int arr[], int left_index, int middle_index, int right_index) 10 | { 11 | int sz_left_arr = middle_index - left_index + 1; 12 | int sz_right_arr = right_index - middle_index; 13 | 14 | /* create temporary arrays */ 15 | int left_arr[sz_left_arr], right_arr[sz_right_arr]; 16 | 17 | /* Copy data to left_arr[] and right_arr[] */ 18 | for (int i = 0; i < sz_left_arr; i++) 19 | left_arr[i] = arr[left_index + i]; 20 | 21 | for (int j = 0; j < sz_right_arr; j++) 22 | right_arr[j] = arr[middle_index + 1 + j]; 23 | 24 | int i, j, k; 25 | /* Merge the temp arrays back into arr[l..r]*/ 26 | i = 0; /* starting index of left_arr */ 27 | j = 0; /* starting index of right_arr */ 28 | k = left_index; /* starting index of merged subarray */ 29 | while (i < sz_left_arr && j < sz_right_arr) { 30 | if (left_arr[i] <= right_arr[j]) { 31 | arr[k] = left_arr[i]; 32 | i++; 33 | } 34 | else { 35 | arr[k] = right_arr[j]; 36 | j++; 37 | } 38 | k++; 39 | } 40 | 41 | /* Copy the remaining elements of left_arr[], if there 42 | are any */ 43 | while (i < sz_left_arr) { 44 | arr[k] = left_arr[i]; 45 | i++; 46 | k++; 47 | } 48 | 49 | /* Copy the remaining elements of right_arr[], if there 50 | are any */ 51 | while (j < sz_right_arr) { 52 | arr[k] = right_arr[j]; 53 | j++; 54 | k++; 55 | } 56 | } 57 | 58 | void merge_sort_func(int arr[], int left_index, int right_index) 59 | { 60 | if (left_index < right_index) { 61 | int middle_index = (left_index + right_index) / 2; 62 | 63 | /*recursively sort first and second halves*/ 64 | merge_sort_func(arr, left_index, middle_index); 65 | merge_sort_func(arr, middle_index + 1, right_index); 66 | 67 | merge(arr, left_index, middle_index, right_index); 68 | } 69 | } 70 | 71 | void print_array_func(int arr[], int size){ 72 | for (int i = 0; i < size; i++) 73 | printf("%d ", arr[i]); 74 | printf("\n"); 75 | } 76 | 77 | int main() 78 | { 79 | int arr[5][8] = { {2, 31, 13, 15, 61, 7, 43, 44}, 80 | {102, 110, 3, -4, 6, 7, -23, 512}, 81 | {12, -11, 103, 54, 16, 37, 23, 55}, 82 | {1, 11, 13, -5, 6, -7, -23, 43}, 83 | {129, -11, -13, 5, -6, 7, 32, 66} }; 84 | 85 | for(int i=0; i<5; i++){ 86 | int arr_size = sizeof(arr[i]) / sizeof(arr[i][0]); 87 | printf("\nGiven array is \n"); 88 | print_array_func(arr[i], arr_size); 89 | 90 | merge_sort_func(arr[i], 0, arr_size - 1); 91 | 92 | printf("Sorted array is\n"); 93 | print_array_func(arr[i], arr_size); 94 | } 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /multidim_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #define ROWS 3 3 | #define COLUMNS 4 4 | 5 | int main(){ 6 | /* 7 | The general pattern for declaration of 2d array: 8 | data_type var_name[num_rows][num_columns] where 9 | - data_type is a valid data type in C language 10 | - var_name is the name of the variable 11 | */ 12 | int twoD_arr[ROWS][COLUMNS]; /* declaration without initialization*/ 13 | 14 | /*Initialization*/ 15 | /*First row: values 0 through 3*/ 16 | /*Second row: values 4 through 7*/ 17 | /*Third row: values 8 through 11*/ 18 | int twoD_arr2[ROWS][COLUMNS] = {0,1,2,3,4,5,6,7,8,9,10,11}; 19 | 20 | /*Clearer way to initialize*/ 21 | int twoD_arr3[ROWS][COLUMNS] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}}; 22 | 23 | /*Accessing the elements in two dimensional array*/ 24 | for (short i = 0; i < ROWS; i++){ 25 | for (short j = 0; j < COLUMNS; j++){ 26 | printf("twoD_arr3[%d][%d] = %d\n", i, j, twoD_arr3[i][j]); 27 | } 28 | } 29 | 30 | /*series of 3 two dimensional arrays*/ 31 | int threeD_arr3[3][ROWS][COLUMNS] = { 32 | {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}}, 33 | {{0,-1,-2,-3}, {-4,-5,-6,-7}, {-8,-9,-10,-11}}, 34 | {{2,4,6,8}, {-2,-4,-6,-8}, {12,13,14,15}} 35 | }; 36 | 37 | for (short i = 0; i < 3; i++){ 38 | for (short j = 0; j < ROWS; j++){ 39 | for (short k = 0; k < COLUMNS; k++){ 40 | printf("threeD_arr3[%d][%d][%d] = %d\n", i,j,k, threeD_arr3[i][j][k]); 41 | } 42 | } 43 | } 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /pointer_arithmetic.c: -------------------------------------------------------------------------------- 1 | #include 2 | /*Author: Aniket Pingley*/ 3 | 4 | int main(){ 5 | int a = 111; 6 | 7 | int* ptr = NULL; /* initialize a ptr to NULL */ 8 | ptr = &a; /* variable ptr now contains address of a */ 9 | printf("Value of ptr = %p.\n", ptr); 10 | ptr++; /*increment the value of pointer.*/ 11 | printf("Value of ptr after increment = %p.\n", ptr); 12 | ptr--; 13 | printf("Value of ptr after decrement = %p.\n", ptr); 14 | 15 | int *ptr2 = ptr; 16 | ptr2++; 17 | int diffVal = ptr2 - ptr; 18 | /*This operation above show the size of integer in bytes*/ 19 | /*However we are operating on two hexadecimal numbers. To get the correct value in 20 | bytes, first the hexadecimal value must be typecast/converted to integer format. */ 21 | diffVal = (unsigned long int)ptr2- (unsigned long int)ptr; 22 | printf("Size of integer in bytes = %d.\n", diffVal); 23 | 24 | int *ptr3 = ptr + 2; 25 | /*ptr + 2 will add twice the number of bytes as per 26 | the byte-size of int data type i.e. 4 bytes*/ 27 | diffVal = (unsigned long int)ptr3- (unsigned long int)ptr; 28 | printf("Size of integer in bytes = %d.\n", diffVal); 29 | 30 | /*Pointer can be used to manipulate the value of variable it points*/ 31 | ++*ptr; /*++ptr is different from ++*pt*/ 32 | printf("New value of 'a' pointed by 'ptr' = %d.\n", *ptr); 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /pointers_intro.c: -------------------------------------------------------------------------------- 1 | #include 2 | /*Author: Aniket Pingley*/ 3 | 4 | int main(){ 5 | int a = 111, b = 0; 6 | printf("Value of a = %d.\n Address of a = %p.\n", a, &a); 7 | 8 | int* ptr = NULL; /* initialize a ptr to NULL */ 9 | ptr = &a; /* variable ptr now contains address of a */ 10 | printf("Value of ptr = %p.\n Value at address pointed by p = %d.\n", ptr, *ptr); 11 | 12 | b = *ptr; /* the value of b is now overwritten with value of a*/ 13 | /* this is equivalent to writing b = a; */ 14 | printf("Value of b = %d.\n", b); 15 | 16 | /* Working with Pointer to Pointers*/ 17 | int **dblPtr = NULL; /* double pointer */ 18 | dblPtr = &ptr; /* dblPtr points to the address where ptr is located. 19 | '*dblPtr' will point to the address pointed by ptr, i.e. address of a */ 20 | if(ptr == *dblPtr){ 21 | printf("ptr == *dblPtr is true.\n"); 22 | printf("Address of 'dblPtr' = %p. \n", &dblPtr); 23 | printf("Address of 'ptr' and value in dblPtr = %p. \n", dblPtr); 24 | printf("Address of 'a' = %p. \n", *dblPtr); 25 | printf("Value at address pointed by 'ptr' = %d. \n", **dblPtr); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /quick_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void swap(int* a, int* b){ 5 | int temp = *a; 6 | *a = *b; 7 | *b = temp; 8 | } 9 | 10 | int partition (int arr[], int left_index, int right_index){ 11 | int pivot = arr[right_index]; 12 | int pivot_index = right_index; 13 | right_index--; /*pivot exists at the right index*/ 14 | 15 | while(left_index <= right_index){ 16 | if(arr[left_index] < pivot){ 17 | left_index++; 18 | continue; 19 | } 20 | else if(arr[right_index] > pivot){ 21 | right_index--; 22 | continue; 23 | } 24 | else{ 25 | swap(&arr[left_index], &arr[right_index]); 26 | } 27 | } 28 | 29 | /*Now the left index point to location where pivot must go*/ 30 | swap(&arr[left_index], &arr[pivot_index]); 31 | return left_index; 32 | } 33 | 34 | void quick_sort_func(int arr[], int left_index, int right_index) 35 | { 36 | if (left_index < right_index) { 37 | int partition_index = partition(arr, left_index, right_index); 38 | 39 | /*recursively sort first and second halves*/ 40 | quick_sort_func(arr, left_index, partition_index - 1); 41 | quick_sort_func(arr, partition_index + 1, right_index); 42 | } 43 | } 44 | 45 | void print_array_func(int arr[], int size){ 46 | for (int i = 0; i < size; i++) 47 | printf("%d ", arr[i]); 48 | printf("\n"); 49 | } 50 | 51 | int main() 52 | { 53 | int arr[6][8] = { {2, 31, 13, 15, 61, 7, 43, 44}, 54 | {102, 110, 3, -4, 6, 7, -23, 512}, 55 | {12, -11, 103, 54, 16, 37, 23, 55}, 56 | {1, 11, 13, -5, 6, -7, -23, 43}, 57 | {129, -11, -13, 5, -6, 7, 32, 66}, 58 | {-129, -11, -13, -5, -6, -7, -32, -66} }; 59 | 60 | for(int i=0; i<6; i++){ 61 | int arr_size = sizeof(arr[i]) / sizeof(arr[i][0]); 62 | printf("\nGiven array is \n"); 63 | print_array_func(arr[i], arr_size); 64 | 65 | quick_sort_func(arr[i], 0, arr_size - 1); 66 | 67 | printf("Sorted array is\n"); 68 | print_array_func(arr[i], arr_size); 69 | } 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /searching_array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int linearSearch(int arr[], int sz, int key){ 5 | int index = -1; 6 | for(int idx = 0; idx < sz; idx++){ 7 | if (key == arr[idx]){ 8 | index = idx; 9 | break; 10 | } 11 | } 12 | return index; 13 | } 14 | 15 | int binarySearch(int arr[], int sz, int key){ 16 | int index = -1; 17 | int left = 0, right = sz - 1, mid = 0; 18 | while(left <= right){ 19 | mid = (left + right)/2; 20 | if (key == arr[mid]){ 21 | index = mid; 22 | break; 23 | } 24 | if(arr[mid] < key){ 25 | left = mid+1; 26 | } 27 | else{ 28 | /*if(arr[mid] > key){}*/ 29 | right = mid-1; 30 | } 31 | } 32 | return index; 33 | } 34 | 35 | int getLargest(int arr[], int sz){ 36 | int retVal = INT_MIN; 37 | for(int idx = 0; idx < sz; idx++){ 38 | int val = arr[idx]; 39 | if (retVal < val){ 40 | retVal = val; 41 | } 42 | } 43 | return retVal; 44 | } 45 | 46 | int main(){ 47 | int arr[] = {3,32,35,7,1,79,53,89,634,789,6}; 48 | printf("linearSearch index = %d\n", linearSearch(arr, sizeof(arr)/sizeof(arr[0]), 35)); 49 | printf("linearSearch index = %d\n", linearSearch(arr, sizeof(arr)/sizeof(arr[0]), 350)); 50 | printf("largest value = %d\n", getLargest(arr, sizeof(arr)/sizeof(arr[0]))); 51 | int arrSorted[] = {2,3,4,5,6,7,8,9,12,34,56,78,99}; 52 | printf("binarySearch index = %d\n", binarySearch(arrSorted, sizeof(arrSorted)/sizeof(arrSorted[0]), 99)); 53 | printf("binarySearch index = %d\n", binarySearch(arrSorted, sizeof(arrSorted)/sizeof(arrSorted[0]), 234)); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /selection_sort.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int *a, int *b) 4 | { 5 | int temp = *a; 6 | *a = *b; 7 | *b = temp; 8 | } 9 | 10 | void selectionSort(int arr[], int n){ 11 | int minIdx = 0; 12 | for (int i = 0; i < n-1; i++){ 13 | minIdx = i; 14 | for (int j = i+1; j < n; j++){ 15 | if (arr[j] < arr[minIdx]){ 16 | minIdx = j; 17 | } 18 | } 19 | swap(&arr[minIdx], &arr[i]); 20 | } 21 | } 22 | 23 | int main() 24 | { 25 | int arr[] = {3,32,35,7,1,79,53,89,634,789,6}; 26 | int n = sizeof(arr)/sizeof(arr[0]); 27 | selectionSort(arr, n); 28 | printf("Sorted array: \n"); 29 | int i; 30 | for (i=0; i < n; i++) 31 | printf("%d ", arr[i]); 32 | printf("\n"); 33 | return 0; 34 | } 35 | --------------------------------------------------------------------------------