Merge branch 'main' of github.com:ergz/tlacyl
This commit is contained in:
@@ -3,25 +3,25 @@
|
|||||||
|
|
||||||
typedef struct Node {
|
typedef struct Node {
|
||||||
int value;
|
int value;
|
||||||
struct Node* next;
|
struct Node *next;
|
||||||
struct Node* prev;
|
struct Node *prev;
|
||||||
} Node;
|
} Node;
|
||||||
|
|
||||||
typedef struct LinkedList {
|
typedef struct LinkedList {
|
||||||
int length;
|
int length;
|
||||||
Node* head;
|
Node *head;
|
||||||
Node* tail;
|
Node *tail;
|
||||||
} LinkedList;
|
} LinkedList;
|
||||||
|
|
||||||
LinkedList* new_linked_list() {
|
LinkedList *new_linked_list() {
|
||||||
LinkedList* ll = malloc(sizeof(LinkedList));
|
LinkedList *ll = malloc(sizeof(LinkedList));
|
||||||
ll->length = 0;
|
ll->length = 0;
|
||||||
|
|
||||||
return (ll);
|
return (ll);
|
||||||
}
|
}
|
||||||
|
|
||||||
Node* new_node(int value) {
|
Node *new_node(int value) {
|
||||||
Node* n = malloc(sizeof(Node));
|
Node *n = malloc(sizeof(Node));
|
||||||
n->value = value;
|
n->value = value;
|
||||||
n->next = NULL;
|
n->next = NULL;
|
||||||
n->prev = NULL;
|
n->prev = NULL;
|
||||||
@@ -29,7 +29,7 @@ Node* new_node(int value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// append - insert a node at the end of the linked list
|
// append - insert a node at the end of the linked list
|
||||||
void append(LinkedList* list, Node* n) {
|
void append(LinkedList *list, Node *n) {
|
||||||
if (list->length == 0) {
|
if (list->length == 0) {
|
||||||
list->head = n;
|
list->head = n;
|
||||||
list->tail = n;
|
list->tail = n;
|
||||||
@@ -43,7 +43,7 @@ void append(LinkedList* list, Node* n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// prepend - insert a node at the front of the linked list
|
// prepend - insert a node at the front of the linked list
|
||||||
void preppend(LinkedList* list, Node* node) {
|
void preppend(LinkedList *list, Node *node) {
|
||||||
if (list->length == 0) {
|
if (list->length == 0) {
|
||||||
list->head = node;
|
list->head = node;
|
||||||
list->tail = node;
|
list->tail = node;
|
||||||
@@ -58,7 +58,7 @@ void preppend(LinkedList* list, Node* node) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// insert at - insert a node at a given index
|
// insert at - insert a node at a given index
|
||||||
void insert_at(LinkedList* list, Node* node, int index) {
|
void insert_at(LinkedList *list, Node *node, int index) {
|
||||||
if (index == 0) {
|
if (index == 0) {
|
||||||
preppend(list, node);
|
preppend(list, node);
|
||||||
} else if (index == list->length) {
|
} else if (index == list->length) {
|
||||||
@@ -66,7 +66,7 @@ void insert_at(LinkedList* list, Node* node, int index) {
|
|||||||
} else if (index > list->length || index < 0) {
|
} else if (index > list->length || index < 0) {
|
||||||
printf("ERROR: cannot add to list at index position %d, this is beyond the current index\n", index);
|
printf("ERROR: cannot add to list at index position %d, this is beyond the current index\n", index);
|
||||||
} else {
|
} else {
|
||||||
Node* current_node_at_index = list->head;
|
Node *current_node_at_index = list->head;
|
||||||
// get to the ith node
|
// get to the ith node
|
||||||
for (int i = 0; i < index; i++) {
|
for (int i = 0; i < index; i++) {
|
||||||
current_node_at_index = current_node_at_index->next;
|
current_node_at_index = current_node_at_index->next;
|
||||||
@@ -91,16 +91,17 @@ void remove_at_end(LinkedList* list) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_length(LinkedList* list) {
|
|
||||||
|
int get_length(LinkedList *list) {
|
||||||
return (list->length);
|
return (list->length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void destroy_list(LinkedList* list) {
|
void destroy_list(LinkedList *list) {
|
||||||
// if any nodes are in list destroy them first
|
// if any nodes are in list destroy them first
|
||||||
if (list->length > 0) {
|
if (list->length > 0) {
|
||||||
Node* current_node = list->head;
|
Node *current_node = list->head;
|
||||||
while (current_node != NULL) {
|
while (current_node != NULL) {
|
||||||
Node* next_node = current_node->next;
|
Node *next_node = current_node->next;
|
||||||
free(current_node);
|
free(current_node);
|
||||||
current_node = next_node;
|
current_node = next_node;
|
||||||
}
|
}
|
||||||
@@ -110,8 +111,8 @@ void destroy_list(LinkedList* list) {
|
|||||||
free(list);
|
free(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_list(LinkedList* list) {
|
void print_list(LinkedList *list) {
|
||||||
Node* curr = list->head;
|
Node *curr = list->head;
|
||||||
printf("[ ");
|
printf("[ ");
|
||||||
while (curr != NULL) {
|
while (curr != NULL) {
|
||||||
printf("%d ", curr->value);
|
printf("%d ", curr->value);
|
||||||
@@ -186,4 +187,5 @@ int main() {
|
|||||||
destroy_list(list);
|
destroy_list(list);
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
6
c/qs.c
6
c/qs.c
@@ -5,9 +5,9 @@
|
|||||||
// of it and everything greater than it on the right
|
// of it and everything greater than it on the right
|
||||||
int partition(int arr[], int lo, int hi) {
|
int partition(int arr[], int lo, int hi) {
|
||||||
int pivot_value = arr[hi];
|
int pivot_value = arr[hi];
|
||||||
int idx = -1; // this will keep track of current location of values less then the pivot
|
int idx = lo - 1; // this will keep track of current location of values less then the pivot
|
||||||
|
|
||||||
for (int i = 0; i < hi; i++) {
|
for (int i = lo; i < hi; i++) {
|
||||||
if (arr[i] <= pivot_value) {
|
if (arr[i] <= pivot_value) {
|
||||||
idx++;
|
idx++;
|
||||||
|
|
||||||
@@ -46,4 +46,4 @@ int main() {
|
|||||||
printf("%d ", arr[i]);
|
printf("%d ", arr[i]);
|
||||||
}
|
}
|
||||||
printf("]\n");
|
printf("]\n");
|
||||||
}
|
}
|
||||||
|
|||||||
69
c/qs2.cpp
Normal file
69
c/qs2.cpp
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* a rewrite to make sure I can do this without reading */
|
||||||
|
|
||||||
|
/*
|
||||||
|
partition
|
||||||
|
|
||||||
|
Modifies array by moving all values less then a pivot point
|
||||||
|
to the left of it and all value greater to the right. Returns
|
||||||
|
the index of the pivot at the end of the process.
|
||||||
|
|
||||||
|
@param int arr[] array to partition
|
||||||
|
@param int lo low value to start iteration on
|
||||||
|
@param int hi high value to end iteration on
|
||||||
|
*/
|
||||||
|
int partition(int arr[], int lo, int hi) {
|
||||||
|
int pindex = lo - 1; // where pivot will end up
|
||||||
|
int pivot_value = arr[hi]; // can be different
|
||||||
|
|
||||||
|
for (int i = lo; i < hi; i++) {
|
||||||
|
if (arr[i] <= pivot_value) {
|
||||||
|
pindex++;
|
||||||
|
int temp = arr[i];
|
||||||
|
arr[i] = arr[pindex];
|
||||||
|
arr[pindex] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// after iterating through array, we place pivot in correct
|
||||||
|
// position
|
||||||
|
pindex++;
|
||||||
|
arr[hi] = arr[pindex];
|
||||||
|
arr[pindex] = pivot_value;
|
||||||
|
|
||||||
|
return (pindex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
quick_sort
|
||||||
|
|
||||||
|
@param int arr[] array to be sorted
|
||||||
|
@param int lo starting point for iteration
|
||||||
|
@param int hi ending point for itereation
|
||||||
|
*/
|
||||||
|
void quick_sort(int arr[], int lo, int hi) {
|
||||||
|
if (lo < hi) { // base case is lo == hi
|
||||||
|
int pivot_index = partition(arr, lo, hi);
|
||||||
|
|
||||||
|
quick_sort(arr, lo, pivot_index - 1);
|
||||||
|
quick_sort(arr, pivot_index + 1, hi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_array(int arr[], int len) {
|
||||||
|
printf("[ ");
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
printf("%d ", arr[i]);
|
||||||
|
}
|
||||||
|
printf("]\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int arr[5] = {5, 4, 3, 2, 1};
|
||||||
|
printf("before ");
|
||||||
|
print_array(arr, 5);
|
||||||
|
quick_sort(arr, 0, 4);
|
||||||
|
printf("after ");
|
||||||
|
print_array(arr, 5);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user