├── .gitignore ├── README.md ├── array ├── Makefile ├── README.md ├── inc │ └── array.h └── src │ ├── array.c │ └── main.c ├── backtracking └── n_queens.py ├── graphs ├── adjacency_matrix │ ├── Makefile │ ├── README.md │ ├── inc │ │ └── am.h │ └── src │ │ └── main.c └── dijkstras.py ├── linked_list └── singly_linked_list │ ├── Makefile │ └── ll.c ├── math └── matrix_multiplication.py ├── multithreading ├── fizzbuzz.py └── ice-cream_cone │ ├── Makefile │ ├── icecream.c │ ├── icecream.h │ └── main.c ├── pointers ├── Makefile ├── README.md ├── inc │ └── pointer.h └── src │ ├── main.c │ └── pointer.c ├── sorts ├── Makefile ├── inc │ ├── sorts.h │ └── unsorted_array.h └── src │ ├── main.c │ └── sorts.c └── tree └── bsearch_tree ├── Makefile └── src └── main.c /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Info 2 | This repository is collection of source code implementation for major Data structures and Algorithms in the C programming language. 3 | 4 | The reference for this work has been the solved examples and problems provided in the book "Data Structures through C in Depth - S.K Srivastava and Deepali Srivastava". 5 | 6 | It may be noted that only the problem statement have been looked up from the book and the solutions/implementations are my own and have not been borrowed from any source. -------------------------------------------------------------------------------- /array/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT=array 2 | SRC= src/main.c src/array.c 3 | INC=./inc/ 4 | TEST=src/test.c 5 | 6 | all: $(SRC) 7 | gcc -Wall -O0 -ggdb $(SRC) -I$(INC) -o $(PROJECT) 8 | 9 | clean: 10 | rm -rf $(PROJECT) -------------------------------------------------------------------------------- /array/README.md: -------------------------------------------------------------------------------- 1 | Array is a collection of data with similar data type. 2 | -------------------------------------------------------------------------------- /array/inc/array.h: -------------------------------------------------------------------------------- 1 | #ifndef __ARRAY_H__ 2 | #define __ARRAY_H__ 3 | 4 | #include 5 | 6 | #define MAX_ARRAY_SIZE 10 7 | #define MAX_MAT_ROW 3 8 | #define MAX_MAT_COL 4 9 | 10 | /* 11 | * Reads array elements from the user and then prints them out. 12 | */ 13 | void read_and_display(); 14 | 15 | /* 16 | * Find the smallest and the largest number is the array user provided. 17 | */ 18 | void smallest_largest(); 19 | 20 | /* 21 | * Reverse and array 22 | */ 23 | void reverse(); 24 | 25 | /* 26 | * Pass each array element to another function 27 | */ 28 | void check_val(int num); 29 | 30 | /* 31 | * Pass an array element to another function. 32 | */ 33 | void pass_element_to_function(); 34 | 35 | /* 36 | * Prints an array being passed. 37 | */ 38 | void print_array(int array[], int size); 39 | 40 | /* 41 | * Passes the entire array to a function. 42 | */ 43 | void pass_array_to_function(); 44 | 45 | /* 46 | * Print Matrix. 47 | */ 48 | void print_matrix(int array[MAX_MAT_ROW][MAX_MAT_COL]); 49 | 50 | /* 51 | * Input and display a matrix. 52 | */ 53 | void intput_and_display_matrix(); 54 | 55 | /* 56 | * prints Sum of matrices. 57 | */ 58 | void sum_of_matrices(); 59 | 60 | /* 61 | * prints Multiplication of matrices. 62 | */ 63 | void matrix_multiplication(); 64 | 65 | #endif -------------------------------------------------------------------------------- /array/src/array.c: -------------------------------------------------------------------------------- 1 | #include "array.h" 2 | 3 | /* 4 | * Reads array elements from the user and then prints them out. 5 | */ 6 | void read_and_display(){ 7 | int array[MAX_ARRAY_SIZE]; 8 | 9 | for (int i=0; i large){ 39 | large = array[i]; 40 | } 41 | } 42 | 43 | printf("Smallest: %d, Largest: %d\n", small, large); 44 | } 45 | 46 | /* 47 | * Reverse and array 48 | */ 49 | void reverse(){ 50 | int array[MAX_ARRAY_SIZE]; 51 | 52 | for (int i=0; i=0): 89 | if (self.board[i][b] == QUEEN): 90 | return False 91 | i -= 1 92 | 93 | return True 94 | 95 | def valid_r_diagonal(self, a, b): 96 | ''' 97 | Check for existance of another queen in diagonal going from left bottom 98 | to right top. 99 | ''' 100 | i = a - 1 101 | j = b + 1 102 | 103 | while (i >= 0 and j < self.board_size): 104 | if self.board[i][j] == QUEEN: 105 | return False 106 | 107 | i -= 1 108 | j += 1 109 | 110 | return True 111 | 112 | def valid_l_diagonal(self, a, b): 113 | ''' 114 | Check for existance of another queen in diagonal going from right bottom 115 | to left top. 116 | ''' 117 | i = a - 1 118 | j = b - 1 119 | 120 | while (i>=0 and j >= 0): 121 | if self.board[i][j] == QUEEN: 122 | return False 123 | 124 | i -= 1 125 | j -= 1 126 | 127 | return True 128 | 129 | def valid_move(self, a, b): 130 | ''' 131 | Check if the queen being placed at a given position is not attacked by 132 | any other queen 133 | ''' 134 | if self.valid_vertical(a, b) and self.valid_r_diagonal(a, b) and self.valid_l_diagonal(a, b): 135 | return True 136 | 137 | return False 138 | 139 | def move_queen(self, a, b): 140 | ''' 141 | move queen to provided location 142 | ''' 143 | if a == self.board_size: 144 | return True 145 | 146 | self.place_queen(a, b) 147 | if not self.valid_move(a, b): 148 | self.undo_move(a, b) 149 | return False 150 | else: 151 | for i in range(0, self.board_size): 152 | if self.move_queen(a+1, i): 153 | return True 154 | else: 155 | self.undo_move(a+1, i) 156 | i += 1 157 | return False 158 | 159 | if __name__ == '__main__': 160 | main() -------------------------------------------------------------------------------- /graphs/adjacency_matrix/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT=adjacency_matrix 2 | SRC= src/main.c 3 | INC=./inc/ 4 | TEST=src/test.c 5 | 6 | all: $(SRC) 7 | gcc -Wall -O0 -ggdb $(SRC) -I$(INC) -o $(PROJECT) 8 | 9 | clean: 10 | rm -rf $(PROJECT) -------------------------------------------------------------------------------- /graphs/adjacency_matrix/README.md: -------------------------------------------------------------------------------- 1 | Adjacency matrix mantains the information of the adjacent vertices. -------------------------------------------------------------------------------- /graphs/adjacency_matrix/inc/am.h: -------------------------------------------------------------------------------- 1 | #define MAX 7 2 | 3 | unsigned int am[MAX][MAX] = { 4 | {0, 1, 0, 0, 0, 0, 0}, 5 | {0, 0, 1, 0, 0, 0, 0}, 6 | {0, 0, 0, 1, 1, 1, 0}, 7 | {0, 0, 0, 0, 0, 0, 0}, 8 | {0, 0, 0, 0, 0, 0, 0}, 9 | {0, 0, 0, 0, 0, 0, 1}, 10 | {0, 0, 0, 0, 0, 0, 0} 11 | }; -------------------------------------------------------------------------------- /graphs/adjacency_matrix/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "am.h" 3 | 4 | extern unsigned int am[MAX][MAX]; 5 | 6 | struct direction { 7 | unsigned int from; 8 | unsigned int to; 9 | }; 10 | 11 | unsigned int print_path(unsigned int from, unsigned int to) { 12 | if (from >= MAX || to >= MAX){ 13 | return 0; 14 | } 15 | 16 | if (from == to){ 17 | return 0; 18 | } 19 | 20 | if (am[from][to]==1){ 21 | printf("%u",to); 22 | return 1; 23 | } 24 | 25 | for (unsigned int i=from; i self.path[n][1] + child[1]: 50 | del self.path[child[0]] 51 | self.path[child[0]] = (n, self.path[n][1] + child[1]) 52 | 53 | return self.formPath() 54 | 55 | def formPath(self): 56 | n = self.end 57 | shortestPath = [n] 58 | 59 | while n is not self.start: 60 | shortestPath.insert(0, self.path[n][0]) 61 | n = self.path[n][0] 62 | 63 | return shortestPath 64 | 65 | def enqueueNode(self, node): 66 | for n in self.graph[node]: 67 | self.q.put(n[0]) 68 | 69 | 70 | def main(): 71 | if len(sys.argv) != 3: 72 | print ("usage: python3 dijkstras.py start end") 73 | return 74 | 75 | ''' 76 | Graph: 77 | A-B : 9, A-C : 2, A-D : 14 78 | B-E : 6 79 | C-D : 9, C-F : 10, C-E = 11 80 | D-F : 7 81 | E-F : 15 82 | ''' 83 | graph = { 84 | 'A':[('B',9),('C',2),('D',14)], 85 | 'B':[('A',9),('E',6)], 86 | 'C':[('A',2),('D',9),('F',10),('E',11)], 87 | 'D':[('A',14),('C',9),('F',7)], 88 | 'E':[('B',6),('C',11),('F',15)], 89 | 'F':[('D',7),('C',10),('E',15)] 90 | } 91 | 92 | d = dijkstras(graph) 93 | path = d.findShortestPath(sys.argv[1], sys.argv[2]) 94 | print(path) 95 | 96 | if __name__ == '__main__': 97 | main() -------------------------------------------------------------------------------- /linked_list/singly_linked_list/Makefile: -------------------------------------------------------------------------------- 1 | all: ll.c 2 | gcc -ggdb ll.c -o ll 3 | 4 | clean: 5 | rm -rf ll -------------------------------------------------------------------------------- /linked_list/singly_linked_list/ll.c: -------------------------------------------------------------------------------- 1 | // Uses GPL v3 license 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | typedef struct __node{ 8 | int data; 9 | struct __node* next; 10 | } node; 11 | 12 | void add_to_list(node **head) { 13 | node *temp_node; 14 | 15 | temp_node = (node *) malloc (sizeof(node)); 16 | temp_node->next = *head; 17 | 18 | printf("Enter the value to add to the list: "); 19 | scanf("%d", &temp_node->data); 20 | 21 | *head = temp_node; 22 | } 23 | 24 | void print_list(node *head) { 25 | if (!head) 26 | return; 27 | printf("%d -> ", head->data); 28 | print_list(head->next); 29 | } 30 | 31 | node *reverse_list(node *cur, node **head) { 32 | node *temp; 33 | int original_head = 0; 34 | 35 | if (cur == *head) 36 | original_head = 1; 37 | 38 | if (!cur->next) { 39 | *head = cur; 40 | return cur; 41 | } 42 | 43 | temp = reverse_list(cur->next, head); 44 | temp->next = cur; 45 | 46 | if (original_head) { 47 | cur->next = NULL; 48 | return *head; 49 | } 50 | 51 | return cur; 52 | } 53 | 54 | void clean_up(node *head) { 55 | if (!head) 56 | return; 57 | clean_up(head->next); 58 | free(head); 59 | } 60 | 61 | void main(){ 62 | int choice; 63 | node *head = NULL; 64 | 65 | do { 66 | printf("0: Add data, 1: Prints the list, 2: Reverses list, >2: Cleans memory and exits\nchoose: "); 67 | scanf("%d", &choice); 68 | 69 | switch(choice) { 70 | case 0: 71 | add_to_list(&head); 72 | break; 73 | 74 | case 1: 75 | print_list(head); 76 | break; 77 | 78 | case 2: 79 | head = reverse_list(head, &head); 80 | break; 81 | 82 | default: 83 | clean_up(head); 84 | return; 85 | } 86 | } while (1); 87 | } -------------------------------------------------------------------------------- /math/matrix_multiplication.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | def matrix_multiplication(a, b, size): 4 | c = [] 5 | for i in range(size): 6 | c.append([]) 7 | 8 | for k in range(size): 9 | for i in range(size): 10 | res = 0 11 | for j in range(size): 12 | res += a[i][j]*b[j][k] 13 | 14 | c[i].append(res) 15 | 16 | return c 17 | 18 | def main(): 19 | a = [[10, 20, 30], 20 | [49, 59, 69], 21 | [79, 87, 96]] 22 | 23 | b = [[1, 0, 0], 24 | [0, 1, 0], 25 | [0, 0, 1]] 26 | 27 | c = matrix_multiplication(a, b, 3) 28 | print(c) 29 | 30 | if __name__ == "__main__": 31 | main() -------------------------------------------------------------------------------- /multithreading/fizzbuzz.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import threading 3 | import sys 4 | 5 | class buzzFizz: 6 | def __init__(self, num): 7 | self.num = num 8 | self.t1_t2 = threading.Event() 9 | self.t1_t3 = threading.Event() 10 | self.t1_t4 = threading.Event() 11 | 12 | self.t2_t1 = threading.Event() 13 | self.t3_t1 = threading.Event() 14 | self.t4_t1 = threading.Event() 15 | 16 | self.t1_t2.clear() 17 | self.t1_t3.clear() 18 | self.t1_t4.clear() 19 | 20 | self.t2_t1.clear() 21 | self.t3_t1.clear() 22 | self.t4_t1.clear() 23 | 24 | def run(self): 25 | t = [] 26 | targets = [self.thread_2, self.thread_3, self.thread_4, self.thread_1] 27 | for i in range(4): 28 | t.append(threading.Thread(target=targets[i])) 29 | t[i].start() 30 | 31 | for i in range(4): 32 | t[i].join() 33 | 34 | def thread_1(self): 35 | # Let every one else read 36 | self.t1_t2.set() 37 | self.t1_t3.set() 38 | self.t1_t4.set() 39 | 40 | while True: 41 | # wait for every one else to signal that they are done 42 | # reading 43 | self.t2_t1.wait() 44 | self.t3_t1.wait() 45 | self.t4_t1.wait() 46 | 47 | self.num -= 1 48 | 49 | # clear the events being set previously 50 | self.t2_t1.clear() 51 | self.t3_t1.clear() 52 | self.t4_t1.clear() 53 | 54 | # Allow other threads to proceed 55 | self.t1_t2.set() 56 | self.t1_t3.set() 57 | self.t1_t4.set() 58 | 59 | if (self.num == 0): 60 | break 61 | 62 | def thread_2(self): 63 | while True: 64 | # Wait for thread 1 to signal updation of value 65 | self.t1_t2.wait() 66 | 67 | if (self.num % 3 == 0 and self.num % 5 != 0): 68 | print ("Buzz! {}".format(self.num)) 69 | 70 | # clear the event being set previously 71 | self.t1_t2.clear() 72 | 73 | # allow thread 1 to proceed 74 | self.t2_t1.set() 75 | 76 | if (self.num == 0): 77 | break 78 | 79 | def thread_3(self): 80 | while True: 81 | # Wait for thread 1 to signal updation of value 82 | self.t1_t3.wait() 83 | 84 | if (self.num % 5 == 0 and self.num % 3 != 0): 85 | print ("Fizz! {}".format(self.num)) 86 | 87 | # clear the event being set previously 88 | self.t1_t3.clear() 89 | self.t3_t1.set() 90 | 91 | if (self.num == 0): 92 | break 93 | 94 | def thread_4(self): 95 | while True: 96 | # Wait for thread 1 to signal updation of value 97 | self.t1_t4.wait() 98 | 99 | if (self.num % 3 == 0 and self.num % 5 == 0): 100 | print ("BuzzFizz! {}".format(self.num)) 101 | 102 | # clear the event being set previously 103 | self.t1_t4.clear() 104 | 105 | # allow thread 1 to proceed 106 | self.t4_t1.set() 107 | 108 | if (self.num == 0): 109 | break 110 | 111 | def main(): 112 | if len(sys.argv) != 2: 113 | print ("usage: python3 fizzbuzz.py NUM") 114 | return 115 | 116 | num = int(sys.argv[1]) 117 | bf = buzzFizz(num) 118 | bf.run() 119 | 120 | if __name__ == "__main__": 121 | main() -------------------------------------------------------------------------------- /multithreading/ice-cream_cone/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT = main 2 | GCCFLAGS = -lpthread -I./ 3 | 4 | SOURCES = $(wildcard *.c) 5 | OBJECTS = $(SOURCES:.c=.o) 6 | 7 | run: $(PROJECT) 8 | clear 9 | @./$(PROJECT) 10 | 11 | $(PROJECT): $(OBJECTS) 12 | gcc $(OBJECTS) $(GCCFLAGS) -o $(PROJECT) 13 | @rm $(OBJECTS) 14 | 15 | %.o: %.c %.h 16 | gcc -c $< $(GCCFLAGS) 17 | 18 | .PHONY: clean 19 | 20 | clean: 21 | @rm $(PROJECT) -------------------------------------------------------------------------------- /multithreading/ice-cream_cone/icecream.c: -------------------------------------------------------------------------------- 1 | #include "icecream.h" 2 | 3 | static pthread_mutex_t cone_queue_lock; 4 | static struct icecream_cone *approval_queue = NULL; 5 | static struct icecream_cone *last_cone = NULL; 6 | 7 | static uint32_t no_more_cones = FALSE; 8 | 9 | static pthread_mutex_t cashier_queue_lock; 10 | static uint32_t num_customers = 0; 11 | 12 | /* 13 | * Make payment to theCashier 14 | */ 15 | static void make_payment(){ 16 | pthread_mutex_lock(&cashier_queue_lock); 17 | num_customers--; 18 | pthread_mutex_unlock(&cashier_queue_lock); 19 | } 20 | 21 | /* 22 | * Used to submit a cone created by the Clerk into 23 | * Manager's inspection queue. 24 | */ 25 | static uint32_t submit_for_approval(struct icecream_cone *cone) { 26 | pthread_mutex_lock(&cone_queue_lock); 27 | if (last_cone == NULL) { 28 | approval_queue = cone; 29 | last_cone = approval_queue; 30 | } else { 31 | last_cone->next_request = cone; 32 | last_cone = cone; 33 | } 34 | pthread_mutex_unlock(&cone_queue_lock); 35 | } 36 | 37 | /* 38 | * Initializes the Mutex variables and sets randomly the number 39 | * of customers in the store. Returns the number of customers. 40 | */ 41 | uint32_t open_store() { 42 | if (pthread_mutex_init(&cone_queue_lock, NULL) != 0) 43 | return -1; 44 | 45 | if (pthread_mutex_init(&cashier_queue_lock, NULL) != 0) 46 | return -2; 47 | 48 | srand(time(0)); 49 | 50 | pthread_mutex_lock(&cashier_queue_lock); 51 | num_customers = rand() % MAX_CUSTOMERS + 1; 52 | pthread_mutex_unlock(&cashier_queue_lock); 53 | 54 | return num_customers; 55 | } 56 | 57 | /* 58 | * Fetches a Cone from the inspection queue and 59 | * decides if it's a GOOD or BAD cone. Exits when there 60 | * are no more cones to process. 61 | */ 62 | void *manager(void *args){ 63 | enum quality decision; 64 | 65 | while (!no_more_cones) { 66 | if (approval_queue) { 67 | decision = (rand() % 2) ? BAD : GOOD; 68 | 69 | pthread_mutex_lock(&cone_queue_lock); 70 | approval_queue->approval_status = decision; 71 | if (approval_queue == last_cone) { 72 | approval_queue = NULL; 73 | last_cone = NULL; 74 | } else { 75 | approval_queue = approval_queue->next_request; 76 | } 77 | pthread_mutex_unlock(&cone_queue_lock); 78 | } 79 | } 80 | 81 | return NULL; 82 | } 83 | 84 | /* 85 | * Orders random quantities of Cone decided by MAX_CONES, 86 | * Spwans a Clerk for every cone needed and waits for 87 | */ 88 | void *customer(void *args){ 89 | uint32_t num_cones = rand() % MAX_CONES + 1; 90 | uint32_t index; 91 | pthread_t clerks[10]; 92 | 93 | printf("CUSTOMER: %lu, Requested cones: %u\n", pthread_self(), num_cones); 94 | 95 | for(index = 0; index < num_cones; index++) 96 | pthread_create(&clerks[index], NULL, clerk, NULL); 97 | 98 | for(index=0; index < num_cones; index++) 99 | pthread_join(clerks[index], NULL); 100 | 101 | printf("CUSTOMER: %lu, Got all cones\n", pthread_self()); 102 | 103 | make_payment(); 104 | 105 | return NULL; 106 | } 107 | 108 | /* 109 | * Creates a Cone and waits for manager to approve the 110 | * quality. If manager is not happy, creates another cone. 111 | */ 112 | void *clerk(void *args){ 113 | struct icecream_cone cone; 114 | 115 | do { 116 | cone.approval_status = PENDING; 117 | cone.next_request = NULL; 118 | 119 | CREATE_CONE; 120 | submit_for_approval(&cone); 121 | 122 | while (cone.approval_status == PENDING); 123 | } while (cone.approval_status == BAD); 124 | 125 | return NULL; 126 | } 127 | 128 | /* 129 | * Takes a while to process the payment, 130 | * reports the number of customer in queue, 131 | * signals the Manager to leave when all 132 | * Customers are served. 133 | */ 134 | void *cashier(void *args){ 135 | while (num_customers){ 136 | CASHIER_PROCESSING; 137 | printf("CASHIER: Customers in Queue: %u\n", num_customers); 138 | } 139 | 140 | no_more_cones = TRUE; 141 | 142 | return NULL; 143 | } 144 | -------------------------------------------------------------------------------- /multithreading/ice-cream_cone/icecream.h: -------------------------------------------------------------------------------- 1 | #ifndef __ICECREAM_H_ 2 | #define __ICECREAM_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define TRUE 1 12 | #define FALSE 0 13 | 14 | #define MAX_CONES 4 15 | #define MAX_CUSTOMERS 10 16 | #define MAX_TIME 3 17 | 18 | #define CREATE_CONE (sleep(rand()%MAX_TIME)) 19 | #define CASHIER_PROCESSING (sleep(rand()%MAX_TIME)) 20 | 21 | enum quality { 22 | PENDING = 0x00, 23 | GOOD = 0x01, 24 | BAD = 0x02 25 | }; 26 | 27 | struct icecream_cone { 28 | enum quality approval_status; 29 | struct icecream_cone *next_request; 30 | }; 31 | 32 | uint32_t open_store(); 33 | void *manager(void *args); 34 | void *customer(void *args); 35 | void *clerk(void *args); 36 | void *cashier(void *args); 37 | 38 | #endif -------------------------------------------------------------------------------- /multithreading/ice-cream_cone/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Problem statement: 3 | * - in an ice-cream shop there is a Manager and a Cashier 4 | * - 1-10 Customers visit the shop 5 | * - Each customer can order 1-4 cones and spwans 1 clerk/cone 6 | * - Each worker creates one cone and visits the manager's office 7 | * to get the approval for the quality. If the quality is bad the 8 | * clerk remakes the cone and returns back later. 9 | * - There cannot be more than 2 clerks in the Manager's office and 10 | * can attend to only one clerk at a time. 11 | * - Once the customer gets his/her cone, he/she exits the shop after 12 | * paying the cashier. 13 | */ 14 | 15 | #include "icecream.h" 16 | 17 | int32_t main(int32_t argc, int8_t **argv){ 18 | pthread_t thread[2 + MAX_CUSTOMERS]; 19 | uint32_t customer_count; 20 | uint32_t index; 21 | 22 | printf("\n-------------------- STORE OPEN --------------------\n\n"); 23 | customer_count = open_store(); 24 | 25 | pthread_create(&thread[0], NULL, manager, NULL); 26 | pthread_create(&thread[1], NULL, cashier, NULL); 27 | 28 | for (index = 2; index < 2 + customer_count; index++) 29 | pthread_create(&thread[index], NULL, customer, NULL); 30 | 31 | for (index = 0; index < 2 + customer_count; index++) 32 | pthread_join(thread[index], NULL); 33 | 34 | printf("\n-------------------- STORE CLOSE -------------------\n\n"); 35 | return 0; 36 | } 37 | -------------------------------------------------------------------------------- /pointers/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT=pointer 2 | SRC= src/main.c src/pointer.c 3 | INC=./inc/ 4 | TEST=src/test.c 5 | 6 | all: $(SRC) 7 | gcc -Wall -O0 -ggdb $(SRC) -I$(INC) -o $(PROJECT) 8 | 9 | clean: 10 | rm -rf $(PROJECT) -------------------------------------------------------------------------------- /pointers/README.md: -------------------------------------------------------------------------------- 1 | Implementation of code relating to the working of pointers. -------------------------------------------------------------------------------- /pointers/inc/pointer.h: -------------------------------------------------------------------------------- 1 | #ifndef __POINTER_H__ 2 | #define __POINTER_H__ 3 | 4 | #include 5 | 6 | #endif -------------------------------------------------------------------------------- /pointers/src/main.c: -------------------------------------------------------------------------------- 1 | #include "pointer.h" 2 | 3 | int main(){ 4 | 5 | return 0; 6 | } -------------------------------------------------------------------------------- /pointers/src/pointer.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/streetdogg/ds_and_algorithms/3099e8a220a67bbf13ff141074cc41029fa9c785/pointers/src/pointer.c -------------------------------------------------------------------------------- /sorts/Makefile: -------------------------------------------------------------------------------- 1 | PROJECT=sorts 2 | SRC= src/main.c src/sorts.c 3 | INC=./inc/ 4 | TEST=src/test.c 5 | 6 | all: $(SRC) 7 | gcc -Wall -O0 -ggdb $(SRC) -I$(INC) -o $(PROJECT) 8 | 9 | clean: 10 | rm -rf $(PROJECT) -------------------------------------------------------------------------------- /sorts/inc/sorts.h: -------------------------------------------------------------------------------- 1 | #ifndef __SORTS_H__ 2 | #define __SORTS_H__ 3 | 4 | #include 5 | 6 | /* 7 | * Insertion Sort 8 | */ 9 | void insertion_sort(int *array, int length); 10 | 11 | /* 12 | * Bubble Sort 13 | */ 14 | void bubble_sort(int *array, int length); 15 | 16 | /* 17 | * Selection sort 18 | */ 19 | void selection_sort(int *array, int length); 20 | 21 | #endif -------------------------------------------------------------------------------- /sorts/inc/unsorted_array.h: -------------------------------------------------------------------------------- 1 | int unsorted_array[] = {589,752,322,308,480,454,504,841,801,470,761,314,944,392,205,195,561,908,179,995,719,452,499,456,269,933,923,206,188,860,293,220,329,414,152,687,194,982,530,443,938,23,503,407,108,623,150,598,381,190,210,134,404,502,736,809,792,811,829,146,494,93,475,650,201,323,797,742,551,185,18,389,983,929,38,311,217,120,426,171,68,835,936,513,316,129,806,585,430,88,961,441,729,665,64,629,320,896,851,628}; -------------------------------------------------------------------------------- /sorts/src/main.c: -------------------------------------------------------------------------------- 1 | #include "sorts.h" 2 | 3 | int main(){ 4 | // Import the unsorted array 5 | #include "unsorted_array.h" 6 | 7 | int choice; 8 | 9 | printf("Please select a Sort:\n" 10 | "1. Insertion sort.\n" 11 | "2. Bubble sort.\n" 12 | "3. Selection sort.\n" 13 | "Choice: "); 14 | scanf("%d", &choice); 15 | 16 | switch(choice){ 17 | case 1: insertion_sort(unsorted_array, 100); 18 | break; 19 | case 2: bubble_sort(unsorted_array, 100); 20 | break; 21 | case 3: selection_sort(unsorted_array, 100); 22 | break; 23 | default: 24 | break; 25 | } 26 | 27 | for (int i=0; i<100; i++){ 28 | printf ("%d ", unsorted_array[i]); 29 | } 30 | printf("\n"); 31 | 32 | return 0; 33 | } -------------------------------------------------------------------------------- /sorts/src/sorts.c: -------------------------------------------------------------------------------- 1 | #include "sorts.h" 2 | 3 | /* 4 | * Insertion Sort 5 | */ 6 | void insertion_sort(int *array, int length) { 7 | int key, i, j; 8 | 9 | for (i=1; ikey && j>=0; j--) { 13 | array[j+1] = array[j]; 14 | } 15 | 16 | array[j+1] = key; 17 | } 18 | } 19 | 20 | /* 21 | * Bubble Sort 22 | */ 23 | void bubble_sort(int *array, int length) { 24 | for (int i=length-1; i>0; i--) { 25 | for (int j=0; j array[j+1]){ 27 | array[j] ^= array[j+1]; 28 | array[j+1] ^= array[j]; 29 | array[j] ^= array[j+1]; 30 | } 31 | } 32 | } 33 | } 34 | 35 | /* 36 | * Selection sort 37 | */ 38 | void selection_sort(int *array, int length) { 39 | int pivot, min; 40 | 41 | for (pivot=0; pivot < length-1; pivot++) { 42 | 43 | min = pivot; 44 | 45 | for (int i=pivot; i 7 | #include 8 | #include 9 | 10 | typedef struct _node { 11 | uint32_t data; 12 | struct _node *l_child; 13 | struct _node *r_child; 14 | } node; 15 | 16 | void print_tree(node *root) { 17 | if (!root) 18 | return; 19 | 20 | print_tree(root->l_child); 21 | printf(" %u ", root->data); 22 | print_tree(root->r_child); 23 | } 24 | 25 | uint32_t add(node **root, uint32_t element) { 26 | 27 | if (!(*root)) { 28 | node *entry = (node *) malloc(sizeof(node)); 29 | 30 | entry->data = element; 31 | entry->l_child = NULL; 32 | entry->r_child = NULL; 33 | 34 | *root = entry; 35 | return 0; 36 | } 37 | 38 | if (element <= (*root)->data) 39 | return add(&(*root)->l_child, element); 40 | else 41 | return add(&(*root)->r_child, element); 42 | } 43 | 44 | int32_t search(const node *root, uint32_t element) { 45 | if (!root) 46 | return -1; 47 | else if (root->data == element) 48 | return 0; 49 | 50 | return (element < root->data) ? search(root->l_child, element) : search(root->r_child, element); 51 | } 52 | 53 | void delete(node *root, uint32_t element) { 54 | if (!root) 55 | return; 56 | 57 | // TODO :) 58 | } 59 | 60 | int32_t main() { 61 | node *root = NULL; 62 | 63 | add(&root, 5); 64 | add(&root, 1); 65 | add(&root, 6); 66 | add(&root, 3); 67 | 68 | print_tree(root); 69 | printf ("\n"); 70 | 71 | printf("looking for 3: %d\n", search(root, 3)); 72 | printf("looking for 13: %d\n", search(root, 13)); 73 | 74 | return 0; 75 | } 76 | --------------------------------------------------------------------------------