Skip to content

Instantly share code, notes, and snippets.

View cs-fedy's full-sized avatar
🎯
Focusing

Fedi Abdouli cs-fedy

🎯
Focusing
View GitHub Profile
@cs-fedy
cs-fedy / maxRotatedArraySum.c
Last active August 24, 2020 20:06
Find maximum value of Sum with only rotations on given array allowed in C.
int get_max(int x, int y) {
if (x > y)
return x;
return y;
}
int get_sum(int * arr, int arr_size) {
int sum = 0, index;
for(index = 0; index < arr_size; index++) {
sum += index * arr[index];
@cs-fedy
cs-fedy / pairMakesSum.c
Last active August 24, 2020 17:30
Given an array A and a number x, check for pair in A with sum as x.
unsigned pair_in_sorted(int x, int * arr, int arr_size) {
int start = 0, end = arr_size - 1;
while(start < end) {
if (arr[start] + arr[end] == x)
return 1;
else if (arr[start] + arr[end] < x)
start++;
else
end--;
}
@cs-fedy
cs-fedy / searchRotated.c
Created August 24, 2020 15:16
Search an element in a sorted and rotated array in C.
int get_pivot(int * arr, int arr_size) {
int index = 1;
while(index < arr_size && arr[index] > arr[index - 1]) {
index++;
}
if (index >= arr_size)
return -1;
return index;
}
@cs-fedy
cs-fedy / rotateArray.c
Last active August 24, 2020 00:21
rotate an array by x in C.
void rotate_array(int * arr, int arr_size, int rotate_x) {
int index, j, temp;
for (index = 0; index < rotate_x; index++) {
temp = arr[0];
for (j = 1; j < arr_size; j++) {
arr[j - 1] = arr[j];
}
arr[arr_size - 1] = temp;
}
}
@cs-fedy
cs-fedy / dynamicArray.c
Last active September 15, 2020 18:36
dynamic array implementation using C.
#include <stdio.h>
#include <stdlib.h>
Arr * create_arr(int max_size) {
Arr * new_arr = malloc(sizeof(Arr));
new_arr -> size = 0;
new_arr -> max_size = 0;
new_arr -> items = malloc(max_size * sizeof(int));
if (new_arr -> items != NULL) {
new_arr -> max_size = max_size;
@cs-fedy
cs-fedy / add_sorted.c
Created August 19, 2020 22:20
Add a key to a sorted stack(result stack must be sorted).
void add_sorted(int key, stck * st) {
int tmp_arr[100], size = 0, index;
while(!is_empty(*st) || get_peek(*st) > key) {
tmp_arr[size++] = get_peek(*st);
pop(st);
}
for (index = 0; index < size; index++) {
push(tmp_arr[index], st);
}
}
@cs-fedy
cs-fedy / is_same.c
Created August 19, 2020 22:18
check if two stack are the same or no.
unsigned is_same(stck * st1, stck * st2) {
while (!is_empty(*st1) && !is_empty(*st2)) {
if (get_peek(*st1) != get_peek(*st2))
return 0;
pop(st1);
pop(st2);
}
if ((is_empty(*st1) && !is_empty(*st2)) ||
(!is_empty(*st1) && is_empty(*st2)))
return 0;
@cs-fedy
cs-fedy / ReverseLinkedList.c
Last active August 17, 2020 17:28
Reverse a singly linked list in C.
void reverse_ll(LinkedList * ll) {
Node * curr = ll -> head;
while (curr -> next) {
insert_first(ll, curr -> next -> key);
remove_succesor(curr);
}
// this for linked list designed with tail pointer:
// ll -> tail = curr;
}
@cs-fedy
cs-fedy / LinkedListQueue.c
Created August 12, 2020 18:02
Queue implementation using linked list in C.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct Node {
int key;
Node * next;
};
struct Queue {
@cs-fedy
cs-fedy / Queue.c
Created August 12, 2020 17:47
queue implementation in C.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct Queue {
int * keys;
int head;
int tail;
int max_size;
};