├── AVL └── Code │ ├── avl_tree.c │ ├── avl_tree.h │ └── test.c ├── LICENSE ├── Lists ├── Doubly_Linked_List │ ├── Exers │ │ ├── doubly_linked_list.c │ │ ├── doubly_linked_list.h │ │ └── exers.c │ ├── doubly_linked_list.c │ └── doubly_linked_list.h ├── List │ ├── Exer │ │ ├── linked_list.c │ │ ├── linked_list.h │ │ ├── main.c │ │ ├── static_list.c │ │ └── static_list.h │ ├── linked_list.c │ ├── linked_list.h │ ├── static_list.c │ └── static_list.h └── Ordered_List │ ├── Exer │ ├── linked_ordered_list.c │ ├── linked_ordered_list.h │ ├── main.c │ ├── static_ordered_list.c │ └── static_ordered_list.h │ ├── linked_ordered_list.c │ ├── linked_ordered_list.h │ ├── static_ordered_list.c │ └── static_ordered_list.h ├── Pointers ├── Basics │ └── basics.c ├── Character_arrays_ptrs │ └── string_ptr.c ├── Dynamic_Memory_Allocation │ └── dyn_mem_alloc.c ├── Dynamic_Variables │ └── dyn_var.c ├── Function_Pointers │ ├── Func_Ptrs │ │ └── function_ptrs.c │ └── Func_ptrs_callbacks │ │ └── func_ptrs_callbacks.c ├── Memory_Leak │ └── main.c ├── Pointer_as_function_returns │ └── ptr_function_return.c ├── Pointer_to_Pointer │ └── ptr2ptr.c ├── Pointers_and_functions │ └── ptrs_and_functions.c ├── Ptrs_and_multidimensional_arrays │ └── ptrs_multi_arrays.c └── links_StackOverFlow.txt ├── Queues ├── Priority_Queue │ ├── Exer │ │ ├── dynamic_queue.c │ │ ├── dynamic_queue.h │ │ ├── exer.c │ │ ├── ordered_list.c │ │ ├── ordered_list.h │ │ ├── priority_queue.c │ │ └── priority_queue.h │ ├── dynamic_queue.c │ ├── dynamic_queue.h │ ├── ordered_list.c │ ├── ordered_list.h │ ├── priority_queue.c │ └── priority_queue.h ├── Queue │ ├── Exer │ │ ├── circular_queue.c │ │ ├── circular_queue.h │ │ ├── dynamic_queue.c │ │ ├── dynamic_queue.h │ │ └── test.c │ ├── circular_queue.c │ ├── circular_queue.h │ ├── dynamic_queue.c │ └── dynamic_queue.h ├── Queue_Circular_Linked_List │ ├── Exer │ │ ├── circular_list_queue.c │ │ ├── circular_list_queue.h │ │ └── exer.c │ ├── circular_list_queue.c │ └── circular_list_queue.h └── Queue_Linked_List │ ├── Implemenation_1 │ ├── linked_list.c │ ├── linked_list.h │ ├── list_queue.c │ └── queue.h │ └── Tutorial_Implementation │ ├── Exers │ ├── queue.c │ ├── queue.h │ └── test.c │ ├── queue.c │ └── queue.h ├── README.md ├── Search_Tree ├── binary_search_tree.c └── binary_search_tree.h ├── Stack ├── Stack_Exercices │ ├── Ex2 │ │ ├── dynamic_queue.c │ │ ├── dynamic_queue.h │ │ ├── dynamic_stack.c │ │ ├── dynamic_stack.h │ │ └── exer2.c │ ├── Exer1 │ │ ├── dynamic_stack.c │ │ ├── dynamic_stack.h │ │ └── ex1.c │ └── Handout_Exercises_with_stacks.pdf ├── dynamic_stack.c ├── dynamic_stack.h ├── static_stack.c └── static_stack.h └── Tree ├── extra_functions.c ├── tree_dynamic.c ├── tree_dynamic.h └── tree_traversal.c /AVL/Code/avl_tree.c: -------------------------------------------------------------------------------- 1 | #include "avl_tree.h" 2 | #include 3 | #include 4 | 5 | void CreateEmptyBST(tBST *tree) { *tree = NULLBST; } 6 | 7 | bool isEmptyTree(tBST tree){ //DONE 8 | return (tree == NULLBST); 9 | } 10 | 11 | tBST LeftChild(tBST tree) { return tree->left; } 12 | 13 | tBST RightChild(tBST tree) { return tree->right; } 14 | 15 | int max(int a, int b){ //Returns the maximum of two integers 16 | if (a>=b) { 17 | return a; 18 | }else { 19 | return b; 20 | } 21 | } 22 | 23 | int height(tBST tree){ //Returns tree height 24 | 25 | if (isEmptyTree(tree)) { 26 | return 0; 27 | }else { 28 | return tree->height; 29 | } 30 | } 31 | 32 | 33 | int getBalance(tBST tree){ 34 | 35 | if (isEmptyTree(tree)){ return 0;} 36 | return (height(tree->left)-height(tree->right)); 37 | 38 | } 39 | 40 | tBST findKey(tBST tree, tKey key){ 41 | 42 | if (isEmptyTree(tree)) 43 | return NULLBST; 44 | else if (key == tree->key) 45 | return tree; 46 | else if (key < tree->key) 47 | return findKey(tree->left, key); 48 | else // (key > tree->key) 49 | return findKey(tree->right, key); 50 | 51 | } 52 | 53 | //Rotations 54 | 55 | 56 | void rr_rotation(tBST *tree){ //left rotate 57 | if (isEmptyTree(*tree)) { return;} 58 | 59 | printf("rr_rotating\n"); 60 | 61 | 62 | tBST aux1 = (*tree)->right; 63 | 64 | (*tree)->right=aux1->left; 65 | aux1->left=*tree; 66 | 67 | //Height update 68 | (*tree)->height=max(height((*tree)->left),height((*tree)->right)) + 1; 69 | aux1->height=max(height(aux1->left),height(aux1->right)) + 1; 70 | 71 | *tree=aux1; 72 | } 73 | 74 | void ll_rotation(tBST *tree){ //right rotate 75 | if (isEmptyTree(*tree)) { return;} 76 | 77 | printf("ll_rotating\n"); 78 | 79 | 80 | tBST aux1 = (*tree)->left; 81 | 82 | (*tree)->left=aux1->right; 83 | aux1->right=*tree; 84 | 85 | //Height update 86 | (*tree)->height=max(height((*tree)->left),height((*tree)->right)) + 1; 87 | aux1->height=max(height(aux1->left),height(aux1->right)) + 1; 88 | 89 | *tree=aux1; 90 | } 91 | 92 | void rl_rotation(tBST *tree){ 93 | if (isEmptyTree(*tree)) { return;} 94 | 95 | ll_rotation(&(*tree)->right); 96 | rr_rotation(tree); 97 | } 98 | 99 | void lr_rotation(tBST *tree){ 100 | if (isEmptyTree(*tree)) { return;} 101 | 102 | 103 | rr_rotation(&(*tree)->left); 104 | ll_rotation(tree); 105 | 106 | } 107 | 108 | bool createBSTNode (tBSTPos* p,tKey key){ 109 | 110 | *p = malloc(sizeof(struct tBSTNode)); 111 | 112 | if(*p != NULLBST){ 113 | (*p)->key = key; 114 | (*p)->left = NULLBST; 115 | (*p)->right = NULLBST; 116 | (*p)->height=1; 117 | } 118 | return *p!=NULLBST; 119 | } 120 | 121 | 122 | bool insertKey(tBST* tree, tKey key) 123 | { 124 | bool check = false; 125 | bool check_createNode= false; 126 | 127 | if (isEmptyTree(*tree)){ 128 | check_createNode = createBSTNode(tree, key); 129 | printf("NK:%d H:%d B:0 \n",(*tree)->key,(*tree)->height); 130 | return check_createNode; 131 | } 132 | else if (key == (*tree)->key) 133 | return true; 134 | else if (key < (*tree)->key) 135 | check = insertKey(&(*tree)->left, key); 136 | else // (key > (*tree)->key) 137 | check = insertKey(&(*tree)->right, key); 138 | 139 | 140 | if(check){ 141 | 142 | 143 | (*tree)->height=max(height((*tree)->left),height((*tree)->right)) + 1; //Height Update 144 | 145 | //Check balance factor of initial node 146 | 147 | int balance = getBalance(*tree); 148 | 149 | printf("NK:%d H:%d B:%d \n",(*tree)->key,(*tree)->height,balance); 150 | 151 | 152 | if(balance>1){ //Left-? 153 | if(key<(*tree)->left->key){ //Left-Left 154 | ll_rotation(tree); 155 | }else{ //Left-Right 156 | lr_rotation(tree); 157 | } 158 | } 159 | 160 | if(balance<-1){ //Right-? 161 | if(key>(*tree)->right->key){ //Right-Right 162 | rr_rotation(tree); 163 | }else{ //Right-Left 164 | rl_rotation(tree); 165 | } 166 | } 167 | 168 | } 169 | 170 | return check; 171 | 172 | } 173 | 174 | 175 | void replace (tBST* subTree,tBST* auxTree){ //Replace the content of a node by its predecessors 176 | if (!isEmptyTree((*subTree)->right)) { 177 | replace(&(*subTree)->right,auxTree); //Going down the right branch 178 | }else { 179 | (*auxTree)->key = (*subTree)->key; //We replace the data fields of the node 180 | *auxTree = *subTree; //We mark the node on which we will call free() 181 | (*subTree) = (*subTree)->left; //We re-link the tree structure by "skyping" 182 | //the node to be deleted 183 | } 184 | } 185 | 186 | void removeKey(tBST* tree,tKey key){ 187 | 188 | tBST aux; 189 | if (key < (*tree)->key) { 190 | removeKey(&(*tree)->left, key); //If the key is smaller, continue through left subtree 191 | }else if (key > (*tree)->key) { 192 | removeKey(&(*tree)->right, key); //If the key is larger, continue through right subtree 193 | }else { // key == (*tree)->key //Once we locate the key, we proceed to its deletion 194 | printf("Removing: %d\n",key); 195 | aux = *tree; 196 | if (isEmptyTree((*tree)->right)) 197 | { //If it has no right child, replace by left one 198 | *tree = (*tree)->left; //this covers the no-child case 199 | } 200 | else if (isEmptyTree((*tree)->left)) 201 | { //If it has no left child, replace by right one. 202 | *tree = (*tree)->right; 203 | 204 | }else { 205 | //If it has two children we call replace() passing 206 | //its left subtree as first argument 207 | replace(&(*tree)->left,&aux); 208 | } 209 | free(aux); 210 | aux=NULLBST; 211 | 212 | } 213 | 214 | if(isEmptyTree(*tree)) {return;} 215 | 216 | //Update Balance Factor and Height 217 | (*tree)->height=max(height((*tree)->left),height((*tree)->right)) + 1; //Height Update 218 | 219 | int balance = getBalance(*tree); 220 | 221 | while((balance>1)||(balance<-1)){ 222 | 223 | if(balance>1){ //Left-? 224 | if(getBalance((*tree)->left)>=0){ //Left-Left 225 | ll_rotation(tree); 226 | }else{ //Left-Right 227 | lr_rotation(tree); 228 | } 229 | } 230 | 231 | if(balance<-1){ //Right-? 232 | if(getBalance((*tree)->right)>0){ //Right-Left 233 | rl_rotation(tree); 234 | }else{ //Right-Right 235 | rr_rotation(tree); 236 | } 237 | } 238 | balance = getBalance(*tree); 239 | 240 | } 241 | 242 | } -------------------------------------------------------------------------------- /AVL/Code/avl_tree.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define NULLBST NULL 6 | 7 | typedef int tKey; 8 | 9 | 10 | typedef struct tBSTNode *tBSTPos; 11 | 12 | struct tBSTNode { 13 | tKey key; 14 | int height; 15 | tBSTPos right; 16 | tBSTPos left; 17 | }; 18 | 19 | typedef tBSTPos tBST; 20 | 21 | void CreateEmptyBST(tBST *tree); 22 | bool isEmptyTree (tBST tree); 23 | bool createBSTNode (tBSTPos* p,tKey key); 24 | bool insertKey(tBST* tree, tKey key); 25 | tBST findKey(tBST tree, tKey key); 26 | void replace (tBST* subTree,tBST* auxTree); 27 | void removeKey(tBST* tree,tKey key); -------------------------------------------------------------------------------- /AVL/Code/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "avl_tree.h" 3 | 4 | 5 | 6 | void preorder_traversal(tBST tree){ 7 | if (tree!=NULLBST) { 8 | printf(" %d ",tree->key); 9 | preorder_traversal(tree->left); 10 | preorder_traversal(tree->right); 11 | } 12 | } 13 | 14 | 15 | int main(){ 16 | 17 | tBST tree; 18 | 19 | CreateEmptyBST(&tree); 20 | insertKey(&tree,65); 21 | insertKey(&tree,43); 22 | insertKey(&tree,70); 23 | insertKey(&tree,25); 24 | insertKey(&tree,58); 25 | insertKey(&tree,67); 26 | insertKey(&tree,75); 27 | insertKey(&tree,10); 28 | insertKey(&tree,47); 29 | insertKey(&tree,62); 30 | insertKey(&tree,66); 31 | insertKey(&tree,69); 32 | insertKey(&tree,73); 33 | insertKey(&tree,86); 34 | insertKey(&tree,60); 35 | insertKey(&tree,68); 36 | insertKey(&tree,93); 37 | 38 | /* 39 | insertKey(&tree,20); 40 | insertKey(&tree,30); 41 | insertKey(&tree,15); 42 | */ 43 | if(isEmptyTree(tree)){printf("VACIO");}; 44 | 45 | 46 | preorder_traversal(tree); 47 | 48 | printf("\n"); 49 | 50 | removeKey(&tree,25); 51 | removeKey(&tree,75); 52 | removeKey(&tree,66); 53 | removeKey(&tree,65); 54 | removeKey(&tree,62); 55 | removeKey(&tree,10); 56 | removeKey(&tree,43); 57 | removeKey(&tree,47); 58 | 59 | 60 | printf("\n"); 61 | 62 | // if(isEmptyTree(tree)){printf("VACIO\n");}; 63 | 64 | preorder_traversal(tree); 65 | 66 | printf("\n"); 67 | /* 68 | insertKey(&tree,2); 69 | printf("\n\n"); 70 | preorder_traversal(tree); 71 | */ 72 | 73 | 74 | 75 | return 0; 76 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 spaceduck 4 | https://github.com/martinge17 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /Lists/Doubly_Linked_List/Exers/doubly_linked_list.c: -------------------------------------------------------------------------------- 1 | #include "doubly_linked_list.h" 2 | #include 3 | 4 | bool isEmptyList(tList L) { return (L == NULL); } 5 | 6 | void createEmptyList(tList *L) { *L = LNULL; } 7 | 8 | bool createNode(tPosL *p) { 9 | *p = malloc(sizeof(struct tNode)); 10 | return *p != NULL; 11 | } 12 | 13 | bool insertItem(tItemL d, tPosL p, tList *L) { 14 | tPosL q, r; //q--> element we want to add 15 | //r-->element prior to q 16 | if (!createNode(&q)) { 17 | return false; 18 | } else { 19 | q->data = d; 20 | q->next = LNULL; 21 | q->prev = LNULL; 22 | if (isEmptyList(*L)) { 23 | *L = q; 24 | } else if (p == LNULL) { //If given position is NULL, add the element at the end of list 25 | for (r = *L; r->next != LNULL; r = r->next) 26 | ; //We advance to the end of the list 27 | r->next = q; 28 | q->prev = r; 29 | 30 | } else if (p == *L) { // insert at the top of the list (first element) 31 | q->next = p; 32 | p->prev = q; 33 | *L = q; 34 | 35 | } else { //insert in intermediate position 36 | 37 | q->data = p->data; // data exchange 38 | p->data = d; 39 | if (p->next != LNULL) { //in case of inserting in the last position we cannot access the "previous" field of NULL 40 | p->next->prev = q; 41 | } 42 | q->next = p->next; 43 | p->next = q; 44 | q->prev = p; 45 | } 46 | return true; 47 | } 48 | } 49 | 50 | void updateItem(tItemL d, tPosL p, tList *L) { 51 | p->data = d; 52 | } 53 | 54 | tPosL findItem(tItemL d, tList L) { 55 | tPosL p; 56 | 57 | for (p = L; (p != NULL) && (p->data != d); p = p->next) 58 | ; 59 | return p; 60 | } 61 | 62 | 63 | 64 | tItemL getItem(tPosL p, tList L) { return p->data; } 65 | 66 | 67 | 68 | tPosL first(tList L) { 69 | 70 | return L; // L always points to te first element of the list 71 | } 72 | 73 | tPosL last(tList L) { 74 | tPosL p; 75 | 76 | for (p = L; p->next != LNULL; p = p->next) 77 | ; 78 | return p; 79 | } 80 | 81 | tPosL previous(tPosL p, tList L) { 82 | return p->prev; 83 | } 84 | 85 | tPosL next(tPosL p, tList L) { return p->next; } 86 | 87 | void deleteAtPosition(tPosL p, tList *L) { 88 | tPosL q; 89 | 90 | if (p == *L) { // Delete first element 91 | *L = (*L)->next; 92 | 93 | if(!isEmptyList(*L)){ 94 | (*L)->prev = LNULL; 95 | } 96 | 97 | }else { // Middle and end deletion 98 | 99 | q = p->prev; 100 | q->next = p->next; 101 | 102 | if (p->next != LNULL) { 103 | p->next->prev = p->prev; 104 | } 105 | } 106 | 107 | free(p); 108 | } 109 | 110 | void deleteList(tList *L) { 111 | tPosL p; 112 | 113 | while (!isEmptyList(*L)) { 114 | p = *L; 115 | *L = (*L)->next; 116 | free(p); 117 | } 118 | } 119 | 120 | bool copyList(tList L, tList *M) { 121 | 122 | tPosL p, q, r; 123 | bool result = true; 124 | 125 | createEmptyList(M); 126 | if (!isEmptyList(L)) { 127 | p = L; 128 | while ((p != LNULL) && 129 | (createNode(&r))) { // createNode() check if there's space enough to continue 130 | r->data = p->data; 131 | r->next = LNULL; 132 | r->prev = LNULL; 133 | if (p == L) { //If we are copying the first element of the list 134 | *M = r; 135 | q = r; 136 | } else { 137 | q->next = r; 138 | r->prev = q; 139 | q = r; 140 | } 141 | p = p->next; //Advance one place in original list 142 | } 143 | if (p != LNULL) // If there's no more free space to continue 144 | { 145 | result = false; 146 | deleteList(M); 147 | } 148 | } 149 | return result; 150 | } 151 | -------------------------------------------------------------------------------- /Lists/Doubly_Linked_List/Exers/doubly_linked_list.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | 5 | #define LNULL NULL 6 | 7 | typedef int tItemL; 8 | 9 | typedef struct tNode* tPosL; //Ptr to tNode 10 | 11 | struct tNode { 12 | 13 | tItemL data; 14 | tPosL next; 15 | tPosL prev; 16 | 17 | }; 18 | 19 | typedef tPosL tList; 20 | 21 | //Prototypes 22 | 23 | void createEmptyList(tList *L); 24 | bool createNode(tPosL* p); 25 | bool insertItem(tItemL d, tPosL p, tList *L); 26 | void updateItem(tItemL d, tPosL p, tList* L); 27 | tPosL findItem(tItemL d,tList L); 28 | bool isEmptyList(tList L); 29 | tItemL getItem(tPosL p, tList L); 30 | tPosL first(tList L); 31 | tPosL last(tList L); 32 | tPosL previous(tPosL p, tList L); 33 | tPosL next(tPosL p,tList L); 34 | void deleteAtPosition(tPosL p , tList *L); 35 | void deleteList(tList *L); 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Lists/Doubly_Linked_List/Exers/exers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "doubly_linked_list.h" 3 | 4 | //EXER 1: Print double list in reverse order. 5 | 6 | 7 | 8 | void insertion(tList *List){ 9 | 10 | tPosL p = LNULL; 11 | 12 | insertItem(1,p,List); 13 | insertItem(2, p, List); 14 | insertItem(3, p, List); 15 | insertItem(4, p, List); 16 | } 17 | 18 | void printElement(tItemL element){ 19 | printf(" %d ",element); 20 | } 21 | 22 | void reverse_printList(tList List){ 23 | tPosL tmp; 24 | if (List!=LNULL) { 25 | //Goto last element 26 | for (tmp=List;tmp->next!=LNULL;tmp=tmp->next); 27 | while (tmp!=LNULL) { 28 | printElement(tmp->data); 29 | tmp=tmp->prev; 30 | } 31 | } 32 | } 33 | 34 | void printList(tList List){ 35 | tPosL tmp; 36 | if (List!=LNULL) { 37 | //Goto last element 38 | for (tmp=List;tmp!=LNULL;tmp=tmp->next){ 39 | printElement(tmp->data); 40 | } 41 | } 42 | } 43 | 44 | int main(){ 45 | 46 | tList List; 47 | createEmptyList(&List); 48 | 49 | insertion(&List); 50 | printList(List); 51 | printf("\n\n"); 52 | reverse_printList(List); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Lists/Doubly_Linked_List/doubly_linked_list.c: -------------------------------------------------------------------------------- 1 | #include "doubly_linked_list.h" 2 | #include 3 | 4 | bool isEmptyList(tList L) { return (L == NULL); } 5 | 6 | void createEmptyList(tList *L) { *L = LNULL; } 7 | 8 | bool createNode(tPosL *p) { 9 | *p = malloc(sizeof(struct tNode)); 10 | return *p != NULL; 11 | } 12 | 13 | bool insertItem(tItemL d, tPosL p, tList *L) { 14 | tPosL q, r; //q--> element we want to add 15 | //r-->element prior to q 16 | if (!createNode(&q)) { 17 | return false; 18 | } else { 19 | q->data = d; 20 | q->next = LNULL; 21 | q->prev = LNULL; 22 | if (isEmptyList(*L)) { 23 | *L = q; 24 | } else if (p == LNULL) { //If given position is NULL, add the element at the end of list 25 | for (r = *L; r->next != LNULL; r = r->next) 26 | ; //We move to the end of the list 27 | r->next = q; 28 | q->prev = r; 29 | 30 | } else if (p == *L) { // insert at the top of the list (first element) 31 | q->next = p; 32 | p->prev = q; 33 | *L = q; 34 | 35 | } else { //insert in intermediate position 36 | 37 | q->data = p->data; // data exchange 38 | p->data = d; 39 | if (p->next != LNULL) { //in case of inserting in the last position we cannot access the "previous" field of NULL 40 | p->next->prev = q; 41 | } 42 | q->next = p->next; 43 | p->next = q; 44 | q->prev = p; 45 | } 46 | return true; 47 | } 48 | } 49 | 50 | void updateItem(tItemL d, tPosL p, tList *L) { 51 | p->data = d; 52 | } 53 | 54 | tPosL findItem(tItemL d, tList L) { 55 | tPosL p; 56 | 57 | for (p = L; (p != NULL) && (p->data != d); p = p->next) 58 | ; 59 | return p; 60 | } 61 | 62 | 63 | 64 | tItemL getItem(tPosL p, tList L) { return p->data; } 65 | 66 | 67 | 68 | tPosL first(tList L) { 69 | 70 | return L; // L always points to te first element of the list 71 | } 72 | 73 | tPosL last(tList L) { 74 | tPosL p; 75 | 76 | for (p = L; p->next != LNULL; p = p->next) 77 | ; 78 | return p; 79 | } 80 | 81 | tPosL previous(tPosL p, tList L) { 82 | return p->prev; 83 | } 84 | 85 | tPosL next(tPosL p, tList L) { return p->next; } 86 | 87 | void deleteAtPosition(tPosL p, tList *L) { 88 | tPosL q; 89 | 90 | if (p == *L) { // Delete first element 91 | *L = (*L)->next; 92 | 93 | if (!isEmptyList(*L)) { 94 | (*L)->prev = LNULL; 95 | } 96 | 97 | }else { // Middle and end deletion 98 | 99 | q = p->prev; 100 | q->next = p->next; 101 | 102 | if (p->next != LNULL) { 103 | p->next->prev = p->prev; 104 | } 105 | } 106 | 107 | free(p); 108 | } 109 | 110 | void deleteList(tList *L) { 111 | tPosL p; 112 | 113 | while (!isEmptyList(*L)) { 114 | p = *L; 115 | *L = (*L)->next; 116 | free(p); 117 | } 118 | } 119 | 120 | bool copyList(tList L, tList *M) { 121 | 122 | /* 123 | * p -> L iterator 124 | * q -> M iterator 125 | * r -> tmp position 126 | */ 127 | 128 | tPosL p, q, r; 129 | bool result = true; 130 | 131 | createEmptyList(M); 132 | if (!isEmptyList(L)) { 133 | p = L; 134 | while ((p != LNULL) && 135 | (createNode(&r))) { // createNode() check if there's space enough to continue 136 | r->data = p->data; 137 | r->next = LNULL; 138 | r->prev = LNULL; 139 | if (p == L) { //If we are copying the first element of the list 140 | *M = r; 141 | q = r; 142 | } else { 143 | q->next = r; 144 | r->prev = q; 145 | q = r; 146 | } 147 | p = p->next; //Advance one place in original list 148 | } 149 | if (p != LNULL) // If there's no more free space to continue 150 | { 151 | result = false; 152 | deleteList(M); 153 | } 154 | } 155 | return result; 156 | } 157 | -------------------------------------------------------------------------------- /Lists/Doubly_Linked_List/doubly_linked_list.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | 5 | #define LNULL NULL 6 | 7 | typedef int tItemL; 8 | 9 | typedef struct tNode* tPosL; //Ptr to tNode 10 | 11 | struct tNode { 12 | 13 | tItemL data; 14 | tPosL next; 15 | tPosL prev; 16 | 17 | }; 18 | 19 | typedef tPosL tList; 20 | 21 | //Prototypes 22 | 23 | void createEmptyList(tList *L); 24 | bool createNode(tPosL* p); 25 | bool insertItem(tItemL d, tPosL p, tList *L); 26 | void updateItem(tItemL d, tPosL p, tList* L); 27 | tPosL findItem(tItemL d,tList L); 28 | bool isEmptyList(tList L); 29 | tItemL getItem(tPosL p, tList L); 30 | tPosL first(tList L); 31 | tPosL last(tList L); 32 | tPosL previous(tPosL p, tList L); 33 | tPosL next(tPosL p,tList L); 34 | void deleteAtPosition(tPosL p , tList *L); 35 | void deleteList(tList *L); 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /Lists/List/Exer/linked_list.c: -------------------------------------------------------------------------------- 1 | #include "linked_list.h" 2 | #include 3 | 4 | bool isEmptyList(tList L) { return (L == NULL); } 5 | 6 | void createEmptyList(tList *L) { *L = LNULL; } 7 | 8 | bool createNode(tPosL *p) { 9 | *p = malloc(sizeof(struct tNode)); 10 | return *p != NULL; 11 | } 12 | 13 | bool insertItem(tItemL d, tPosL p, tList *L) { 14 | tPosL q, r; //q--> element we want to add 15 | //r-->element prior to q 16 | if (!createNode(&q)) { 17 | return false; 18 | } else { 19 | q->data = d; 20 | q->next = LNULL; 21 | if (isEmptyList(*L)) { 22 | *L = q; 23 | } else if (p == LNULL) { //If given position is NULL, add the element at the end of list 24 | for (r = *L; r->next != LNULL; r = r->next) 25 | ; //We move to the end of the list 26 | r->next = q; 27 | 28 | } else if (p == *L) { // insert at the top of the list (first element) 29 | q->next = p; 30 | *L = q; 31 | 32 | } else { ///insert in intermediate position 33 | 34 | q->data = p->data; // data exchange 35 | p->data = d; 36 | q->next = p->next; 37 | p->next = q; 38 | } 39 | return true; 40 | } 41 | } 42 | 43 | void updateItem(tItemL d, tPosL p, tList *L) { 44 | p->data = d; 45 | } 46 | 47 | tPosL findItem(tItemL d, tList L) { 48 | tPosL p; 49 | 50 | for (p = L; (p != NULL) && (p->data != d); p = p->next) 51 | ; 52 | return p; 53 | } 54 | 55 | 56 | tItemL getItem(tPosL p, tList L) { return p->data; } 57 | 58 | 59 | tPosL first(tList L) { 60 | 61 | return L; // L always points to te first element of the list 62 | } 63 | 64 | tPosL last(tList L) { 65 | 66 | tPosL p; 67 | 68 | for (p = L; p->next != LNULL; p = p->next) 69 | ; 70 | return p; 71 | } 72 | 73 | tPosL previous(tPosL p, tList L) { 74 | tPosL q; 75 | 76 | if (p == L) { 77 | return LNULL; 78 | } else { 79 | for (q = L; q->next != p; q = q->next) 80 | ; 81 | return q; 82 | } 83 | } 84 | 85 | tPosL next(tPosL p, tList L) { return p->next; } 86 | 87 | void deleteAtPosition(tPosL p, tList *L) { 88 | tPosL q; 89 | 90 | if (p == *L) { // Delete first element 91 | *L = (*L)->next; 92 | } else if (p->next == LNULL) { //Delete last element 93 | for (q = *L; q->next->next != LNULL; q = q->next) 94 | ; 95 | q->next = LNULL; 96 | 97 | } else { //Middle deletion Overwrite p with q 98 | //p--> element we want to delete 99 | //q-->next element 100 | 101 | q = p->next; 102 | p->data = q->data; 103 | p->next = q->next; 104 | p = q; 105 | } 106 | 107 | free(p); 108 | } 109 | 110 | void deleteList(tList *L) { 111 | tPosL p; 112 | 113 | while (!isEmptyList(*L)) { 114 | p = *L; 115 | *L = (*L)->next; 116 | free(p); 117 | } 118 | } 119 | 120 | bool copyList(tList L, tList *M) { 121 | 122 | tPosL p, q, r; 123 | bool result = true; 124 | 125 | createEmptyList(M); 126 | if (!isEmptyList(L)) { 127 | p = L; //Point p to the top of the list 128 | while ((p != LNULL) && 129 | (createNode(&r))) { // createNode() check if there's space enough to continue 130 | r->data = p->data; 131 | r->next = LNULL; 132 | if (p == L) { //If we are copying the first element of the list 133 | *M = r; 134 | q = r; 135 | } else { 136 | q->next = r; 137 | q = r; 138 | } 139 | p = p->next; //Advance one place in original list 140 | } 141 | if (p != LNULL) // If there's no more availible space 142 | { 143 | result = false; 144 | deleteList(M); 145 | } 146 | } 147 | return result; 148 | } 149 | -------------------------------------------------------------------------------- /Lists/List/Exer/linked_list.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | 5 | #define LNULL NULL 6 | 7 | typedef int tItemL; 8 | 9 | typedef struct tNode* tPosL; //Ptr to tNode 10 | 11 | struct tNode { 12 | 13 | tItemL data; 14 | tPosL next; 15 | 16 | }; 17 | 18 | typedef tPosL tList; 19 | 20 | /* Function prototypes */ 21 | 22 | void createEmptyList(tList *L); 23 | bool createNode(tPosL* p); 24 | bool insertItem(tItemL d, tPosL p, tList *L); 25 | void updateItem(tItemL d, tPosL p, tList* L); 26 | tPosL findItem(tItemL d,tList L); 27 | bool isEmptyList(tList L); 28 | tItemL getItem(tPosL p, tList L); 29 | tPosL first(tList L); 30 | tPosL last(tList L); 31 | tPosL previous(tPosL p, tList L); 32 | tPosL next(tPosL p,tList L); 33 | void deleteAtPosition(tPosL p , tList *L); 34 | void deleteList(tList *L); 35 | bool copyList(tList L, tList *M); 36 | 37 | -------------------------------------------------------------------------------- /Lists/List/Exer/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "linked_list.h" 3 | //#include "static_list.h" 4 | 5 | //Introduce a few elements 6 | 7 | void introduce(tList *L){ 8 | 9 | insertItem(1,LNULL,L); 10 | insertItem(2,LNULL,L); 11 | insertItem(3,LNULL,L); 12 | } 13 | 14 | //Print List 15 | 16 | void printList(tList L){ 17 | tPosL p; 18 | printf("("); 19 | if (!isEmptyList(L)) 20 | { 21 | p=first(L); 22 | while (p!=LNULL) { 23 | 24 | printf(" %d ",getItem(p,L)); 25 | p=next(p,L); 26 | 27 | } 28 | 29 | } 30 | printf(")"); 31 | 32 | } 33 | 34 | //Reverse Print List 35 | 36 | void reverse_print(tList L){ 37 | 38 | tPosL p; 39 | 40 | printf("("); 41 | if (!isEmptyList(L)) 42 | { 43 | 44 | p=last(L); 45 | while (p!=LNULL) { 46 | 47 | printf(" %d ",getItem(p,L)); 48 | p=previous(p,L); 49 | 50 | } 51 | 52 | } 53 | printf(")"); 54 | 55 | } 56 | 57 | int main(){ 58 | //Initialization 59 | tList L; 60 | tList H; 61 | createEmptyList(&L); 62 | introduce(&L); 63 | printList(L); 64 | // copyList(L,&H); 65 | //printList(H); 66 | printf("\n\n\n"); 67 | reverse_print(L); 68 | printf("\n"); 69 | 70 | 71 | return 0; 72 | } -------------------------------------------------------------------------------- /Lists/List/Exer/static_list.c: -------------------------------------------------------------------------------- 1 | #include "static_list.h" 2 | 3 | bool isEmptyList(tList L) { return L.lastPos == LNULL; } 4 | 5 | void createEmptyList(tList *L) { L->lastPos = LNULL; } 6 | 7 | bool insertItem(tItemL d, tPosL p, tList *L) { 8 | tPosL i; 9 | 10 | if (L->lastPos == MAX - 1) 11 | return false; 12 | else { 13 | L->lastPos++; 14 | if (p == LNULL) { // Insert at the end 15 | L->data[L->lastPos] = d; 16 | } else { 17 | for (i = L->lastPos; i >= p + 1;i--) //moves the elements one position forward 18 | L->data[i] = L->data[i - 1]; 19 | L->data[p] = d; 20 | } 21 | return true; 22 | } 23 | } 24 | 25 | bool copyList(tList L, tList *M) { 26 | tPosL p; 27 | 28 | createEmptyList(M); //First initialize the destination list 29 | 30 | for (p = 0; p <= L.lastPos; p++) 31 | M->data[p] = L.data[p]; 32 | M->lastPos = L.lastPos; 33 | return true; 34 | } 35 | 36 | void updateItem(tItemL d, tPosL p, tList *L) { L->data[p] = d; } 37 | 38 | tPosL findItem(tItemL d, tList L) { 39 | tPosL p; 40 | 41 | if (isEmptyList(L)) { 42 | return LNULL; 43 | } else { 44 | for (p = 0; (p < L.lastPos) && (L.data[p] != d); p++) 45 | ; 46 | 47 | if (L.data[p] == d) { 48 | return p; 49 | } else 50 | return LNULL; 51 | } 52 | } 53 | 54 | 55 | 56 | tItemL getItem(tPosL p, tList L) { return L.data[p]; } 57 | 58 | tPosL first(tList L) { 59 | return 0; 60 | } 61 | 62 | tPosL last(tList L) { return L.lastPos; } 63 | 64 | tPosL previous(tPosL p, tList L) { return --p; } 65 | 66 | tPosL next(tPosL p, tList L) { 67 | if (p == L.lastPos) 68 | return LNULL; 69 | else 70 | return ++p; 71 | } 72 | 73 | 74 | void deleteAtPosition(tPosL p, tList *L) { 75 | tPosL i; 76 | 77 | L->lastPos--; 78 | for (i = p; i <= L->lastPos; i++) 79 | L->data[i] = L->data[i + 1]; //Move the elements back one position 80 | } 81 | 82 | void deleteList(tList *L) { 83 | L->lastPos = LNULL; 84 | } 85 | -------------------------------------------------------------------------------- /Lists/List/Exer/static_list.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | 5 | #define LNULL -1 6 | #define MAX 1000 7 | 8 | typedef int tItemL; 9 | typedef int tPosL; 10 | 11 | typedef struct { 12 | tItemL data[MAX]; 13 | tPosL lastPos; 14 | } tList; 15 | 16 | 17 | 18 | /* Function prototypes */ 19 | 20 | void createEmptyList(tList* L); 21 | bool insertItem(tItemL d, tPosL p, tList* L); 22 | bool copyList(tList L, tList* M); 23 | void updateItem(tItemL d , tPosL p, tList* L); 24 | void deleteAtPosition(tPosL p, tList* L); 25 | void deleteList(tList* L); 26 | tPosL findItem(tItemL d, tList L); 27 | bool isEmptyList(tList L); 28 | tItemL getItem(tPosL p, tList L); 29 | tPosL first(tList L); 30 | tPosL last(tList L); 31 | tPosL previous(tPosL p, tList L); 32 | tPosL next(tPosL p, tList L); 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Lists/List/linked_list.c: -------------------------------------------------------------------------------- 1 | #include "linked_list.h" 2 | #include 3 | 4 | bool isEmptyList(tList L) { return (L == LNULL); } 5 | 6 | void createEmptyList(tList *L) { *L = LNULL; } 7 | 8 | bool createNode(tPosL *p) //Equivalent to (tNode **p) pointer to pointer that's why we do *p = malloc..... 9 | { 10 | *p = malloc(sizeof(struct tNode)); 11 | return *p != LNULL; 12 | } 13 | 14 | bool insertItem(tItemL d, tPosL p, tList *L) 15 | { 16 | tPosL q, r; //q--> element we want to add 17 | //r-->element prior to q 18 | if (!createNode(&q)) 19 | { 20 | return false; 21 | } 22 | else 23 | { 24 | q->data = d; 25 | q->next = LNULL; 26 | if (isEmptyList(*L)) 27 | { 28 | *L = q; 29 | } 30 | else if (p == LNULL) 31 | { //If given position is NULL, add the element at the end of list 32 | for (r = *L; r->next != LNULL; r = r->next) 33 | ; //We move to the end of the list 34 | r->next = q; 35 | } 36 | else if (p == *L) 37 | { // insert at the top of the list (first element) 38 | q->next = p; 39 | *L = q; 40 | } 41 | else 42 | { ///insert in intermediate position 43 | 44 | q->data = p->data; // data exchange 45 | p->data = d; 46 | q->next = p->next; 47 | p->next = q; 48 | } 49 | return true; 50 | } 51 | } 52 | 53 | void updateItem(tItemL d, tPosL p, tList *L) 54 | { 55 | p->data = d; 56 | } 57 | 58 | tPosL findItem(tItemL d, tList L) 59 | { 60 | tPosL p; 61 | 62 | for (p = L; (p != LNULL) && (p->data != d); p = p->next) 63 | ; 64 | return p; 65 | } 66 | 67 | tItemL getItem(tPosL p, tList L) { return p->data; } 68 | 69 | tPosL first(tList L) 70 | { 71 | 72 | return L; // L always points to te first element of the list 73 | } 74 | 75 | tPosL last(tList L) 76 | { 77 | 78 | tPosL p; 79 | 80 | for (p = L; p->next != LNULL; p = p->next) 81 | ; 82 | return p; 83 | } 84 | 85 | tPosL previous(tPosL p, tList L) 86 | { 87 | tPosL q; 88 | 89 | if (p == L) 90 | { 91 | return LNULL; 92 | } 93 | else 94 | { 95 | for (q = L; q->next != p; q = q->next) 96 | ; 97 | return q; 98 | } 99 | } 100 | 101 | tPosL next(tPosL p, tList L) { return p->next; } 102 | 103 | void deleteAtPosition(tPosL p, tList *L) 104 | { 105 | tPosL q; 106 | 107 | if (p == *L) 108 | { // Delete first element 109 | *L = (*L)->next; 110 | } 111 | else if (p->next == LNULL) 112 | { //Delete last element 113 | for (q = *L; q->next->next != LNULL; q = q->next) 114 | ; 115 | q->next = LNULL; 116 | } 117 | else 118 | { //Middle deletion Overwrite p with q 119 | //p--> element we want to delete 120 | //q-->next element 121 | 122 | q = p->next; 123 | p->data = q->data; 124 | p->next = q->next; 125 | p = q; 126 | } 127 | 128 | free(p); 129 | } 130 | 131 | void deleteList(tList *L) 132 | { 133 | tPosL p; 134 | 135 | while (!isEmptyList(*L)) 136 | { 137 | p = *L; 138 | *L = (*L)->next; 139 | free(p); 140 | } 141 | } 142 | 143 | bool copyList(tList L, tList *M) 144 | { 145 | /* 146 | * p -> L iterator 147 | * q -> M iterator 148 | * r -> tmp position 149 | */ 150 | 151 | tPosL p, q, r; 152 | bool result = true; 153 | 154 | createEmptyList(M); 155 | if (!isEmptyList(L)) 156 | { 157 | p = L; //Point p to the top of the list 158 | while ((p != LNULL) && 159 | (createNode(&r))) 160 | { // createNode() check if there's space enough to continue 161 | r->data = p->data; 162 | r->next = LNULL; 163 | 164 | if (p == L) 165 | { //If we are copying the first element of the list 166 | *M = r; 167 | q = r; 168 | } 169 | else 170 | { 171 | q->next = r; 172 | q = r; 173 | } 174 | p = p->next; //Advance one place in original list 175 | } 176 | 177 | if (p != LNULL) // If there's no more availible space 178 | { 179 | result = false; 180 | deleteList(M); 181 | } 182 | } 183 | 184 | return result; 185 | } 186 | -------------------------------------------------------------------------------- /Lists/List/linked_list.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | 5 | #define LNULL NULL 6 | 7 | typedef int tItemL; 8 | 9 | typedef struct tNode* tPosL; //Ptr to tNode 10 | 11 | struct tNode { 12 | 13 | tItemL data; 14 | tPosL next; 15 | 16 | }; 17 | 18 | typedef tPosL tList; 19 | 20 | /* Function prototypes */ 21 | 22 | void createEmptyList(tList *L); 23 | bool createNode(tPosL* p); 24 | bool insertItem(tItemL d, tPosL p, tList *L); 25 | void updateItem(tItemL d, tPosL p, tList* L); 26 | tPosL findItem(tItemL d,tList L); 27 | bool isEmptyList(tList L); 28 | tItemL getItem(tPosL p, tList L); 29 | tPosL first(tList L); 30 | tPosL last(tList L); 31 | tPosL previous(tPosL p, tList L); 32 | tPosL next(tPosL p,tList L); 33 | void deleteAtPosition(tPosL p , tList *L); 34 | void deleteList(tList *L); 35 | 36 | -------------------------------------------------------------------------------- /Lists/List/static_list.c: -------------------------------------------------------------------------------- 1 | #include "static_list.h" 2 | 3 | bool isEmptyList(tList L) { return L.lastPos == LNULL; } 4 | 5 | void createEmptyList(tList *L) { L->lastPos = LNULL; } 6 | 7 | bool insertItem(tItemL d, tPosL p, tList *L) { 8 | tPosL i; 9 | 10 | if (L->lastPos == MAX - 1) //Check if list is full 11 | return false; 12 | else { 13 | L->lastPos++; 14 | if (p == LNULL) { // Insert at the end 15 | L->data[L->lastPos] = d; 16 | } else { 17 | for (i = L->lastPos; i > p;i--) //moves the elements one position forward 18 | L->data[i] = L->data[i - 1]; 19 | 20 | L->data[p] = d; 21 | } 22 | return true; 23 | } 24 | } 25 | 26 | bool copyList(tList L, tList *M) { 27 | tPosL p; 28 | 29 | createEmptyList(M); //First initialize the destination list 30 | 31 | for (p = 0; p <= L.lastPos; p++) 32 | M->data[p] = L.data[p]; 33 | M->lastPos = L.lastPos; 34 | return true; 35 | } 36 | 37 | void updateItem(tItemL d, tPosL p, tList *L) { L->data[p] = d; } 38 | 39 | tPosL findItem(tItemL d, tList L) { 40 | tPosL p; 41 | 42 | if (isEmptyList(L)) { 43 | return LNULL; 44 | } else { 45 | for (p = 0; (p < L.lastPos) && (L.data[p] != d); p++) 46 | ; 47 | 48 | if (L.data[p] == d) { 49 | return p; 50 | } else 51 | return LNULL; 52 | } 53 | } 54 | 55 | 56 | 57 | tItemL getItem(tPosL p, tList L) { return L.data[p]; } 58 | 59 | tPosL first(tList L) { 60 | return 0; 61 | } 62 | 63 | tPosL last(tList L) { return L.lastPos; } 64 | 65 | tPosL previous(tPosL p, tList L) { return --p; } 66 | 67 | tPosL next(tPosL p, tList L) { 68 | if (p == L.lastPos) 69 | return LNULL; 70 | else 71 | return ++p; 72 | } 73 | 74 | 75 | void deleteAtPosition(tPosL p, tList *L) { 76 | tPosL i; 77 | 78 | L->lastPos--; 79 | for (i = p; i <= L->lastPos; i++) 80 | L->data[i] = L->data[i + 1]; //Move the elements back one position 81 | } 82 | 83 | void deleteList(tList *L) { 84 | L->lastPos = LNULL; 85 | } 86 | -------------------------------------------------------------------------------- /Lists/List/static_list.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | 5 | #define LNULL -1 6 | #define MAX 1000 7 | 8 | typedef int tItemL; 9 | typedef int tPosL; 10 | 11 | typedef struct { 12 | tItemL data[MAX]; 13 | tPosL lastPos; 14 | } tList; 15 | 16 | 17 | 18 | /* Function prototypes */ 19 | 20 | void createEmptyList(tList* L); 21 | bool insertItem(tItemL d, tPosL p, tList* L); 22 | bool copyList(tList L, tList* M); 23 | void updateItem(tItemL d , tPosL p, tList* L); 24 | void deleteAtPosition(tPosL p, tList* L); 25 | void deleteList(tList* L); 26 | tPosL findItem(tItemL d, tList L); 27 | bool isEmptyList(tList L); 28 | tItemL getItem(tPosL p, tList L); 29 | tPosL first(tList L); 30 | tPosL last(tList L); 31 | tPosL previous(tPosL p, tList L); 32 | tPosL next(tPosL p, tList L); 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Lists/Ordered_List/Exer/linked_ordered_list.c: -------------------------------------------------------------------------------- 1 | #include "linked_ordered_list.h" 2 | #include 3 | #include 4 | 5 | bool isEmptyList(tList L) { return (L == LNULL); } 6 | 7 | void createEmptyList(tList *L) { *L = LNULL; } 8 | 9 | bool createNode(tPosL *p) 10 | { 11 | *p = malloc(sizeof(struct tNode)); 12 | return *p != LNULL; 13 | } 14 | 15 | tPosL findPosition(tList L, tItemL d) 16 | { 17 | 18 | tPosL p, tmp; 19 | 20 | p = L; 21 | tmp = L; 22 | while ((p != LNULL) && 23 | (p->data < d)) 24 | { //Continue while data is ordered 25 | tmp = p; 26 | p = p->next; 27 | } 28 | return tmp; 29 | } 30 | 31 | bool insertItem(tItemL d, tList *L) 32 | { 33 | tPosL q, p; 34 | 35 | if (!createNode(&q)) 36 | { 37 | return false; 38 | } 39 | else 40 | { 41 | q->data = d; 42 | q->next = LNULL; 43 | 44 | if (isEmptyList(*L)) 45 | { 46 | *L = q; 47 | } 48 | else if (d < (*L)->data) 49 | { // insert at the top of the list (first element) 50 | 51 | q->next = *L; 52 | *L = q; 53 | } 54 | else 55 | { //Find the right position 56 | p = findPosition(*L, d); 57 | q->next = p->next; 58 | p->next = q; 59 | } 60 | return true; 61 | } 62 | } 63 | 64 | void updateItem(tItemL d, tPosL p, tList *L) 65 | { 66 | p->data = d; 67 | } 68 | 69 | tPosL findItem(tItemL d, tList L) 70 | { 71 | tPosL p; 72 | 73 | for (p = L; (p != LNULL) && (p->data < d); p = p->next) 74 | ; 75 | 76 | return p; 77 | } 78 | 79 | tItemL getItem(tPosL p, tList L) { return p->data; } 80 | 81 | tPosL first(tList L) 82 | { 83 | 84 | return L; // L always points to te first element of the list 85 | } 86 | 87 | tPosL last(tList L) 88 | { 89 | 90 | tPosL p; 91 | 92 | for (p = L; p->next != LNULL; p = p->next) 93 | ; 94 | return p; 95 | } 96 | 97 | tPosL previous(tPosL p, tList L) 98 | { 99 | tPosL q; 100 | 101 | if (p == L) 102 | { 103 | return LNULL; 104 | } 105 | else 106 | { 107 | for (q = L; q->next != p; q = q->next) 108 | ; 109 | return q; 110 | } 111 | } 112 | 113 | tPosL next(tPosL p, tList L) { return p->next; } 114 | 115 | void deleteAtPosition(tPosL p, tList *L) 116 | { 117 | tPosL q; 118 | 119 | if (p == *L) 120 | { // Delete first element 121 | *L = (*L)->next; 122 | } 123 | else if (p->next == LNULL) 124 | { //Delete last element 125 | for (q = *L; q->next->next != p; q = q->next) 126 | ; 127 | q->next = LNULL; 128 | } 129 | else 130 | { //Middle deletion Overwrite p with q 131 | //p--> element we want to delete 132 | //q-->next element 133 | q = p->next; 134 | p->data = q->data; 135 | p->next = q->next; 136 | p = q; 137 | } 138 | 139 | free(p); 140 | } 141 | 142 | void deleteList(tList *L) 143 | { 144 | tPosL p; 145 | 146 | while (!isEmptyList(*L)) 147 | { 148 | p = *L; 149 | *L = (*L)->next; 150 | free(p); 151 | } 152 | } 153 | 154 | bool copyList(tList L, tList *M) 155 | { 156 | 157 | tPosL p, q, r; 158 | bool result = true; 159 | 160 | createEmptyList(M); 161 | if (!isEmptyList(L)) 162 | { 163 | p = L; //Point p to the top of the list 164 | while ((p != LNULL) && 165 | (createNode(&r))) 166 | { // createNode() check if there's space enough to continue 167 | r->data = p->data; 168 | r->next = LNULL; 169 | if (p == L) //If we are copying the first element of the list 170 | { 171 | *M = r; 172 | q = r; 173 | } 174 | else 175 | { 176 | q->next = r; 177 | q = r; 178 | } 179 | p = p->next; //Advance one place in original list 180 | } 181 | if (p != LNULL) // In case there's not enough space availible 182 | { 183 | result = false; 184 | deleteList(M); 185 | } 186 | } 187 | return result; 188 | } 189 | -------------------------------------------------------------------------------- /Lists/Ordered_List/Exer/linked_ordered_list.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | 5 | #define LNULL NULL 6 | 7 | typedef int tItemL; 8 | 9 | typedef struct tNode* tPosL; //Ptr to tNode 10 | 11 | struct tNode { 12 | 13 | tItemL data; 14 | tPosL next; 15 | 16 | }; 17 | 18 | typedef tPosL tList; 19 | 20 | /* Function prototypes */ 21 | 22 | void createEmptyList(tList *L); 23 | bool createNode(tPosL* p); 24 | bool insertItem(tItemL d,tList *L); 25 | tPosL findPosition(tList L, tItemL d); 26 | void updateItem(tItemL d, tPosL p, tList* L); 27 | tPosL findItem(tItemL d,tList L); 28 | bool isEmptyList(tList L); 29 | tItemL getItem(tPosL p, tList L); 30 | tPosL first(tList L); 31 | tPosL last(tList L); 32 | tPosL previous(tPosL p, tList L); 33 | tPosL next(tPosL p,tList L); 34 | void deleteAtPosition(tPosL p , tList *L); 35 | void deleteList(tList *L); 36 | 37 | 38 | -------------------------------------------------------------------------------- /Lists/Ordered_List/Exer/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "linked_ordered_list.h" 3 | //#include "static_ordered_list.h" 4 | 5 | //Introduce a few elements 6 | 7 | void introduce(tList *L){ 8 | insertItem(2,L); 9 | insertItem(1,L); 10 | insertItem(3,L); 11 | } 12 | 13 | //Print List 14 | 15 | void printList(tList L){ 16 | tPosL p; 17 | printf("("); 18 | if (!isEmptyList(L)) 19 | { 20 | p=first(L); 21 | while (p!=LNULL) { 22 | 23 | printf(" %d ",getItem(p,L)); 24 | p=next(p,L); 25 | 26 | } 27 | 28 | } 29 | printf(")"); 30 | 31 | } 32 | 33 | //Reverse Print List 34 | 35 | void reverse_print(tList L){ 36 | 37 | tPosL p; 38 | 39 | printf("("); 40 | if (!isEmptyList(L)) 41 | { 42 | 43 | p=last(L); 44 | while (p!=LNULL) { 45 | 46 | printf(" %d ",getItem(p,L)); 47 | p=previous(p,L); 48 | 49 | } 50 | 51 | } 52 | printf(")"); 53 | 54 | } 55 | 56 | int main(){ 57 | //Initialization 58 | tList L; 59 | createEmptyList(&L); 60 | 61 | introduce(&L); 62 | printList(L); 63 | printf("\n\n\n"); 64 | reverse_print(L); 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Lists/Ordered_List/Exer/static_ordered_list.c: -------------------------------------------------------------------------------- 1 | #include "static_ordered_list.h" 2 | #include 3 | 4 | bool isEmptyList(tList L) { return L.lastPos == LNULL; } 5 | 6 | void createEmptyList(tList *L) { L->lastPos = LNULL; } 7 | 8 | bool insertItem(tItemL d, tList *L) { 9 | tPosL i; 10 | 11 | if (L->lastPos == MAX - 1) //Check if list is full 12 | return false; 13 | else { 14 | L->lastPos++; 15 | if (isEmptyList(*L) || d > L->data[L->lastPos]) { // Insert at the end 16 | L->data[L->lastPos] = d; 17 | } else { 18 | for (i = L->lastPos; (i > 0) && (d < L->data[i - 1]); i--) { 19 | L->data[i] = L->data[i - 1]; //moves the elements one position forwar 20 | } 21 | L->data[i] = d; //Finally insert element in right position 22 | } 23 | return true; 24 | } 25 | } 26 | 27 | bool copyList(tList L, tList *M) { 28 | tPosL p; 29 | 30 | for (p = 0; p <= L.lastPos; p++) 31 | M->data[p] = L.data[p]; 32 | M->lastPos = L.lastPos; 33 | return true; 34 | } 35 | 36 | void updateItem(tItemL d, tPosL p, tList *L) { L->data[p] = d; } 37 | 38 | tPosL findItem(tItemL d, tList L) { 39 | tPosL p; 40 | 41 | if (isEmptyList(L)){ 42 | return LNULL; 43 | }else if ((d < L.data[0]) || (d > L.data[L.lastPos])) { //If the item is not in the list 44 | return LNULL; 45 | } else { 46 | for (p = 0; (p < L.lastPos) && (L.data[p] < d); p++) 47 | ; 48 | if (L.data[p] == d) { 49 | return p; 50 | } else { 51 | return LNULL; 52 | } 53 | } 54 | } 55 | 56 | 57 | tItemL getItem(tPosL p, tList L) { return L.data[p]; } 58 | 59 | tPosL first(tList L) { 60 | return 0; 61 | } 62 | 63 | tPosL last(tList L) { return L.lastPos; } 64 | 65 | tPosL previous(tPosL p, tList L) { return --p; } 66 | 67 | tPosL next(tPosL p, tList L) { 68 | if (p == L.lastPos) 69 | return LNULL; 70 | else 71 | return ++p; 72 | } 73 | 74 | 75 | void deleteAtPosition(tPosL p, tList *L) { 76 | tPosL i; 77 | 78 | L->lastPos--; 79 | for (i = p; i <= L->lastPos; i++) //Move the elements back one position 80 | L->data[i] = L->data[i + 1]; 81 | } 82 | 83 | void deleteList(tList *L) { 84 | L->lastPos = LNULL; 85 | } 86 | -------------------------------------------------------------------------------- /Lists/Ordered_List/Exer/static_ordered_list.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | 5 | #define LNULL -1 6 | #define MAX 1000 7 | 8 | typedef int tItemL; 9 | typedef int tPosL; 10 | 11 | typedef struct { 12 | tItemL data[MAX]; 13 | tPosL lastPos; 14 | } tList; 15 | 16 | 17 | 18 | /* Function prototypes */ 19 | 20 | void createEmptyList(tList* L); 21 | bool insertItem(tItemL d, tList* L); 22 | bool copyList(tList L, tList* M); 23 | void updateItem(tItemL d , tPosL p, tList* L); 24 | void deleteAtPosition(tPosL p, tList* L); 25 | void deleteList(tList* L); 26 | tPosL findItem(tItemL d, tList L); 27 | bool isEmptyList(tList L); 28 | tItemL getItem(tPosL p, tList L); 29 | tPosL first(tList L); 30 | tPosL last(tList L); 31 | tPosL previous(tPosL p, tList L); 32 | tPosL next(tPosL p, tList L); 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Lists/Ordered_List/linked_ordered_list.c: -------------------------------------------------------------------------------- 1 | #include "linked_ordered_list.h" 2 | #include 3 | #include 4 | 5 | bool isEmptyList(tList L) { return (L == LNULL); } 6 | 7 | void createEmptyList(tList *L) { *L = LNULL; } 8 | 9 | bool createNode(tPosL *p) 10 | { 11 | *p = malloc(sizeof(struct tNode)); 12 | return *p != LNULL; 13 | } 14 | 15 | tPosL findPosition(tList L, tItemL d) 16 | { 17 | 18 | tPosL p, tmp; 19 | 20 | p = L; 21 | tmp = L; 22 | while ((p != LNULL) && 23 | (p->data < d)) 24 | { //Continue while data is ordered 25 | tmp = p; 26 | p = p->next; 27 | } 28 | return tmp; 29 | } 30 | 31 | bool insertItem(tItemL d, tList *L) 32 | { 33 | tPosL q, p; 34 | 35 | if (!createNode(&q)) 36 | { 37 | return false; 38 | } 39 | else 40 | { 41 | q->data = d; 42 | q->next = LNULL; 43 | 44 | if (isEmptyList(*L)) 45 | { 46 | *L = q; 47 | } 48 | else if (d < (*L)->data) 49 | { // insert at the top of the list (first element) 50 | 51 | q->next = *L; 52 | *L = q; 53 | } 54 | else 55 | { //Find right position 56 | p = findPosition(*L, d); 57 | q->next = p->next; 58 | p->next = q; 59 | } 60 | return true; 61 | } 62 | } 63 | 64 | void updateItem(tItemL d, tPosL p, tList *L) 65 | { 66 | p->data = d; 67 | } 68 | 69 | tPosL findItem(tItemL d, tList L) 70 | { 71 | tPosL p; 72 | 73 | for (p = L; (p != LNULL) && (p->data < d); p = p->next) 74 | ; 75 | 76 | return p; 77 | } 78 | 79 | tItemL getItem(tPosL p, tList L) { return p->data; } 80 | 81 | tPosL first(tList L) 82 | { 83 | 84 | return L; // L always points to te first element of the list 85 | } 86 | 87 | tPosL last(tList L) 88 | { 89 | 90 | tPosL p; 91 | 92 | for (p = L; p->next != LNULL; p = p->next) 93 | ; 94 | return p; 95 | } 96 | 97 | tPosL previous(tPosL p, tList L) 98 | { 99 | tPosL q; 100 | 101 | if (p == L) 102 | { 103 | return LNULL; 104 | } 105 | else 106 | { 107 | for (q = L; q->next != p; q = q->next) 108 | ; 109 | return q; 110 | } 111 | } 112 | 113 | tPosL next(tPosL p, tList L) { return p->next; } 114 | 115 | void deleteAtPosition(tPosL p, tList *L) 116 | { 117 | tPosL q; 118 | 119 | if (p == *L) 120 | { // Delete first element 121 | *L = (*L)->next; 122 | } 123 | else if (p->next == LNULL) 124 | { //Delete last element 125 | for (q = *L; q->next->next != p; q = q->next) 126 | ; 127 | q->next = LNULL; 128 | } 129 | else 130 | { //Middle deletion Overwrite p with q 131 | //p--> element we want to delete 132 | //q-->next element 133 | q = p->next; 134 | p->data = q->data; 135 | p->next = q->next; 136 | p = q; 137 | } 138 | 139 | free(p); 140 | } 141 | 142 | void deleteList(tList *L) 143 | { 144 | tPosL p; 145 | 146 | while (!isEmptyList(*L)) 147 | { 148 | p = *L; 149 | *L = (*L)->next; 150 | free(p); 151 | } 152 | } 153 | 154 | bool copyList(tList L, tList *M) 155 | { 156 | 157 | tPosL p, q, r; 158 | bool result = true; 159 | 160 | createEmptyList(M); 161 | if (!isEmptyList(L)) 162 | { 163 | p = L; //Point p to the top of the list 164 | while ((p != LNULL) && 165 | (createNode(&r))) 166 | { // createNode() check if there's space enough to continue 167 | r->data = p->data; 168 | r->next = LNULL; 169 | if (p == L) //If we are copying the first element of the list 170 | { 171 | *M = r; 172 | q = r; 173 | } 174 | else 175 | { 176 | q->next = r; 177 | q = r; 178 | } 179 | p = p->next; //Advance one place in original list 180 | } 181 | if (p != LNULL) // In case there's not enough space availible 182 | { 183 | result = false; 184 | deleteList(M); 185 | } 186 | } 187 | return result; 188 | } 189 | -------------------------------------------------------------------------------- /Lists/Ordered_List/linked_ordered_list.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | 5 | #define LNULL NULL 6 | 7 | typedef int tItemL; 8 | 9 | typedef struct tNode* tPosL; //Ptr to tNode 10 | 11 | struct tNode { 12 | 13 | tItemL data; 14 | tPosL next; 15 | 16 | }; 17 | 18 | typedef tPosL tList; 19 | 20 | /* Function prototypes */ 21 | 22 | void createEmptyList(tList *L); 23 | bool createNode(tPosL* p); 24 | bool insertItem(tItemL d,tList *L); 25 | tPosL findPosition(tList L, tItemL d); 26 | void updateItem(tItemL d, tPosL p, tList* L); 27 | tPosL findItem(tItemL d,tList L); 28 | bool isEmptyList(tList L); 29 | tItemL getItem(tPosL p, tList L); 30 | tPosL first(tList L); 31 | tPosL last(tList L); 32 | tPosL previous(tPosL p, tList L); 33 | tPosL next(tPosL p,tList L); 34 | void deleteAtPosition(tPosL p , tList *L); 35 | void deleteList(tList *L); 36 | 37 | 38 | -------------------------------------------------------------------------------- /Lists/Ordered_List/static_ordered_list.c: -------------------------------------------------------------------------------- 1 | #include "static_ordered_list.h" 2 | #include 3 | 4 | bool isEmptyList(tList L) { return L.lastPos == LNULL; } 5 | 6 | void createEmptyList(tList *L) { L->lastPos = LNULL; } 7 | 8 | bool insertItem(tItemL d, tList *L) { 9 | tPosL i; 10 | 11 | if (L->lastPos == MAX - 1) //Check if list is full 12 | return false; 13 | else { 14 | if (isEmptyList(*L) || d > L->data[L->lastPos]) { // Insert at the end 15 | L->lastPos++; 16 | L->data[L->lastPos] = d; 17 | } else { 18 | L->lastPos++; 19 | for (i = L->lastPos; (i > 0) && (d < L->data[i - 1]); i--) { 20 | L->data[i] = L->data[i - 1]; //moves the elements one position forwar 21 | } 22 | L->data[i] = d; //Finally insert element in right position 23 | } 24 | return true; 25 | } 26 | } 27 | 28 | bool copyList(tList L, tList *M) { 29 | tPosL p; 30 | 31 | createEmptyList(M); //First initialize the destination list 32 | 33 | for (p = 0; p <= L.lastPos; p++) 34 | M->data[p] = L.data[p]; 35 | M->lastPos = L.lastPos; 36 | return true; 37 | } 38 | 39 | void updateItem(tItemL d, tPosL p, tList *L) { L->data[p] = d; } 40 | 41 | tPosL findItem(tItemL d, tList L) { 42 | tPosL p; 43 | 44 | if (isEmptyList(L)){ 45 | return LNULL; 46 | }else if ((d < L.data[0]) || (d > L.data[L.lastPos])) { //If the item is not in the list 47 | return LNULL; 48 | } else { 49 | for (p = 0; (p < L.lastPos) && (L.data[p] < d); p++) 50 | ; 51 | if (L.data[p] == d) { 52 | return p; 53 | } else { 54 | return LNULL; 55 | } 56 | } 57 | } 58 | 59 | 60 | tItemL getItem(tPosL p, tList L) { return L.data[p]; } 61 | 62 | tPosL first(tList L) { 63 | return 0; 64 | } 65 | 66 | tPosL last(tList L) { return L.lastPos; } 67 | 68 | tPosL previous(tPosL p, tList L) { return --p; } 69 | 70 | tPosL next(tPosL p, tList L) { 71 | if (p == L.lastPos) 72 | return LNULL; 73 | else 74 | return ++p; 75 | } 76 | 77 | 78 | void deleteAtPosition(tPosL p, tList *L) { 79 | tPosL i; 80 | 81 | L->lastPos--; 82 | for (i = p; i <= L->lastPos; i++) //Move the elements back one position 83 | L->data[i] = L->data[i + 1]; 84 | } 85 | 86 | void deleteList(tList *L) { 87 | L->lastPos = LNULL; 88 | } 89 | -------------------------------------------------------------------------------- /Lists/Ordered_List/static_ordered_list.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | 5 | #define LNULL -1 6 | #define MAX 1000 7 | 8 | typedef int tItemL; 9 | typedef int tPosL; 10 | 11 | typedef struct { 12 | tItemL data[MAX]; 13 | tPosL lastPos; 14 | } tList; 15 | 16 | 17 | 18 | /* Function prototypes */ 19 | 20 | void createEmptyList(tList* L); 21 | bool insertItem(tItemL d, tList* L); 22 | bool copyList(tList L, tList* M); 23 | void updateItem(tItemL d , tPosL p, tList* L); 24 | void deleteAtPosition(tPosL p, tList* L); 25 | void deleteList(tList* L); 26 | tPosL findItem(tItemL d, tList L); 27 | bool isEmptyList(tList L); 28 | tItemL getItem(tPosL p, tList L); 29 | tPosL first(tList L); 30 | tPosL last(tList L); 31 | tPosL previous(tPosL p, tList L); 32 | tPosL next(tPosL p, tList L); 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Pointers/Basics/basics.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | 6 | 7 | 8 | int main(){ 9 | 10 | int a = 2; 11 | 12 | int *ptr; 13 | 14 | ptr=&a; 15 | 16 | int **p; 17 | p=&ptr; 18 | 19 | 20 | printf("Address: %p\n",ptr); 21 | printf("Value: %d\n",*ptr); 22 | printf("Address: %p\n",&a); 23 | printf("Address: %p\n",p); 24 | printf("Value: %d\n",**p); 25 | 26 | 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Pointers/Character_arrays_ptrs/string_ptr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void print(char* c){ 4 | int i=0; 5 | while(c[i]!='\0'){ 6 | printf("%c",c[i]); 7 | i++; 8 | } 9 | printf("\n"); 10 | } 11 | 12 | int main(){ 13 | char c[20]="Hello"; 14 | print(c); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Pointers/Dynamic_Memory_Allocation/dyn_mem_alloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | 6 | /* 7 | int a; //goes on stack 8 | 9 | int *p; //goes on heap 10 | p= malloc(sizeof(int)); 11 | *p=20; 12 | free(p); 13 | */ 14 | 15 | int n; 16 | printf("Enter size of array\n"); 17 | scanf("%d",&n); 18 | int *A= calloc(n,sizeof(int)); //Dynamically allocated array 19 | for (int i=0; ichange the size of a existing dynami variable 31 | -------------------------------------------------------------------------------- /Pointers/Dynamic_Variables/dyn_var.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | //Dyn Var by value 1st way 6 | 7 | void print1(int param){ 8 | printf("param's value: %d\n",param); 9 | param=0; 10 | } 11 | 12 | //Dyn Var by value 2nd way 13 | 14 | void print2(int *param){ 15 | printf("param's value: %d\n",*param); 16 | param=0; 17 | } 18 | 19 | 20 | //Dyn Var by reference 21 | 22 | void sum(int *par){ 23 | *par=*par + 10; 24 | } 25 | 26 | int main(){ 27 | 28 | //Dyn Var by value 1st way 29 | 30 | int variable = 111; 31 | int *ptr= &variable; 32 | 33 | print1(*ptr); 34 | printf("ptr's value after %d\n",*ptr); 35 | 36 | //Dyn Var by value 2nd way 37 | 38 | print2(ptr); 39 | printf("ptr's value after %d\n",*ptr); 40 | 41 | 42 | 43 | //Dyn Var by reference 44 | 45 | int var = 111; 46 | 47 | printf("ptr's value before %d\n",var); 48 | sum(&var); 49 | printf("ptr's value after %d\n",var); 50 | 51 | 52 | return 0; 53 | } 54 | 55 | -------------------------------------------------------------------------------- /Pointers/Function_Pointers/Func_Ptrs/function_ptrs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | // Pointer to functions 5 | 6 | int Add(int a, int b) { return a + b; } 7 | 8 | void swap(int *a, int *b) { 9 | int temp; 10 | temp = *a; 11 | *a = *b; 12 | *b = temp; 13 | } 14 | 15 | int main() { 16 | 17 | int c; 18 | int (*p)(int, int); // Pointer to function that should take 19 | //(int,int) as argument/parameter and return int 20 | 21 | p = &Add; 22 | c = (*p)(2, 3); // dereferencing and executing the function 23 | printf("Sum = %d\n\n", c); 24 | 25 | int m = 25; 26 | int n = 100; 27 | printf("m is %d, n is %d\n", m, n); 28 | swap(&m, &n); 29 | printf("m is %d, n is %d\n", m, n); 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Pointers/Function_Pointers/Func_ptrs_callbacks/func_ptrs_callbacks.c: -------------------------------------------------------------------------------- 1 | // Function ptrs and callbacks 2 | 3 | // In this example we will use qsort() "That use function ptrs" to sort an array 4 | 5 | #include 6 | #include 7 | 8 | int cmp_function(const void *a, const void *b) { 9 | 10 | int A = *((int *)a); // First typecst to int pointer and then de-reference. 11 | int B = *((int *)b); 12 | 13 | return A - B; // A-B in case of lower to higher B-A in case of higher to 14 | // lower 15 | } 16 | 17 | int main() { 18 | 19 | int nums[] = {13, 26, -4, 6, 67, -20, 32}; 20 | int num_elements = sizeof(nums) / sizeof(int); 21 | 22 | for (int i = 0; i < num_elements; i++) { 23 | printf("%d ", nums[i]); 24 | } 25 | printf("\n\n"); 26 | 27 | qsort(nums, num_elements, sizeof(int), cmp_function); 28 | 29 | for (int i = 0; i < num_elements; i++) { 30 | printf("%d ", nums[i]); 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Pointers/Memory_Leak/main.c: -------------------------------------------------------------------------------- 1 | //Memory Leak 2 | 3 | //Simple Betting Game 4 | //"Jack Queen King" - computer shuffles these cards 5 | //player has to guess the potion of quee 6 | //if he wins, he takes 3*bet 7 | //if he looses he looses the bet amount 8 | //player has $100 initially 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | int cash = 100; 15 | void Play(int bet){ 16 | //char C[3]={'J','Q','K'}; 17 | char *C = (char*)malloc(3*sizeof(char)); 18 | C[0]='J';C[1]='Q';C[2]='K'; 19 | printf("Shuffling ......\n"); 20 | srand(time(NULL)); //seeding 21 | int i; 22 | for (i=0; i<5; i++) { 23 | int x = rand() % 3; //So it returns 0 1 or 2 24 | int y = rand() % 3; 25 | int temp = C[x]; 26 | C[x] = C[y]; 27 | C[y] = temp; //swaps characters at position x and y 28 | } 29 | int playerGuess; 30 | printf("What's the position of queen - 1,2 or 3?\n"); 31 | scanf("%d",&playerGuess); 32 | if (C[playerGuess-1] == 'Q') { 33 | cash += 3*bet; 34 | printf("You Win ! Result = %c %c %c Total Cash = %d\n",C[0],C[1],C[2],cash); 35 | 36 | }else { 37 | cash -=bet; 38 | printf("You Loose ! Result = %c %c %c Total Cash = %d\n",C[0],C[1],C[2],cash); 39 | 40 | } 41 | free(C); 42 | 43 | } 44 | 45 | 46 | 47 | int main() 48 | { 49 | int bet; 50 | printf("Welcome\n\n"); 51 | printf("Your initial cash %d\n",cash); 52 | while (cash>0) { 53 | printf("What's your bet? $"); 54 | scanf("%d",&bet); 55 | if (bet == 0 || bet > cash) break; 56 | Play(bet); 57 | printf("****************************************\n\n"); 58 | 59 | } 60 | 61 | 62 | return (0); 63 | } -------------------------------------------------------------------------------- /Pointers/Pointer_as_function_returns/ptr_function_return.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int* Add(int* a,int* b){ 5 | int *c= malloc(sizeof(int)); 6 | *c = (*a) + (*b); 7 | return c; 8 | } 9 | 10 | int main(){ 11 | int a=2,b=4; 12 | int* ptr = Add(&a, &b); 13 | printf("Sum = %d\n",*ptr); 14 | 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Pointers/Pointer_to_Pointer/ptr2ptr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | int x = 5; 6 | int* p; 7 | p= &x; 8 | *p = 6; 9 | int** q; 10 | q = &p; 11 | 12 | printf("%d",**q); 13 | 14 | 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Pointers/Pointers_and_functions/ptrs_and_functions.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // Pointers by value 6 | 7 | void function2(int *param) 8 | { 9 | printf("param's address %p\n", param); 10 | param = NULL; 11 | } 12 | 13 | // Pointer by reference 14 | 15 | void function4(int **par) 16 | { 17 | *par = NULL; 18 | } 19 | 20 | int main() 21 | { 22 | 23 | // Pointer by value 24 | 25 | int variable = 111; 26 | int *ptr = &variable; 27 | 28 | function2(ptr); 29 | printf("ptr's address %p\n", ptr); 30 | 31 | // Pointer by reference 32 | int *ptr2 = &variable; 33 | 34 | function4(&ptr2); 35 | printf("ptr's address %p\n", ptr2); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Pointers/Ptrs_and_multidimensional_arrays/ptrs_multi_arrays.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int c[3][2][2]={{{2,5},{7,9}}, 5 | {{3,4},{6,1}}, 6 | {{0,8},{11,13}}}; 7 | 8 | printf("%p %p %p %p\n",c,*c,c[0],&c[0][0]); 9 | printf("%d\n",*(c[0][0]+1)); 10 | 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Pointers/links_StackOverFlow.txt: -------------------------------------------------------------------------------- 1 | https://stackoverflow.com/questions/1398307/how-can-i-allocate-memory-and-return-it-via-a-pointer-parameter-to-the-calling 2 | 3 | https://stackoverflow.com/questions/897366/how-do-pointer-to-pointers-work-in-c-and-when-might-you-use-them 4 | 5 | https://stackoverflow.com/questions/22250067/how-to-get-address-of-a-pointer-in-c-c 6 | 7 | https://stackoverflow.com/questions/13431108/changing-address-contained-by-pointer-using-function 8 | -------------------------------------------------------------------------------- /Queues/Priority_Queue/Exer/dynamic_queue.c: -------------------------------------------------------------------------------- 1 | #include "dynamic_queue.h" 2 | #include 3 | 4 | bool isEmptyQueue(tQueue Q) { return (Q.front == QNULL); } 5 | 6 | 7 | void createEmptyQueue(tQueue *Q) { 8 | Q->front = //We don't use (*Q)->front because tQueue is a struct, not a pointer 9 | QNULL; 10 | Q->rear = QNULL; 11 | } 12 | 13 | bool createNode(tPosQ *p) { 14 | *p = malloc(sizeof(struct tNodeQ)); 15 | return *p != QNULL; 16 | } 17 | 18 | bool enqueue(tItemQ d, tQueue *Q) { 19 | tPosQ p; 20 | 21 | if (!createNode(&p)) { 22 | return false; 23 | } else { 24 | p->item = d; 25 | p->next = QNULL; 26 | 27 | if (isEmptyQueue(*Q)) { 28 | Q->front = p; 29 | 30 | } else { 31 | Q->rear->next = p; 32 | } 33 | 34 | Q->rear = p; 35 | 36 | return true; 37 | } 38 | } 39 | 40 | void dequeue(tQueue *Q) { 41 | tPosQ p; 42 | 43 | if (isEmptyQueue(*Q)) { // If isEmptyQueue return nothing 44 | return; 45 | } 46 | 47 | p = Q->front; 48 | Q->front = p->next; 49 | 50 | if (isEmptyQueue(*Q)) { 51 | Q->rear = QNULL; 52 | } 53 | 54 | free(p); 55 | } 56 | 57 | tItemQ front(tQueue Q) { return Q.front->item; } 58 | 59 | -------------------------------------------------------------------------------- /Queues/Priority_Queue/Exer/dynamic_queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | #define QNULL NULL 5 | 6 | typedef int tItemQ; 7 | 8 | typedef struct tNodeQ *tPosQ; 9 | 10 | struct tNodeQ{ 11 | tItemQ item; 12 | tPosQ next; 13 | }; 14 | 15 | typedef struct Queue{ 16 | tPosQ front; 17 | tPosQ rear; 18 | 19 | } tQueue; 20 | 21 | /* Function prototypes */ 22 | 23 | void createEmptyQueue(tQueue *Q); 24 | bool enqueue(tItemQ d, tQueue *Q); 25 | void dequeue(tQueue *Q); 26 | tItemQ front(tQueue Q); 27 | bool isEmptyQueue(tQueue Q); 28 | 29 | -------------------------------------------------------------------------------- /Queues/Priority_Queue/Exer/exer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "priority_queue.h" 3 | 4 | //Create a function that prints the priority of each node and the front element the embedded queue 5 | 6 | void print_headers(tPosL pos,tOrderedList queueP){ 7 | tPriority prio; 8 | tQueue embedded_queue; 9 | 10 | if(pos==LNULL){ 11 | return; 12 | } 13 | 14 | getItem(pos, queueP, &prio, &embedded_queue); 15 | 16 | printf("Priority: %d First: %d \n",prio,front(embedded_queue)); 17 | 18 | } 19 | 20 | 21 | void queue_tour(tQueueP queueP){ 22 | 23 | tPosL pos; 24 | 25 | if (isEmptyQueueP(queueP)) { 26 | printf("Empty\n"); 27 | }else { 28 | for (pos=queueP;pos!=LNULL;pos=pos->next){ 29 | print_headers(pos, queueP); 30 | } 31 | } 32 | } 33 | 34 | 35 | int main(){ 36 | 37 | tQueueP queueP; 38 | createEmptyQueueP(&queueP); 39 | if(isEmptyQueueP(queueP)){ printf("Empty\n");} 40 | 41 | 42 | 43 | enqueueP(911,1,&queueP); 44 | enqueueP(836,1,&queueP); 45 | queue_tour(queueP); 46 | dequeueP(&queueP); 47 | printf("Dequeue\n"); 48 | enqueueP(432,2,&queueP); 49 | enqueueP(536,2,&queueP); 50 | enqueueP(123,5,&queueP); 51 | enqueueP(456,5,&queueP); 52 | queue_tour(queueP); 53 | dequeueP(&queueP); 54 | printf("Dequeue\n"); 55 | queue_tour(queueP); 56 | dequeueP(&queueP); 57 | printf("Dequeue\n"); 58 | queue_tour(queueP); 59 | dequeueP(&queueP); 60 | printf("Dequeue\n"); 61 | queue_tour(queueP); 62 | dequeueP(&queueP); 63 | printf("Dequeue\n"); 64 | queue_tour(queueP); 65 | dequeueP(&queueP); 66 | printf("Dequeue\n"); 67 | queue_tour(queueP); 68 | 69 | 70 | 71 | return 0; 72 | } 73 | -------------------------------------------------------------------------------- /Queues/Priority_Queue/Exer/ordered_list.c: -------------------------------------------------------------------------------- 1 | #include "ordered_list.h" 2 | #include 3 | #include 4 | 5 | 6 | /* 7 | * Order Explanation: 8 | * 1 -> Inmediate 9 | * 2 -> Very Urgent 10 | * 3 -> Urgent 11 | * 4 -> Standard 12 | * 5 -> Non-Urgent 13 | */ 14 | 15 | 16 | bool isEmptyList(tOrderedList L) { return (L == LNULL); } 17 | 18 | void createEmptyList(tOrderedList *L) { *L = LNULL; } 19 | 20 | bool createNodeL(tPosL *p) { 21 | *p = malloc(sizeof(struct tNodeL)); 22 | return *p != LNULL; 23 | } 24 | 25 | tPosL findPosition(tOrderedList L, tItemL d) { 26 | 27 | tPosL p,tmp; 28 | 29 | p = L; 30 | tmp = L; 31 | while ((p != LNULL) && 32 | (p->data.prio < d.prio)) { //Continue while data is ordered 33 | tmp = p; 34 | p = p->next; 35 | } 36 | return tmp; 37 | } 38 | 39 | bool insertItem(tPriority prio, tOrderedList *L) { 40 | tPosL q, p; 41 | 42 | if (!createNodeL(&q) || prio > MAXPRIO) 43 | { 44 | return false; 45 | } else { 46 | 47 | q->data.prio = prio; 48 | createEmptyQueue(&q->data.queue); //Attention! Queue must be initialized 49 | 50 | q->next = LNULL; 51 | 52 | if (isEmptyList(*L)) 53 | { 54 | *L = q; 55 | 56 | } 57 | else if (prio < (*L)->data.prio) 58 | { // insert at the top of the list (first element) 59 | 60 | q->next = *L; 61 | *L = q; 62 | } 63 | else 64 | { //Find right position 65 | p = findPosition(*L, q->data); 66 | q->next = p->next; 67 | p->next = q; 68 | } 69 | return true; 70 | } 71 | } 72 | 73 | void updateItem(tOrderedList *L, tPosL p, tQueue queue) { 74 | p->data.queue = queue; 75 | } 76 | 77 | tPosL findItem(tPriority prio, tOrderedList L) { 78 | tPosL p; 79 | 80 | for (p = L; (p != LNULL) && (p->data.prio < prio); p = p->next) 81 | ; 82 | 83 | return p; 84 | } 85 | 86 | 87 | void getItem(tPosL p, tOrderedList L, tPriority *prio, tQueue *queue) { 88 | 89 | *prio = p->data.prio; 90 | *queue = p->data.queue; 91 | } 92 | 93 | 94 | 95 | tPosL first(tOrderedList L) { 96 | 97 | return L; // L always points to te first element of the list 98 | } 99 | 100 | tPosL last(tOrderedList L) { 101 | 102 | tPosL p; 103 | 104 | for (p = L; p->next != LNULL; p = p->next) 105 | ; 106 | return p; 107 | } 108 | 109 | tPosL previous(tPosL p, tOrderedList L) { 110 | tPosL q; 111 | 112 | if (p == L) { 113 | return LNULL; 114 | } else { 115 | for (q = L; q->next != p; q = q->next) 116 | ; 117 | return q; 118 | } 119 | } 120 | 121 | tPosL next(tPosL p, tOrderedList L) { return p->next; } 122 | 123 | void deleteAtPosition(tPosL p, tOrderedList *L) { 124 | tPosL q; 125 | 126 | if (p == *L) 127 | { // Delete first element 128 | *L = (*L)->next; 129 | } 130 | else if (p->next == LNULL) 131 | { //Delete last element 132 | for (q = *L; q->next->next != p; q = q->next) 133 | ; 134 | q->next = LNULL; 135 | 136 | } 137 | else { //Middle deletion Overwrite p with q 138 | //p--> element we want to delete 139 | //q-->next element 140 | 141 | q = p->next; 142 | p->data = q->data; 143 | p->next = q->next; 144 | p = q; 145 | } 146 | 147 | free(p); 148 | } 149 | 150 | void deleteList(tOrderedList *L) { 151 | tPosL p; 152 | 153 | while (!isEmptyList(*L)) { 154 | p = *L; 155 | *L = (*L)->next; 156 | free(p); 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /Queues/Priority_Queue/Exer/ordered_list.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dynamic_queue.h" 3 | 4 | /* Types definition */ 5 | 6 | #define LNULL NULL 7 | #define MAXPRIO 5 //Max priority level 8 | 9 | typedef int tPriority; 10 | 11 | typedef struct tItemL { 12 | tPriority prio; 13 | tQueue queue; 14 | } tItemL; 15 | 16 | typedef struct tNodeL* tPosL; //Ptr to tNode 17 | 18 | struct tNodeL { 19 | tItemL data; 20 | tPosL next; 21 | }; 22 | 23 | typedef tPosL tOrderedList; 24 | 25 | /* Function prototypes */ 26 | 27 | void createEmptyList(tOrderedList *L); 28 | bool insertItem(tPriority prio,tOrderedList *L); 29 | tPosL findPosition(tOrderedList L, tItemL d); 30 | void updateItem(tOrderedList* L,tPosL p,tQueue queue); 31 | tPosL findItem(tPriority prio,tOrderedList L); 32 | bool isEmptyList(tOrderedList L); 33 | void getItem(tPosL p, tOrderedList L,tPriority *prio,tQueue *queue); 34 | tPosL first(tOrderedList L); 35 | tPosL last(tOrderedList L); 36 | tPosL previous(tPosL p, tOrderedList L); 37 | tPosL next(tPosL p,tOrderedList L); 38 | void deleteAtPosition(tPosL p , tOrderedList *L); 39 | void deleteList(tOrderedList *L); 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Queues/Priority_Queue/Exer/priority_queue.c: -------------------------------------------------------------------------------- 1 | #include "priority_queue.h" 2 | 3 | 4 | bool isEmptyQueueP(tQueueP queueP){ 5 | return (queueP==LNULL); 6 | } 7 | 8 | void createEmptyQueueP(tQueueP *queueP){ 9 | *queueP=LNULL; 10 | } 11 | 12 | bool enqueueP(tItemQ item, tPriority prio, tQueueP *queueP) { 13 | /*PreCondition: There's memory enough for the operation */ 14 | 15 | tPosL pos; 16 | tQueue Q; 17 | 18 | //There is such a priority? 19 | pos = findItem(prio, *queueP); 20 | 21 | if (pos == LNULL) { 22 | // Doesn't exist?--> Add it 23 | if (!insertItem(prio, queueP)) { 24 | return false; 25 | } 26 | pos = findItem(prio, *queueP); 27 | } 28 | 29 | //Extract the queue to add the element 30 | 31 | getItem(pos, *queueP, &prio, &Q); 32 | 33 | // Update the queue 34 | if (!enqueue(item, &Q)) { 35 | return false; 36 | } 37 | //Update the list 38 | updateItem(queueP, pos, Q); 39 | return true; 40 | } 41 | 42 | void dequeueP(tQueueP *queueP) { 43 | /* Precondition: priority queue is not empty */ 44 | 45 | tPosL posL; 46 | tItemL itemL; 47 | tQueue queue; 48 | tPriority priority; 49 | 50 | //Extract the queue 51 | posL = first(*queueP); 52 | getItem(posL, *queueP, &priority, &queue); 53 | dequeue(&queue); 54 | //If the queue remains empty, delete the entire node. 55 | if (isEmptyQueue(queue)) { 56 | deleteAtPosition(posL, queueP); 57 | } else { 58 | updateItem(queueP, posL, queue); 59 | } 60 | } 61 | 62 | tItemQ frontP(tQueueP queueP){ //Precondition: QueueP not empty 63 | tPosL first_pos; 64 | tQueue queue; 65 | tPriority node_prio; 66 | 67 | 68 | first_pos=first(queueP); 69 | getItem(first_pos, queueP, &node_prio, &queue); 70 | return queue.front->item; 71 | 72 | } 73 | -------------------------------------------------------------------------------- /Queues/Priority_Queue/Exer/priority_queue.h: -------------------------------------------------------------------------------- 1 | #include "ordered_list.h" 2 | #include 3 | 4 | 5 | #define LNULL NULL 6 | 7 | typedef tOrderedList tQueueP; 8 | 9 | void createEmptyQueueP(tQueueP *queueP); 10 | bool enqueueP(tItemQ item,tPriority prio, tQueueP *queueP); 11 | void dequeueP(tQueueP *queueP); 12 | bool isEmptyQueueP(tQueueP queueP); 13 | tItemQ frontP(tQueueP queueP); -------------------------------------------------------------------------------- /Queues/Priority_Queue/dynamic_queue.c: -------------------------------------------------------------------------------- 1 | #include "dynamic_queue.h" 2 | #include 3 | 4 | bool isEmptyQueue(tQueue Q) { return (Q.front == QNULL); } 5 | 6 | 7 | void createEmptyQueue(tQueue *Q) { 8 | Q->front = //We don't use (*Q)->front because tQueue is a struct, not a pointer 9 | QNULL; 10 | Q->rear = QNULL; 11 | } 12 | 13 | bool createNode(tPosQ *p) { 14 | *p = malloc(sizeof(struct tNodeQ)); 15 | return *p != QNULL; 16 | } 17 | 18 | bool enqueue(tItemQ d, tQueue *Q) { 19 | tPosQ p; 20 | 21 | if (!createNode(&p)) { 22 | return false; 23 | } else { 24 | p->item = d; 25 | p->next = QNULL; 26 | 27 | if (isEmptyQueue(*Q)) { 28 | Q->front = p; 29 | 30 | } else { 31 | Q->rear->next = p; 32 | } 33 | 34 | Q->rear = p; 35 | 36 | return true; 37 | } 38 | } 39 | 40 | void dequeue(tQueue *Q) { 41 | tPosQ p; 42 | 43 | if (isEmptyQueue(*Q)) { // If isEmptyQueue return nothing 44 | return; 45 | } 46 | 47 | p = Q->front; 48 | Q->front = p->next; 49 | 50 | if (isEmptyQueue(*Q)) { 51 | Q->rear = QNULL; 52 | } 53 | 54 | free(p); 55 | } 56 | 57 | tItemQ front(tQueue Q) { return Q.front->item; } 58 | 59 | -------------------------------------------------------------------------------- /Queues/Priority_Queue/dynamic_queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | #define QNULL NULL 5 | 6 | typedef int tItemQ; 7 | 8 | typedef struct tNodeQ *tPosQ; 9 | 10 | struct tNodeQ{ 11 | tItemQ item; 12 | tPosQ next; 13 | }; 14 | 15 | typedef struct Queue{ 16 | tPosQ front; 17 | tPosQ rear; 18 | 19 | } tQueue; 20 | 21 | /* Function prototypes */ 22 | 23 | void createEmptyQueue(tQueue *Q); 24 | bool enqueue(tItemQ d, tQueue *Q); 25 | void dequeue(tQueue *Q); 26 | tItemQ front(tQueue Q); 27 | bool isEmptyQueue(tQueue Q); 28 | 29 | -------------------------------------------------------------------------------- /Queues/Priority_Queue/ordered_list.c: -------------------------------------------------------------------------------- 1 | #include "ordered_list.h" 2 | #include 3 | #include 4 | 5 | 6 | /* 7 | * Order Explanation: 8 | * 1 -> Inmediate 9 | * 2 -> Very Urgent 10 | * 3 -> Urgent 11 | * 4 -> Standard 12 | * 5 -> Non-Urgent 13 | */ 14 | 15 | 16 | bool isEmptyList(tOrderedList L) { return (L == LNULL); } 17 | 18 | void createEmptyList(tOrderedList *L) { *L = LNULL; } 19 | 20 | bool createNodeL(tPosL *p) { 21 | *p = malloc(sizeof(struct tNodeL)); 22 | return *p != LNULL; 23 | } 24 | 25 | tPosL findPosition(tOrderedList L, tItemL d) { 26 | 27 | tPosL p,tmp; 28 | 29 | p = L; 30 | tmp = L; 31 | while ((p != LNULL) && 32 | (p->data.prio < d.prio)) { //Continue while data is ordered 33 | tmp = p; 34 | p = p->next; 35 | } 36 | return tmp; 37 | } 38 | 39 | bool insertItem(tPriority prio, tOrderedList *L) { 40 | tPosL q, p; 41 | 42 | if (!createNodeL(&q) || prio > MAXPRIO) 43 | { 44 | return false; 45 | } else { 46 | 47 | q->data.prio = prio; 48 | createEmptyQueue(&q->data.queue); //Attention! Queue must be initialized 49 | q->next = LNULL; 50 | 51 | if (isEmptyList(*L)) 52 | { 53 | *L = q; 54 | 55 | } 56 | else if (prio < (*L)->data.prio) 57 | { // insert at the top of the list (first element) 58 | 59 | q->next = *L; 60 | *L = q; 61 | } 62 | else 63 | { //Find right position 64 | p = findPosition(*L, q->data); 65 | q->next = p->next; 66 | p->next = q; 67 | } 68 | return true; 69 | } 70 | } 71 | 72 | void updateItem(tOrderedList *L, tPosL p, tQueue queue) { 73 | p->data.queue = queue; 74 | } 75 | 76 | tPosL findItem(tPriority prio, tOrderedList L) { 77 | tPosL p; 78 | 79 | for (p = L; (p != LNULL) && (p->data.prio < prio); p = p->next) 80 | ; 81 | 82 | return p; 83 | } 84 | 85 | 86 | void getItem(tPosL p, tOrderedList L, tPriority *prio, tQueue *queue) { 87 | 88 | *prio = p->data.prio; 89 | *queue = p->data.queue; 90 | } 91 | 92 | 93 | 94 | tPosL first(tOrderedList L) { 95 | 96 | return L; // L always points to te first element of the list 97 | } 98 | 99 | tPosL last(tOrderedList L) { 100 | 101 | tPosL p; 102 | 103 | for (p = L; p->next != LNULL; p = p->next) 104 | ; 105 | return p; 106 | } 107 | 108 | tPosL previous(tPosL p, tOrderedList L) { 109 | tPosL q; 110 | 111 | if (p == L) { 112 | return LNULL; 113 | } else { 114 | for (q = L; q->next != p; q = q->next) 115 | ; 116 | return q; 117 | } 118 | } 119 | 120 | tPosL next(tPosL p, tOrderedList L) { return p->next; } 121 | 122 | void deleteAtPosition(tPosL p, tOrderedList *L) { 123 | tPosL q; 124 | 125 | if (p == *L) 126 | { // Delete first element 127 | *L = (*L)->next; 128 | } 129 | else if (p->next == LNULL) 130 | { //Delete last element 131 | for (q = *L; q->next->next != p; q = q->next) 132 | ; 133 | q->next = LNULL; 134 | 135 | } 136 | else { //Middle deletion Overwrite p with q 137 | //p--> element we want to delete 138 | //q-->next element 139 | 140 | q = p->next; 141 | p->data = q->data; 142 | p->next = q->next; 143 | p = q; 144 | } 145 | 146 | free(p); 147 | } 148 | 149 | void deleteList(tOrderedList *L) { 150 | tPosL p; 151 | 152 | while (!isEmptyList(*L)) { 153 | p = *L; 154 | *L = (*L)->next; 155 | free(p); 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /Queues/Priority_Queue/ordered_list.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "dynamic_queue.h" 3 | 4 | /* Types definition */ 5 | 6 | #define LNULL NULL 7 | #define MAXPRIO 5 //Max priority level 8 | 9 | typedef int tPriority; 10 | 11 | typedef struct tItemL { 12 | tPriority prio; 13 | tQueue queue; 14 | } tItemL; 15 | 16 | typedef struct tNodeL* tPosL; //Ptr to tNode 17 | 18 | struct tNodeL { 19 | tItemL data; 20 | tPosL next; 21 | }; 22 | 23 | typedef tPosL tOrderedList; 24 | 25 | /* Function prototypes */ 26 | 27 | void createEmptyList(tOrderedList *L); 28 | bool insertItem(tPriority prio,tOrderedList *L); 29 | tPosL findPosition(tOrderedList L, tItemL d); 30 | void updateItem(tOrderedList* L,tPosL p,tQueue queue); 31 | tPosL findItem(tPriority prio,tOrderedList L); 32 | bool isEmptyList(tOrderedList L); 33 | void getItem(tPosL p, tOrderedList L,tPriority *prio,tQueue *queue); 34 | tPosL first(tOrderedList L); 35 | tPosL last(tOrderedList L); 36 | tPosL previous(tPosL p, tOrderedList L); 37 | tPosL next(tPosL p,tOrderedList L); 38 | void deleteAtPosition(tPosL p , tOrderedList *L); 39 | void deleteList(tOrderedList *L); 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Queues/Priority_Queue/priority_queue.c: -------------------------------------------------------------------------------- 1 | #include "priority_queue.h" 2 | 3 | 4 | bool isEmptyQueueP(tQueueP queueP){ 5 | return (queueP==LNULL); 6 | } 7 | 8 | void createEmptyQueueP(tQueueP *queueP){ 9 | *queueP=LNULL; 10 | } 11 | 12 | bool enqueueP(tItemQ item, tPriority prio, tQueueP *queueP) { 13 | /*PreCondition: There's memory enough for the operation */ 14 | 15 | tPosL pos; 16 | tQueue Q; 17 | 18 | //There is such a priority? 19 | pos = findItem(prio, *queueP); 20 | 21 | if (pos == LNULL) { 22 | // Doesn't exist?--> Add it 23 | if (!insertItem(prio, queueP)) { 24 | return false; 25 | } 26 | pos = findItem(prio, *queueP); 27 | } 28 | 29 | //Extract the queue to add the element 30 | 31 | getItem(pos, *queueP, &prio, &Q); 32 | 33 | // Update the queue 34 | if (!enqueue(item, &Q)) { 35 | return false; 36 | } 37 | //Update the list 38 | updateItem(queueP, pos, Q); 39 | return true; 40 | } 41 | 42 | void dequeueP(tQueueP *queueP) { 43 | /* Precondition: priority queue is not empty */ 44 | 45 | tPosL posL; 46 | tItemL itemL; 47 | tQueue queue; 48 | tPriority priority; 49 | 50 | //Extract the queue 51 | posL = first(*queueP); 52 | getItem(posL, *queueP, &priority, &queue); 53 | dequeue(&queue); 54 | //If the queue remains empty, delete the entire node. 55 | if (isEmptyQueue(queue)) { 56 | deleteAtPosition(posL, queueP); 57 | } else { 58 | updateItem(queueP, posL, queue); 59 | } 60 | } 61 | 62 | tItemQ frontP(tQueueP queueP){ //Precondition: QueueP not empty 63 | tPosL first_pos; 64 | tQueue queue; 65 | tPriority node_prio; 66 | 67 | 68 | first_pos=first(queueP); 69 | getItem(first_pos, queueP, &node_prio, &queue); 70 | return queue.front->item; 71 | 72 | } 73 | -------------------------------------------------------------------------------- /Queues/Priority_Queue/priority_queue.h: -------------------------------------------------------------------------------- 1 | #include "ordered_list.h" 2 | #include 3 | 4 | 5 | #define LNULL NULL 6 | 7 | typedef tOrderedList tQueueP; 8 | 9 | void createEmptyQueueP(tQueueP *queueP); 10 | bool enqueueP(tItemQ item,tPriority prio, tQueueP *queueP); 11 | void dequeueP(tQueueP *queueP); 12 | bool isEmptyQueueP(tQueueP queueP); 13 | tItemQ frontP(tQueueP queueP); -------------------------------------------------------------------------------- /Queues/Queue/Exer/circular_queue.c: -------------------------------------------------------------------------------- 1 | #include "circular_queue.h" 2 | #include 3 | 4 | void createEmptyQueue(tQueue *Q) { 5 | Q->front = 0; 6 | Q->rear = Q_MAX - 1; 7 | } 8 | int addOne(int i) { // Advance one position. 9 | if (i == Q_MAX - 1) { // If there's no more space we can "reset" 10 | return 0; 11 | } else { 12 | return ++i; 13 | } 14 | } 15 | 16 | bool enqueue(tItemQ d, tQueue *Q) { 17 | int one_fordward = addOne(Q->rear); // Doing this we only call addOne 2 times. 18 | if (Q->front == addOne(one_fordward)) { //If there's no more space 19 | return false; 20 | } else { 21 | Q->rear = one_fordward; 22 | Q->data[Q->rear] = d; 23 | return true; 24 | } 25 | } 26 | 27 | void dequeue(tQueue *Q) { 28 | Q->front = addOne(Q->front); 29 | } 30 | 31 | tItemQ front(tQueue Q) { 32 | return Q.data[Q.front]; 33 | } 34 | 35 | bool isEmptyQueue(tQueue Q) { 36 | return (Q.front == addOne(Q.rear)); //Is empty when rear is right behind front 37 | } 38 | -------------------------------------------------------------------------------- /Queues/Queue/Exer/circular_queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | 5 | #define Q_MAX 10 6 | typedef int tItemQ; 7 | 8 | typedef struct Queue{ 9 | tItemQ data[Q_MAX]; 10 | int front, rear; 11 | } tQueue; 12 | 13 | /* Function prototypes */ 14 | 15 | void createEmptyQueue(tQueue *Q); 16 | bool enqueue(tItemQ d, tQueue *Q); 17 | void dequeue(tQueue *Q); 18 | tItemQ front(tQueue Q); 19 | bool isEmptyQueue(tQueue Q); 20 | 21 | 22 | -------------------------------------------------------------------------------- /Queues/Queue/Exer/dynamic_queue.c: -------------------------------------------------------------------------------- 1 | #include "dynamic_queue.h" 2 | #include 3 | 4 | bool isEmptyQueue(tQueue Q) { return (Q.front == QNULL); } 5 | 6 | 7 | void createEmptyQueue(tQueue *Q) { 8 | Q->front = //We don't use (*Q)->front because tQueue is a struct, not a pointer 9 | QNULL; 10 | Q->rear = QNULL; 11 | } 12 | 13 | bool createNode(tPosQ *p) { 14 | *p = malloc(sizeof(struct tNodeQ)); 15 | return *p != QNULL; 16 | } 17 | 18 | bool enqueue(tItemQ d, tQueue *Q) { 19 | tPosQ p; 20 | 21 | if (!createNode(&p)) { 22 | return false; 23 | } else { 24 | p->item = d; 25 | p->next = QNULL; 26 | 27 | if (isEmptyQueue(*Q)) { 28 | Q->front = p; 29 | 30 | } else { 31 | Q->rear->next = p; 32 | } 33 | 34 | Q->rear = p; 35 | 36 | return true; 37 | } 38 | } 39 | 40 | void dequeue(tQueue *Q) { 41 | tPosQ p; 42 | 43 | if (isEmptyQueue(*Q)) { // If isEmptyQueue return nothing 44 | return; 45 | } 46 | 47 | p = Q->front; 48 | Q->front = p->next; 49 | 50 | if (isEmptyQueue(*Q)) { 51 | Q->rear = QNULL; 52 | } 53 | 54 | free(p); 55 | } 56 | 57 | tItemQ front(tQueue Q) { return Q.front->item; } 58 | 59 | -------------------------------------------------------------------------------- /Queues/Queue/Exer/dynamic_queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | #define QNULL NULL 5 | 6 | typedef int tItemQ; 7 | 8 | typedef struct tNodeQ *tPosQ; 9 | 10 | struct tNodeQ{ 11 | tItemQ item; 12 | tPosQ next; 13 | }; 14 | 15 | typedef struct Queue{ 16 | tPosQ front; 17 | tPosQ rear; 18 | 19 | } tQueue; 20 | 21 | /* Function prototypes */ 22 | 23 | void createEmptyQueue(tQueue *Q); 24 | bool enqueue(tItemQ d, tQueue *Q); 25 | void dequeue(tQueue *Q); 26 | tItemQ front(tQueue Q); 27 | bool isEmptyQueue(tQueue Q); 28 | 29 | -------------------------------------------------------------------------------- /Queues/Queue/Exer/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "circular_queue.h" 3 | 4 | 5 | void print_front(tQueue Q){ 6 | if(!isEmptyQueue(Q)){ 7 | printf(" %d \n",front(Q)); 8 | }else{ 9 | printf("Empty!\n"); 10 | } 11 | } 12 | 13 | int main(){ 14 | 15 | tQueue Queue; 16 | createEmptyQueue(&Queue); 17 | enqueue(1,&Queue); 18 | if(!isEmptyQueue(Queue)){ dequeue(&Queue); } 19 | enqueue(3,&Queue); 20 | print_front(Queue); 21 | if(!isEmptyQueue(Queue)){ dequeue(&Queue); } 22 | print_front(Queue); 23 | enqueue(15,&Queue); 24 | enqueue(20,&Queue); 25 | if(!isEmptyQueue(Queue)){ dequeue(&Queue); } 26 | print_front(Queue); 27 | if(!isEmptyQueue(Queue)){ dequeue(&Queue); } 28 | print_front(Queue); 29 | if(!isEmptyQueue(Queue)){ dequeue(&Queue); } 30 | print_front(Queue); //Empty 31 | if(!isEmptyQueue(Queue)){ dequeue(&Queue); } 32 | print_front(Queue); //Empty 33 | 34 | printf("Check\n"); 35 | 36 | 37 | //Check if full 38 | if(!enqueue(1,&Queue)){ 39 | printf("Full!!!!\n"); 40 | }else{ 41 | printf("No problem\n"); 42 | } 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Queues/Queue/circular_queue.c: -------------------------------------------------------------------------------- 1 | #include "circular_queue.h" 2 | #include 3 | 4 | void createEmptyQueue(tQueue *Q) { 5 | Q->front = 0; 6 | Q->rear = Q_MAX - 1; 7 | } 8 | int addOne(int i) { // Advance one position. 9 | if (i == Q_MAX - 1) { // If there's no more space we can "reset" 10 | return 0; 11 | } else { 12 | return ++i; 13 | } 14 | } 15 | 16 | bool enqueue(tItemQ d, tQueue *Q) { 17 | int one_fordward = addOne(Q->rear); // Doing this we only call addOne 2 times. 18 | if (Q->front == addOne(one_fordward)) { //If there's no more space 19 | return false; 20 | } else { 21 | Q->rear = one_fordward; 22 | Q->data[Q->rear] = d; 23 | return true; 24 | } 25 | } 26 | 27 | void dequeue(tQueue *Q) { 28 | Q->front = addOne(Q->front); 29 | } 30 | 31 | tItemQ front(tQueue Q) { 32 | return Q.data[Q.front]; 33 | } 34 | 35 | bool isEmptyQueue(tQueue Q) { 36 | return (Q.front == addOne(Q.rear)); //Is empty when rear is right behind front 37 | } 38 | -------------------------------------------------------------------------------- /Queues/Queue/circular_queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | 5 | #define Q_MAX 10 6 | typedef int tItemQ; 7 | 8 | typedef struct Queue{ 9 | tItemQ data[Q_MAX]; 10 | int front, rear; 11 | } tQueue; 12 | 13 | /* Function prototypes */ 14 | 15 | void createEmptyQueue(tQueue *Q); 16 | bool enqueue(tItemQ d, tQueue *Q); 17 | void dequeue(tQueue *Q); 18 | tItemQ front(tQueue Q); 19 | bool isEmptyQueue(tQueue Q); 20 | 21 | 22 | -------------------------------------------------------------------------------- /Queues/Queue/dynamic_queue.c: -------------------------------------------------------------------------------- 1 | #include "dynamic_queue.h" 2 | #include 3 | 4 | bool isEmptyQueue(tQueue Q) { return (Q.front == QNULL); } 5 | 6 | 7 | void createEmptyQueue(tQueue *Q) { 8 | Q->front = //We don't use (*Q)->front because tQueue is a struct, not a pointer 9 | QNULL; 10 | Q->rear = QNULL; 11 | } 12 | 13 | bool createNode(tPosQ *p) { 14 | *p = malloc(sizeof(struct tNodeQ)); 15 | return *p != QNULL; 16 | } 17 | 18 | bool enqueue(tItemQ d, tQueue *Q) { 19 | tPosQ p; 20 | 21 | if (!createNode(&p)) { 22 | return false; 23 | } else { 24 | p->item = d; 25 | p->next = QNULL; 26 | 27 | if (isEmptyQueue(*Q)) { 28 | Q->front = p; 29 | 30 | } else { 31 | Q->rear->next = p; 32 | } 33 | 34 | Q->rear = p; 35 | 36 | return true; 37 | } 38 | } 39 | 40 | void dequeue(tQueue *Q) { 41 | tPosQ p; 42 | 43 | if (isEmptyQueue(*Q)) { // If isEmptyQueue return nothing 44 | return; 45 | } 46 | 47 | p = Q->front; 48 | Q->front = p->next; //Move front one position forward 49 | 50 | if (isEmptyQueue(*Q)) { //After moving, check again if the queue is empty 51 | Q->rear = QNULL; 52 | } 53 | 54 | free(p); 55 | } 56 | 57 | tItemQ front(tQueue Q) { return Q.front->item; } 58 | 59 | -------------------------------------------------------------------------------- /Queues/Queue/dynamic_queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | #define QNULL NULL 5 | 6 | typedef int tItemQ; 7 | 8 | typedef struct tNodeQ *tPosQ; 9 | 10 | struct tNodeQ{ 11 | tItemQ item; 12 | tPosQ next; 13 | }; 14 | 15 | typedef struct Queue{ 16 | tPosQ front; 17 | tPosQ rear; 18 | 19 | } tQueue; 20 | 21 | /* Function prototypes */ 22 | 23 | void createEmptyQueue(tQueue *Q); 24 | bool enqueue(tItemQ d, tQueue *Q); 25 | void dequeue(tQueue *Q); 26 | tItemQ front(tQueue Q); 27 | bool isEmptyQueue(tQueue Q); 28 | 29 | -------------------------------------------------------------------------------- /Queues/Queue_Circular_Linked_List/Exer/circular_list_queue.c: -------------------------------------------------------------------------------- 1 | #include "circular_list_queue.h" 2 | #include 3 | 4 | void createEmptyQueue(tQueue *Q) { *Q = QNULL; } 5 | 6 | bool isEmptyQueue(tQueue Q) { return (Q == QNULL); } 7 | 8 | bool createNode(tPosQ *p) { 9 | *p = malloc(sizeof(struct tNode)); 10 | return *p != QNULL; 11 | } 12 | 13 | bool enqueue(tItemQ data, tQueue *Q) { 14 | tPosQ new; 15 | 16 | if (!createNode(&new)) { 17 | return false; 18 | } else { 19 | new->data = data; 20 | new->next = QNULL; 21 | 22 | if (isEmptyQueue(*Q)) { //if the queue is empty the node to be inserted points to itself 23 | new->next = new; 24 | } else { 25 | new->next = (*Q)->next; //1st --> new element points to front 26 | (*Q)->next = new; //2nd --> last element points to new 27 | } 28 | 29 | *Q = new; // 3rd --> new element become last element 30 | 31 | return true; 32 | } 33 | } 34 | 35 | void dequeue(tQueue *Q) { 36 | tPosQ del; 37 | 38 | if ((*Q)->next == *Q) { // If there's only one element 39 | del = *Q; 40 | *Q = QNULL; 41 | } else { 42 | del = (*Q)->next; 43 | (*Q)->next = del->next; 44 | 45 | } 46 | 47 | free(del); 48 | 49 | } 50 | 51 | tItemQ front(tQueue Q) { return Q->next->data; } 52 | -------------------------------------------------------------------------------- /Queues/Queue_Circular_Linked_List/Exer/circular_list_queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define QNULL NULL 4 | 5 | typedef int tItemQ; 6 | 7 | typedef struct tNode *tPosQ; // Ptr to tNode 8 | 9 | struct tNode { 10 | 11 | tItemQ data; 12 | tPosQ next; 13 | }; 14 | 15 | typedef tPosQ tQueue; 16 | 17 | void createEmptyQueue(tQueue *Q); 18 | bool enqueue(tItemQ data, tQueue *Q); 19 | void dequeue(tQueue *Q); 20 | tItemQ front(tQueue Q); 21 | bool isEmptyQueue(tQueue Q); -------------------------------------------------------------------------------- /Queues/Queue_Circular_Linked_List/Exer/exer.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "circular_list_queue.h" 3 | 4 | void print_front(tQueue Q){ 5 | printf(" %d ",front(Q)); 6 | } 7 | 8 | 9 | int main(){ 10 | 11 | tQueue Queue; 12 | createEmptyQueue(&Queue); 13 | enqueue(1,&Queue); 14 | enqueue(3,&Queue); 15 | print_front(Queue); 16 | printf("\n ------------------------------------\n"); 17 | dequeue(&Queue); 18 | print_front(Queue); 19 | printf("\n ------------------------------------\n"); 20 | enqueue(15,&Queue); 21 | enqueue(20,&Queue); 22 | print_front(Queue); 23 | printf("\n ------------------------------------\n"); 24 | dequeue(&Queue); 25 | //dequeue(&Queue); 26 | //dequeue(&Queue); 27 | print_front(Queue); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Queues/Queue_Circular_Linked_List/circular_list_queue.c: -------------------------------------------------------------------------------- 1 | #include "circular_list_queue.h" 2 | #include 3 | 4 | void createEmptyQueue(tQueue *Q) { *Q = QNULL; } 5 | 6 | bool isEmptyQueue(tQueue Q) { return (Q == QNULL); } 7 | 8 | bool createNode(tPosQ *p) { 9 | *p = malloc(sizeof(struct tNode)); 10 | return *p != QNULL; 11 | } 12 | 13 | bool enqueue(tItemQ data, tQueue *Q) { 14 | tPosQ new; 15 | 16 | if (!createNode(&new)) { 17 | return false; 18 | } else { 19 | new->data = data; 20 | new->next = QNULL; 21 | 22 | if (isEmptyQueue(*Q)) { //if the queue is empty the node to be inserted points to itself 23 | new->next = new; 24 | } else { 25 | new->next = (*Q)->next; //1st --> new element points to front 26 | (*Q)->next = new; //2nd --> last element points to new 27 | } 28 | 29 | *Q = new; // 3rd --> new element become last element 30 | 31 | return true; 32 | } 33 | } 34 | 35 | void dequeue(tQueue *Q) { 36 | tPosQ del; 37 | 38 | if ((*Q)->next == *Q) { // If there's only one element 39 | del = *Q; 40 | *Q = QNULL; 41 | } else { 42 | del = (*Q)->next; 43 | (*Q)->next = del->next; 44 | 45 | } 46 | 47 | free(del); 48 | 49 | } 50 | 51 | tItemQ front(tQueue Q) { return Q->next->data; } 52 | -------------------------------------------------------------------------------- /Queues/Queue_Circular_Linked_List/circular_list_queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define QNULL NULL 4 | 5 | typedef int tItemQ; 6 | 7 | typedef struct tNode *tPosQ; // Ptr to tNode 8 | 9 | struct tNode { 10 | 11 | tItemQ data; 12 | tPosQ next; 13 | }; 14 | 15 | typedef tPosQ tQueue; 16 | 17 | void createEmptyQueue(tQueue *Q); 18 | bool enqueue(tItemQ data, tQueue *Q); 19 | void dequeue(tQueue *Q); 20 | tItemQ front(tQueue Q); 21 | bool isEmptyQueue(tQueue Q); -------------------------------------------------------------------------------- /Queues/Queue_Linked_List/Implemenation_1/linked_list.c: -------------------------------------------------------------------------------- 1 | #include "linked_list.h" 2 | #include 3 | 4 | bool isEmptyList(tList L) { return (L == NULL); } 5 | 6 | void createEmptyList(tList *L) { *L = LNULL; } 7 | 8 | bool createNode(tPosL *p) { 9 | *p = malloc(sizeof(struct tNode)); 10 | return *p != NULL; 11 | } 12 | 13 | bool insertItem(tItemL d, tPosL p, tList *L) { 14 | tPosL q, r; //q--> element we want to add 15 | //r-->element prior to q 16 | if (!createNode(&q)) { 17 | return false; 18 | } else { 19 | q->data = d; 20 | q->next = LNULL; 21 | if (isEmptyList(*L)) { 22 | *L = q; 23 | } else if (p == LNULL) { //If given position is NULL, add the element at the end of list 24 | for (r = *L; r->next != LNULL; r = r->next) 25 | ; //We move to the end of the list 26 | r->next = q; 27 | 28 | } else if (p == *L) { // insert at the top of the list (first element) 29 | q->next = p; 30 | *L = q; 31 | 32 | } else { ///insert in intermediate position 33 | 34 | q->data = p->data; // data exchange 35 | p->data = d; 36 | q->next = p->next; 37 | p->next = q; 38 | } 39 | return true; 40 | } 41 | } 42 | 43 | void updateItem(tItemL d, tPosL p, tList *L) { 44 | p->data = d; 45 | } 46 | 47 | tPosL findItem(tItemL d, tList L) { 48 | tPosL p; 49 | 50 | for (p = L; (p != NULL) && (p->data != d); p = p->next) 51 | ; 52 | return p; 53 | } 54 | 55 | 56 | tItemL getItem(tPosL p, tList L) { return p->data; } 57 | 58 | 59 | tPosL first(tList L) { 60 | 61 | return L; // L always points to te first element of the list 62 | } 63 | 64 | tPosL last(tList L) { 65 | 66 | tPosL p; 67 | 68 | for (p = L; p->next != LNULL; p = p->next) 69 | ; 70 | return p; 71 | } 72 | 73 | tPosL previous(tPosL p, tList L) { 74 | tPosL q; 75 | 76 | if (p == L) { 77 | return LNULL; 78 | } else { 79 | for (q = L; q->next != p; q = q->next) 80 | ; 81 | return q; 82 | } 83 | } 84 | 85 | tPosL next(tPosL p, tList L) { return p->next; } 86 | 87 | void deleteAtPosition(tPosL p, tList *L) { 88 | tPosL q; 89 | 90 | if (p == *L) { // Delete first element 91 | *L = (*L)->next; 92 | } else if (p->next == LNULL) { //Delete last element 93 | for (q = *L; q->next != LNULL; q = q->next) 94 | ; 95 | q->next = LNULL; 96 | 97 | } else { //Middle deletion Overwrite p with q 98 | //p--> element we want to delete 99 | //q-->next element 100 | 101 | q = p->next; 102 | p->data = q->data; 103 | p->next = q->next; 104 | p = q; 105 | } 106 | 107 | free(p); 108 | } 109 | 110 | void deleteList(tList *L) { 111 | tPosL p; 112 | 113 | while (!isEmptyList(*L)) { 114 | p = *L; 115 | *L = (*L)->next; 116 | free(p); 117 | } 118 | } 119 | 120 | bool copyList(tList L, tList *M) { 121 | 122 | tPosL p, q, r; 123 | bool result = true; 124 | 125 | createEmptyList(M); 126 | if (!isEmptyList(L)) { 127 | p = L; //Point p to the top of the list 128 | while ((p != LNULL) && 129 | (createNode(&r))) { // createNode() check if there's space enough to continue 130 | r->data = p->data; 131 | r->next = LNULL; 132 | if (p == L) { //If we are copying the first element of the list 133 | *M = r; 134 | q = r; 135 | } else { 136 | q->next = r; 137 | q = r; 138 | } 139 | p = p->next; //Advance one place in original list 140 | } 141 | if (p != LNULL) // If there's no more availible space 142 | { 143 | result = false; 144 | deleteList(M); 145 | } 146 | } 147 | return result; 148 | } 149 | -------------------------------------------------------------------------------- /Queues/Queue_Linked_List/Implemenation_1/linked_list.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | 5 | #define LNULL NULL 6 | 7 | typedef int tItemL; 8 | 9 | typedef struct tNode* tPosL; //Ptr to tNode 10 | 11 | struct tNode { 12 | 13 | tItemL data; 14 | tPosL next; 15 | 16 | }; 17 | 18 | typedef tPosL tList; 19 | 20 | /* Function prototypes */ 21 | 22 | void createEmptyList(tList *L); 23 | bool createNode(tPosL* p); 24 | bool insertItem(tItemL d, tPosL p, tList *L); 25 | void updateItem(tItemL d, tPosL p, tList* L); 26 | tPosL findItem(tItemL d,tList L); 27 | bool isEmptyList(tList L); 28 | tItemL getItem(tPosL p, tList L); 29 | tPosL first(tList L); 30 | tPosL last(tList L); 31 | tPosL previous(tPosL p, tList L); 32 | tPosL next(tPosL p,tList L); 33 | void deleteAtPosition(tPosL p , tList *L); 34 | void deleteList(tList *L); 35 | 36 | -------------------------------------------------------------------------------- /Queues/Queue_Linked_List/Implemenation_1/list_queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "queue.h" 3 | 4 | void createEmptyQueue(tQueue *Q) { 5 | createEmptyList(Q); 6 | } 7 | bool enqueue(tItemQ d, tQueue *Q) { 8 | return insertItem(d, QNULL, Q); //Always insert at the end 9 | } 10 | bool isEmptyQueue(tQueue Q) { 11 | return isEmptyList(Q); 12 | } 13 | 14 | tItemQ front(tQueue Q) { 15 | return getItem(first(Q), Q); 16 | } 17 | void dequeue(tQueue *Q) { 18 | deleteAtPosition(first(*Q), Q); //Always delete from the front 19 | } 20 | -------------------------------------------------------------------------------- /Queues/Queue_Linked_List/Implemenation_1/queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include "linked_list.h" 3 | 4 | #define QNULL LNULL 5 | 6 | typedef tList tQueue; 7 | typedef tItemL tItemQ; 8 | typedef tPosL front_pos; 9 | typedef tPosL rear_pos; 10 | 11 | void createEmptyQueue(tQueue *Q); 12 | bool enqueue(tItemQ d, tQueue *Q); 13 | void dequeue(tQueue *Q); 14 | tItemQ front(tQueue Q); 15 | bool isEmptyQueue(tQueue Q); -------------------------------------------------------------------------------- /Queues/Queue_Linked_List/Tutorial_Implementation/Exers/queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "queue.h" 4 | 5 | bool isEmptyQueue(tQueue Q) { 6 | return (Q->rear == QNULL); //Alternative--> (Q.front == QNULL) 7 | } 8 | 9 | bool createNode(tPosQ* p) { 10 | *p = malloc(sizeof(struct tNodeQ)); 11 | return *p!= QNULL; 12 | } 13 | 14 | void createEmptyQueue(tQueue* Q) { 15 | 16 | *Q = malloc(sizeof(tQueue)); 17 | (*Q)->front =QNULL; 18 | (*Q)->rear=QNULL; 19 | } 20 | 21 | bool enqueue(tItemQ item, tQueue *Q) { 22 | tPosQ tmp; 23 | if (!createNode(&tmp)) { 24 | return false; 25 | }else { 26 | tmp->data=item; 27 | tmp->next=QNULL; 28 | if(isEmptyQueue(*Q)){ 29 | (*Q)->front=(*Q)->rear=tmp; 30 | }else { 31 | (*Q)->rear->next=tmp; 32 | (*Q)->rear=tmp; 33 | } 34 | return true; 35 | } 36 | } 37 | 38 | void dequeue(tQueue *Q) { 39 | tPosQ tmp; 40 | 41 | if (isEmptyQueue(*Q)) { // If isEmptyQueue return nothing 42 | return; 43 | } 44 | 45 | tmp = (*Q)->front; // Front goes backward 46 | (*Q)->front = (*Q)->front->next; 47 | 48 | if ((*Q)->front == QNULL) { 49 | (*Q)->rear = QNULL; 50 | } 51 | free(tmp); 52 | } 53 | 54 | tItemQ front(tQueue Q){ //Precondition: Queue not empty. 55 | return Q->front->data; 56 | } 57 | -------------------------------------------------------------------------------- /Queues/Queue_Linked_List/Tutorial_Implementation/Exers/queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | #define QNULL NULL 3 | 4 | typedef int tItemQ; 5 | 6 | typedef struct tNodeQ* tPosQ; 7 | 8 | struct tNodeQ{ 9 | tItemQ data; 10 | tPosQ next; 11 | }; 12 | 13 | typedef struct tNodeQueue* tQueue; 14 | 15 | struct tNodeQueue{ 16 | tPosQ front; 17 | tPosQ rear; 18 | }; 19 | 20 | 21 | void createEmptyQueue(tQueue* Q); 22 | bool enqueue(tItemQ item,tQueue *Q); 23 | void dequeue(tQueue *Q); 24 | tItemQ front(tQueue Q); 25 | bool isEmptyQueue(tQueue Q); 26 | 27 | -------------------------------------------------------------------------------- /Queues/Queue_Linked_List/Tutorial_Implementation/Exers/test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "queue.h" 3 | 4 | void print_front(tQueue Q){ 5 | printf(" %d ",front(Q)); 6 | } 7 | 8 | 9 | int main(){ 10 | 11 | tQueue Queue; 12 | createEmptyQueue(&Queue); 13 | enqueue(1,&Queue); 14 | enqueue(3,&Queue); 15 | dequeue(&Queue); 16 | print_front(Queue); 17 | enqueue(15,&Queue); 18 | enqueue(20,&Queue); 19 | print_front(Queue); 20 | dequeue(&Queue); 21 | print_front(Queue); 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Queues/Queue_Linked_List/Tutorial_Implementation/queue.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "queue.h" 4 | 5 | bool isEmptyQueue(tQueue Q) { 6 | return (Q->rear == QNULL); //Alternative--> (Q.front == QNULL) 7 | } 8 | 9 | bool createNode(tPosQ* p) { 10 | *p = malloc(sizeof(struct tNodeQ)); 11 | return *p!= QNULL; 12 | } 13 | 14 | void createEmptyQueue(tQueue* Q) { 15 | 16 | *Q = malloc(sizeof(tQueue)); 17 | (*Q)->front =QNULL; 18 | (*Q)->rear=QNULL; 19 | } 20 | 21 | bool enqueue(tItemQ item, tQueue *Q) { 22 | tPosQ tmp; 23 | if (!createNode(&tmp)) { 24 | return false; 25 | }else { 26 | tmp->data=item; 27 | tmp->next=QNULL; 28 | if(isEmptyQueue(*Q)){ 29 | (*Q)->front=tmp; 30 | }else { 31 | (*Q)->rear->next=tmp; 32 | } 33 | (*Q)->rear=tmp; 34 | return true; 35 | } 36 | } 37 | 38 | void dequeue(tQueue *Q) { 39 | tPosQ tmp; 40 | 41 | if (isEmptyQueue(*Q)) { // If isEmptyQueue return nothing 42 | return; 43 | } 44 | 45 | tmp = (*Q)->front; // Front goes backward 46 | (*Q)->front = (*Q)->front->next; 47 | 48 | if ((*Q)->front == QNULL) { 49 | (*Q)->rear = QNULL; 50 | } 51 | free(tmp); 52 | } 53 | 54 | tItemQ front(tQueue Q){ //Precondition: Queue not empty. 55 | return Q->front->data; 56 | } 57 | -------------------------------------------------------------------------------- /Queues/Queue_Linked_List/Tutorial_Implementation/queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | #define QNULL NULL 3 | 4 | typedef int tItemQ; 5 | 6 | typedef struct tNodeQ* tPosQ; 7 | 8 | struct tNodeQ{ 9 | tItemQ data; 10 | tPosQ next; 11 | }; 12 | 13 | typedef struct tNodeQueue* tQueue; 14 | 15 | struct tNodeQueue{ 16 | tPosQ front; 17 | tPosQ rear; 18 | }; 19 | 20 | 21 | void createEmptyQueue(tQueue* Q); 22 | bool enqueue(tItemQ item,tQueue *Q); 23 | void dequeue(tQueue *Q); 24 | tItemQ front(tQueue Q); 25 | bool isEmptyQueue(tQueue Q); 26 | 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C_ADTs 2 | C implementation of most common ADTs and pointers 3 | -------------------------------------------------------------------------------- /Search_Tree/binary_search_tree.c: -------------------------------------------------------------------------------- 1 | #include "binary_search_tree.h" 2 | #include 3 | #include 4 | 5 | 6 | bool isEmptyTree (tBST tree){ 7 | return (tree == NULLBST); 8 | } 9 | 10 | void createEmptyTree(tBST* tree){ 11 | *tree=NULLBST; 12 | } 13 | 14 | 15 | tBST findKey(tBST tree, tKey key){ 16 | 17 | if (isEmptyTree(tree)) 18 | return NULLBST; 19 | else if (key == tree->key) 20 | return tree; 21 | else if (key < tree->key) 22 | return findKey(tree->left, key); 23 | else // (key > tree->key) 24 | return findKey(tree->right, key); 25 | 26 | } 27 | 28 | bool createBSTNode(tBSTPos* p,tKey key){ 29 | 30 | *p = malloc(sizeof(struct tBSTNode)); 31 | 32 | if(*p != NULLBST){ 33 | (*p)->key = key; 34 | (*p)->left = NULLBST; 35 | (*p)->right = NULLBST; 36 | } 37 | return *p!=NULLBST; 38 | } 39 | 40 | bool insertKey(tBST* tree, tKey key) 41 | { 42 | if (isEmptyTree(*tree)) 43 | return createBSTNode(tree, key); 44 | else if (key == (*tree)->key) 45 | return true; 46 | else if (key < (*tree)->key) 47 | return insertKey(&(*tree)->left, key); 48 | else // (key > (*tree)->key) 49 | return insertKey(&(*tree)->right, key); 50 | } 51 | 52 | 53 | void replace (tBST* subTree,tBST* auxTree){ //Replace the content of a node by its predecessors 54 | if (!isEmptyTree((*subTree)->right)) { 55 | replace(&(*subTree)->right,auxTree); //Going down the right branch 56 | }else { 57 | (*auxTree)->key = (*subTree)->key; //We replace the data fields of the node 58 | *auxTree = *subTree; //We mark the node on which we will call free() 59 | (*subTree) = (*subTree)->left; //We re-link the tree structure by "skyping" 60 | //the node to be deleted 61 | } 62 | } 63 | 64 | void removeKey(tBST* tree,tKey key){ 65 | 66 | tBST aux; 67 | if (key < (*tree)->key) { 68 | removeKey(&(*tree)->left, key); //If the key is smaller, continue through left subtree 69 | }else if (key > (*tree)->key) { 70 | removeKey(&(*tree)->right, key); //If the key is larger, continue through right subtree 71 | }else { // key == (*tree)->key //Once we locate the key, we proceed to its deletion 72 | aux = *tree; 73 | if (isEmptyTree((*tree)->right)) { //If it has no right child, replace by left one 74 | *tree = (*tree)->left; //this covers the no-child case 75 | }else if (isEmptyTree((*tree)->left)) { //If it has no left child, replace by right one. 76 | *tree = (*tree)->right; 77 | }else { //If it has two children we call replace() passing 78 | //its left subtree as first argument 79 | replace(&(*tree)->left,&aux); 80 | } 81 | free(aux); 82 | } 83 | 84 | } 85 | 86 | tBST LeftChild(tBST tree){ 87 | return tree->left; 88 | } 89 | tBST RightChild(tBST tree){ 90 | return tree->right; 91 | } 92 | tKey Root(tBST tree){ 93 | return tree->key; 94 | } 95 | -------------------------------------------------------------------------------- /Search_Tree/binary_search_tree.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define NULLBST NULL 6 | 7 | typedef int tKey; 8 | 9 | typedef struct tBSTNode *tBSTPos; 10 | 11 | struct tBSTNode { 12 | tKey key; 13 | tBSTPos right; 14 | tBSTPos left; 15 | }; 16 | 17 | typedef tBSTPos tBST; 18 | 19 | 20 | bool isEmptyTree (tBST tree); 21 | void createEmptyTree(tBST* tree); 22 | bool insertKey(tBST* tree, tKey key); 23 | tBST findKey(tBST tree, tKey key); 24 | void replace (tBST* subTree,tBST* auxTree); 25 | void removeKey(tBST* tree,tKey key); 26 | tBST LeftChild(tBST tree); 27 | tBST RightChild(tBST tree); 28 | tKey Root(tBST tree); 29 | 30 | -------------------------------------------------------------------------------- /Stack/Stack_Exercices/Ex2/dynamic_queue.c: -------------------------------------------------------------------------------- 1 | #include "dynamic_queue.h" 2 | #include 3 | 4 | bool isEmptyQueue(tQueue Q) { return (Q.front == QNULL); } 5 | 6 | 7 | void createEmptyQueue(tQueue *Q) { 8 | Q->front = //We don't use (*Q)->front because tQueue is a struct, not a pointer 9 | QNULL; 10 | Q->rear = QNULL; 11 | } 12 | 13 | bool createNode(tPosQ *p) { 14 | *p = malloc(sizeof(struct tNodeQ)); 15 | return *p != QNULL; 16 | } 17 | 18 | bool enqueue(tItemQ d, tQueue *Q) { 19 | tPosQ p; 20 | 21 | if (!createNode(&p)) { 22 | return false; 23 | } else { 24 | p->item = d; 25 | p->next = QNULL; 26 | 27 | if (isEmptyQueue(*Q)) { 28 | Q->front = p; 29 | 30 | } else { 31 | Q->rear->next = p; 32 | } 33 | 34 | Q->rear = p; 35 | 36 | return true; 37 | } 38 | } 39 | 40 | void dequeue(tQueue *Q) { 41 | tPosQ p; 42 | 43 | if (isEmptyQueue(*Q)) { // If isEmptyQueue return nothing 44 | return; 45 | } 46 | 47 | p = Q->front; 48 | Q->front = p->next; 49 | 50 | if (isEmptyQueue(*Q)) { 51 | Q->rear = QNULL; 52 | } 53 | 54 | free(p); 55 | } 56 | 57 | tItemQ front(tQueue Q) { return Q.front->item; } 58 | 59 | -------------------------------------------------------------------------------- /Stack/Stack_Exercices/Ex2/dynamic_queue.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | #define QNULL NULL 5 | 6 | typedef int tItemQ; 7 | 8 | typedef struct tNodeQ *tPosQ; 9 | 10 | struct tNodeQ{ 11 | tItemQ item; 12 | tPosQ next; 13 | }; 14 | 15 | typedef struct Queue{ 16 | tPosQ front; 17 | tPosQ rear; 18 | 19 | } tQueue; 20 | 21 | /* Function prototypes */ 22 | 23 | void createEmptyQueue(tQueue *Q); 24 | bool enqueue(tItemQ d, tQueue *Q); 25 | void dequeue(tQueue *Q); 26 | tItemQ front(tQueue Q); 27 | bool isEmptyQueue(tQueue Q); 28 | 29 | -------------------------------------------------------------------------------- /Stack/Stack_Exercices/Ex2/dynamic_stack.c: -------------------------------------------------------------------------------- 1 | #include "dynamic_stack.h" 2 | #include 3 | 4 | void createEmptyStack(tStack *S) { *S = SNULL; } 5 | 6 | bool createNodeS(tPosS *p) { 7 | *p = malloc(sizeof(struct tNodeS)); 8 | return (*p != SNULL); 9 | } 10 | 11 | bool push(tItemS d, tStack *S) { 12 | tPosS aux; 13 | 14 | if (!(createNodeS(&aux))) { 15 | return false; 16 | } else { 17 | aux->data = d; 18 | aux->down = *S; 19 | *S = aux; 20 | return true; 21 | } 22 | } 23 | 24 | void pop(tStack *S) { 25 | tPosS aux; 26 | 27 | aux = *S; 28 | *S = (*S)->down; 29 | free(aux); 30 | } 31 | 32 | tItemS peek(tStack S) { return S->data; } 33 | 34 | bool isEmptyStack(tStack S) { return (S == SNULL); } 35 | -------------------------------------------------------------------------------- /Stack/Stack_Exercices/Ex2/dynamic_stack.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* Types definition */ 5 | 6 | #define SNULL NULL 7 | 8 | typedef int tItemS; 9 | typedef struct tNodeS *tPosS; 10 | 11 | struct tNodeS { 12 | tItemS data; 13 | tPosS down; 14 | }; 15 | 16 | typedef tPosS tStack; 17 | 18 | /* Function prototypes */ 19 | 20 | void createEmptyStack(tStack *S); 21 | bool createNode(tPosS *p); 22 | bool push(tItemS d, tStack *S); 23 | void pop(tStack *S); 24 | tItemS peek(tStack S); 25 | bool isEmptyStack(tStack S); 26 | 27 | 28 | -------------------------------------------------------------------------------- /Stack/Stack_Exercices/Ex2/exer2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "dynamic_queue.h" 4 | #include "dynamic_stack.h" 5 | 6 | void string_entry(char str[]){ 7 | 8 | printf("Enter character sequence (MAX 50)(ENTER to finish): \n"); 9 | scanf("%50[^\n]",str); 10 | 11 | } 12 | 13 | void str2stack(char str[],tStack* stack){ 14 | char ch; 15 | int lenght= strlen(str); 16 | for (int i=lenght; 0<=i; i--) { 17 | push(str[i],stack); 18 | } 19 | } 20 | 21 | void stack2queue(tStack* stack,tQueue* queue){ 22 | 23 | char tmp; 24 | while (!isEmptyStack(*stack)) { 25 | tmp=peek(*stack); 26 | pop(stack); 27 | enqueue(tmp,queue); 28 | } 29 | } 30 | 31 | void queue2stack(tStack* stack,tQueue* queue){ 32 | 33 | char tmp; 34 | while (!isEmptyQueue(*queue)) { 35 | tmp=front(*queue); 36 | dequeue(queue); 37 | push(tmp,stack); 38 | } 39 | } 40 | 41 | void read_stack(tStack stack){ //Only for test purposes !!!!!!!!!!!!! 42 | char tmp; 43 | while (!isEmptyStack(stack)) { 44 | tmp=peek(stack); 45 | pop(&stack); 46 | printf("%c\n",tmp); 47 | } 48 | } 49 | 50 | 51 | 52 | int main(){ 53 | char string[50]; 54 | tStack stack; 55 | createEmptyStack(&stack); 56 | tQueue queue; 57 | createEmptyQueue(&queue); 58 | 59 | 60 | string_entry(string); 61 | printf("Inital Stack:\n"); 62 | printf("%s\n",string); 63 | str2stack(string,&stack); 64 | stack2queue(&stack,&queue); 65 | queue2stack(&stack,&queue); 66 | printf("Reverse Stack:\n"); 67 | read_stack(stack); 68 | 69 | return 0; 70 | } -------------------------------------------------------------------------------- /Stack/Stack_Exercices/Exer1/dynamic_stack.c: -------------------------------------------------------------------------------- 1 | #include "dynamic_stack.h" 2 | #include 3 | 4 | void createEmptyStack(tStack *S) { *S = SNULL; } 5 | 6 | bool createNode(tPosS *p) { 7 | *p = malloc(sizeof(struct tNodeS)); 8 | return (*p != SNULL); 9 | } 10 | 11 | bool push(tItemS d, tStack *S) { 12 | tPosS aux; 13 | 14 | if (!(createNode(&aux))) { 15 | return false; 16 | } else { 17 | aux->data = d; 18 | aux->down = *S; 19 | *S = aux; 20 | return true; 21 | } 22 | } 23 | 24 | void pop(tStack *S) { 25 | tPosS aux; 26 | 27 | aux = *S; 28 | *S = (*S)->down; 29 | free(aux); 30 | } 31 | 32 | tItemS peek(tStack S) { return S->data; } 33 | 34 | bool isEmptyStack(tStack S) { return (S == SNULL); } 35 | -------------------------------------------------------------------------------- /Stack/Stack_Exercices/Exer1/dynamic_stack.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* Types definition */ 5 | 6 | #define SNULL NULL 7 | 8 | typedef int tItemS; 9 | typedef struct tNodeS *tPosS; 10 | 11 | struct tNodeS { 12 | tItemS data; 13 | tPosS down; 14 | }; 15 | 16 | typedef tPosS tStack; 17 | 18 | /* Function prototypes */ 19 | 20 | void createEmptyStack(tStack *S); 21 | bool createNode(tPosS *p); 22 | bool push(tItemS d, tStack *S); 23 | void pop(tStack *S); 24 | tItemS peek(tStack S); 25 | bool isEmptyStack(tStack S); 26 | 27 | 28 | -------------------------------------------------------------------------------- /Stack/Stack_Exercices/Exer1/ex1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "dynamic_stack.h" 4 | 5 | void string_entry(char str[]){ 6 | 7 | printf("Enter algebric expresion {} () [] (MAX 50)(ENTER to finish): \n"); 8 | scanf("%50[^\n]",str); 9 | 10 | } 11 | 12 | void check_stack(char ch,tStack* stack){ 13 | char stack_char=peek(*stack); 14 | 15 | if ((stack_char=='(')&&(ch==')')) { 16 | pop(stack); 17 | }else if((stack_char=='{')&&(ch=='}')){ 18 | pop(stack); 19 | }else if ((stack_char=='[')&&(ch==']')) { 20 | pop(stack); 21 | }else { 22 | printf("Error! Incorrect Sequence\n"); 23 | exit(1); 24 | 25 | } 26 | } 27 | 28 | 29 | void read_and_push(char str[],tStack* stack){ 30 | char ch; 31 | int lenght= strlen(str); 32 | for (int i=0; i 3 | 4 | void createEmptyStack(tStack *S) { *S = SNULL; } 5 | 6 | bool createNode(tPosS *p) { 7 | *p = malloc(sizeof(struct tNodeS)); 8 | return (*p != SNULL); 9 | } 10 | 11 | bool push(tItemS d, tStack *S) { 12 | tPosS aux; 13 | 14 | if (!(createNode(&aux))) { 15 | return false; 16 | } else { 17 | aux->data = d; 18 | aux->down = *S; 19 | *S = aux; 20 | return true; 21 | } 22 | } 23 | 24 | void pop(tStack *S) { 25 | tPosS aux; 26 | 27 | aux = *S; 28 | *S = (*S)->down; 29 | free(aux); 30 | } 31 | 32 | tItemS peek(tStack S) { return S->data; } 33 | 34 | bool isEmptyStack(tStack S) { return (S == SNULL); } 35 | -------------------------------------------------------------------------------- /Stack/dynamic_stack.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* Types definition */ 5 | 6 | #define SNULL NULL 7 | 8 | typedef int tItemS; 9 | typedef struct tNodeS *tPosS; 10 | 11 | struct tNodeS { 12 | tItemS data; 13 | tPosS down; 14 | }; 15 | 16 | typedef tPosS tStack; 17 | 18 | /* Function prototypes */ 19 | 20 | void createEmptyStack(tStack *S); 21 | bool createNode(tPosS *p); 22 | bool push(tItemS d, tStack *S); 23 | void pop(tStack *S); 24 | tItemS peek(tStack S); 25 | bool isEmptyStack(tStack S); 26 | 27 | 28 | -------------------------------------------------------------------------------- /Stack/static_stack.c: -------------------------------------------------------------------------------- 1 | #include "static_stack.h" 2 | 3 | void createEmptyStack(tStack *S) { S->top = SNULL; } 4 | 5 | bool push(tItemS d, tStack *S) { 6 | if (S->top == SMAX - 1) { //Full Stack 7 | return false; 8 | } else { 9 | S->top++; 10 | S->data[S->top] = d; 11 | } 12 | return true; 13 | } 14 | 15 | void pop(tStack *S) { 16 | S->top--; 17 | } 18 | 19 | tItemS peek(tStack S) { return S.data[S.top]; } 20 | 21 | bool isEmptyStack(tStack S) { return (S.top == SNULL); } 22 | -------------------------------------------------------------------------------- /Stack/static_stack.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Types definition */ 4 | 5 | #define SNULL -1 6 | #define SMAX 10 7 | 8 | typedef int tItemS; 9 | typedef int tPosS; 10 | 11 | typedef struct Stack { 12 | tItemS data[SMAX]; 13 | tPosS top; 14 | } tStack; 15 | 16 | /* Function prototypes */ 17 | 18 | void createEmptyStack(tStack *S); 19 | bool push(tItemS d, tStack *S); 20 | void pop(tStack *S); 21 | tItemS peek(tStack S); 22 | bool isEmptyStack(tStack S); 23 | 24 | -------------------------------------------------------------------------------- /Tree/extra_functions.c: -------------------------------------------------------------------------------- 1 | #include "tree_dynamic.h" 2 | #include 3 | 4 | int countNodes(tBinTree T) 5 | { 6 | 7 | if (IsEmptyTree(T)) 8 | { 9 | return 0; 10 | } 11 | 12 | return 1 + countNodes(LeftChild(T)) + countNodes(RightChild(T)); 13 | } 14 | 15 | int countL(tBinTree T) 16 | { // Leaf counter 17 | if (IsEmptyTree(T)) 18 | { 19 | return 0; 20 | } 21 | else if ((IsEmptyTree(LeftChild(T))) && (IsEmptyTree(RightChild(T)))) 22 | { 23 | return 1; 24 | } 25 | else 26 | { 27 | return ((countL(LeftChild(T))) + countL(RightChild(T))); 28 | } 29 | } 30 | 31 | int comp(int a, int b) 32 | { 33 | if (a >= b) 34 | { 35 | return a; 36 | } 37 | else 38 | { 39 | return b; 40 | } 41 | } 42 | 43 | int height(tBinTree T) 44 | { // Max tree height 45 | 46 | if (IsEmptyTree(T)) 47 | { 48 | return 0; 49 | } 50 | else 51 | { 52 | return 1 + comp(height(LeftChild(T)), height(RightChild(T))); 53 | } 54 | } 55 | 56 | /* Check if tree is a "proper" tree (full tree) 57 | 58 | - Every parent node/internal node has either two or no children. 59 | - Leafs only at last level 60 | 61 | */ 62 | 63 | bool EsLleno_AUX(tBinTree T, int level, int height) 64 | { 65 | 66 | // Check if tree is empty 67 | if (IsEmptyTree(T)) 68 | { 69 | return false; 70 | } 71 | 72 | // Leaf --> last level? 73 | if (IsEmptyTree(T->left) && IsEmptyTree(T->right)) 74 | { 75 | 76 | return (level == height); 77 | } 78 | 79 | // Two childs --> Keep checking 80 | if (!IsEmptyTree(T->left) && !IsEmptyTree(T->right)) 81 | { 82 | 83 | return (EsLleno_AUX(T->left, level + 1, height) && EsLleno_AUX(T->right, level + 1, height)); 84 | } 85 | 86 | // One child only 87 | return false; 88 | } 89 | 90 | bool EsLleno(tBinTree T) 91 | { 92 | 93 | return EsLleno_AUX(T, 1, height(T)); 94 | } 95 | 96 | void deleteTroo(tBinTree *T){ 97 | 98 | if (!IsEmptyTree(*T)) { 99 | 100 | deleteTroo(&(*T)->left); 101 | deleteTroo(&(*T)->right); 102 | 103 | /* At last, delete root node */ 104 | printf("Deleteing Node : %d\n", (*T)->data); 105 | free(*T); 106 | *T = TNULL; 107 | } 108 | 109 | } 110 | 111 | /* >------------------------------------------------------ TEST ------------------------------------------------------< */ 112 | 113 | /* Original Tree 114 | * 100 115 | * 24 42 116 | * 1 1 2 32 117 | */ 118 | 119 | int main() 120 | { 121 | 122 | tBinTree main_tree; 123 | CreateEmptyTree(&main_tree); 124 | tBinTree tree_2; 125 | CreateEmptyTree(&tree_2); 126 | tBinTree tree_3; 127 | CreateEmptyTree(&tree_3); 128 | tBinTree tree_4; 129 | CreateEmptyTree(&tree_4); 130 | tBinTree tree_5; 131 | CreateEmptyTree(&tree_5); 132 | tBinTree tree_6; 133 | CreateEmptyTree(&tree_6); 134 | tBinTree tree_7; 135 | CreateEmptyTree(&tree_7); 136 | 137 | BuildTree(TNULL, 2, TNULL, &tree_6); 138 | BuildTree(TNULL, 32, TNULL, &tree_7); 139 | 140 | BuildTree(tree_6, 42, tree_7, &tree_3); 141 | 142 | BuildTree(TNULL, 1, TNULL, &tree_4); 143 | BuildTree(TNULL, 1, TNULL, &tree_5); 144 | 145 | BuildTree(tree_4, 24, tree_5, &tree_2); 146 | 147 | BuildTree(tree_2, 100, tree_3, &main_tree); 148 | 149 | // Test Node Counter 150 | printf("Num of Nodes: %d\n", countNodes(main_tree)); 151 | 152 | // Test Leaf 153 | printf("Num of Leafs: %d\n", countL(main_tree)); 154 | 155 | // Test Height 156 | printf("Height: %d\n", height(main_tree)); 157 | 158 | // Check full tree 159 | if (EsLleno(main_tree)) 160 | { 161 | printf("Full Tree!\n"); 162 | } 163 | else 164 | { 165 | printf("No Full Tree!\n"); 166 | } 167 | 168 | deleteTroo(&main_tree); 169 | 170 | 171 | // Test Node Counter 172 | printf("Num of Nodes: %d\n", countNodes(main_tree)); 173 | 174 | // Test Leaf 175 | printf("Num of Leafs: %d\n", countL(main_tree)); 176 | 177 | // Test Height 178 | printf("Height: %d\n", height(main_tree)); 179 | 180 | return 0; 181 | } 182 | -------------------------------------------------------------------------------- /Tree/tree_dynamic.c: -------------------------------------------------------------------------------- 1 | #include "tree_dynamic.h" 2 | #include 3 | 4 | void CreateEmptyTree(tBinTree *T) { *T = TNULL; } 5 | 6 | bool createNode(tPosT *p) { 7 | *p = malloc(sizeof(struct tNodeT)); 8 | return *p != TNULL; 9 | } 10 | 11 | bool BuildTree(tBinTree LTree, tItemT d, tBinTree Rtree, tBinTree *T) { 12 | if (createNode(T)) { 13 | (*T)->data = d; 14 | (*T)->left = LTree; 15 | (*T)->right = Rtree; 16 | return true; 17 | 18 | } else { 19 | return false; 20 | } 21 | } 22 | 23 | void deleteTree(tBinTree *T){ 24 | 25 | if (!IsEmptyTree(*T)) { 26 | 27 | deleteTree(&(*T)->left); 28 | deleteTree(&(*T)->right); 29 | 30 | free(*T); 31 | *T = TNULL; 32 | } 33 | 34 | 35 | } 36 | 37 | 38 | 39 | tBinTree LeftChild(tBinTree T) { return T->left; } 40 | 41 | tBinTree RightChild(tBinTree T) { return T->right; } 42 | 43 | tItemT Root(tBinTree T) { return T->data; } 44 | 45 | bool IsEmptyTree(tBinTree T) { return (T == TNULL); } 46 | -------------------------------------------------------------------------------- /Tree/tree_dynamic.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | 5 | 6 | #define TNULL NULL 7 | 8 | typedef int tItemT; 9 | 10 | typedef struct tNodeT *tPosT; 11 | 12 | struct tNodeT { 13 | tItemT data; 14 | tPosT right; 15 | tPosT left; 16 | }; 17 | typedef tPosT tBinTree; 18 | 19 | 20 | void CreateEmptyTree(tBinTree *T); 21 | bool BuildTree(tBinTree LTree, tItemT d, tBinTree Rtree, tBinTree *T); 22 | tBinTree LeftChild(tBinTree T); 23 | tBinTree RightChild(tBinTree T); 24 | tItemT Root(tBinTree T); 25 | bool IsEmptyTree(tBinTree T); 26 | void deleteTree(tBinTree *T); 27 | 28 | 29 | -------------------------------------------------------------------------------- /Tree/tree_traversal.c: -------------------------------------------------------------------------------- 1 | #include "tree_dynamic.h" 2 | #include 3 | #include 4 | 5 | 6 | //****************************************** 7 | //Recursive traversal 8 | //****************************************** 9 | 10 | //Pre-Order (root left right) 11 | 12 | void preorder_traversal(tBinTree T){ 13 | if (T!=TNULL) { 14 | printf(" %d ",T->data); 15 | preorder_traversal(T->left); 16 | preorder_traversal(T->right); 17 | } 18 | } 19 | 20 | //In-Order (left root right) 21 | 22 | void inorden_traversal(tBinTree T){ 23 | if (T!=TNULL) { 24 | inorden_traversal(T->left); 25 | printf(" %d ",T->data); 26 | inorden_traversal(T->right); 27 | } 28 | } 29 | 30 | //Post-Order (left right root) 31 | 32 | void post_order_traversal(tBinTree T){ 33 | if (T!=TNULL) { 34 | post_order_traversal(T->left); 35 | post_order_traversal(T->right); 36 | printf(" %d ",T->data); 37 | } 38 | } 39 | 40 | --------------------------------------------------------------------------------