From c3cf173d30db6cff2561696a46abfcdef538bf71 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Sat, 2 Dec 2017 06:03:52 +0800 Subject: category --- .../Data Structure and Algorithm Analysis in C.pdf | Bin 0 -> 5089252 bytes ...res and algorithm analysis in C 2nd version.pdf | Bin 0 -> 6725931 bytes .../chapter_3/.polynomial_in_list.c.swp | Bin 0 -> 12288 bytes Computer_Science/data_structures/chapter_3/a.out | Bin 0 -> 8480 bytes .../data_structures/chapter_3/linked_list.c | 138 +++++++++++++++++++++ .../data_structures/chapter_3/linked_list.h | 23 ++++ .../data_structures/chapter_3/polynomial.c | 103 +++++++++++++++ .../data_structures/chapter_3/polynomial_in_list.c | 36 ++++++ Computer_Science/data_structures/chapter_3/stack.c | 120 ++++++++++++++++++ Computer_Science/data_structures/chapter_3/stack.h | 16 +++ .../data_structures/chapter_3/stack.h.gch | Bin 0 -> 1569200 bytes .../data_structures/chapter_3/stack_array.c | 90 ++++++++++++++ .../data_structures/chapter_3/stack_array.h | 16 +++ Computer_Science/leetcode/1_two_sum.c | 26 ++++ Computer_Science/leetcode/a.out | Bin 0 -> 7104 bytes Computer_Science/scala/Scala.pdf | Bin 0 -> 3824304 bytes .../Data Structure and Algorithm Analysis in C.pdf | Bin 5089252 -> 0 bytes ...res and algorithm analysis in C 2nd version.pdf | Bin 6725931 -> 0 bytes .../chapter_3/.polynomial_in_list.c.swp | Bin 12288 -> 0 bytes data_structures/chapter_3/a.out | Bin 8480 -> 0 bytes data_structures/chapter_3/linked_list.c | 138 --------------------- data_structures/chapter_3/linked_list.h | 23 ---- data_structures/chapter_3/polynomial.c | 103 --------------- data_structures/chapter_3/polynomial_in_list.c | 36 ------ data_structures/chapter_3/stack.c | 120 ------------------ data_structures/chapter_3/stack.h | 16 --- data_structures/chapter_3/stack.h.gch | Bin 1569200 -> 0 bytes data_structures/chapter_3/stack_array.c | 90 -------------- data_structures/chapter_3/stack_array.h | 16 --- leetcode/1_two_sum.c | 26 ---- leetcode/a.out | Bin 7104 -> 0 bytes scala/Scala.pdf | Bin 3824304 -> 0 bytes 32 files changed, 568 insertions(+), 568 deletions(-) create mode 100644 Computer_Science/data_structures/Data Structure and Algorithm Analysis in C.pdf create mode 100644 Computer_Science/data_structures/Data Structures and algorithm analysis in C 2nd version.pdf create mode 100644 Computer_Science/data_structures/chapter_3/.polynomial_in_list.c.swp create mode 100755 Computer_Science/data_structures/chapter_3/a.out create mode 100644 Computer_Science/data_structures/chapter_3/linked_list.c create mode 100644 Computer_Science/data_structures/chapter_3/linked_list.h create mode 100644 Computer_Science/data_structures/chapter_3/polynomial.c create mode 100644 Computer_Science/data_structures/chapter_3/polynomial_in_list.c create mode 100644 Computer_Science/data_structures/chapter_3/stack.c create mode 100644 Computer_Science/data_structures/chapter_3/stack.h create mode 100644 Computer_Science/data_structures/chapter_3/stack.h.gch create mode 100644 Computer_Science/data_structures/chapter_3/stack_array.c create mode 100644 Computer_Science/data_structures/chapter_3/stack_array.h create mode 100644 Computer_Science/leetcode/1_two_sum.c create mode 100755 Computer_Science/leetcode/a.out create mode 100644 Computer_Science/scala/Scala.pdf delete mode 100644 data_structures/Data Structure and Algorithm Analysis in C.pdf delete mode 100644 data_structures/Data Structures and algorithm analysis in C 2nd version.pdf delete mode 100644 data_structures/chapter_3/.polynomial_in_list.c.swp delete mode 100755 data_structures/chapter_3/a.out delete mode 100644 data_structures/chapter_3/linked_list.c delete mode 100644 data_structures/chapter_3/linked_list.h delete mode 100644 data_structures/chapter_3/polynomial.c delete mode 100644 data_structures/chapter_3/polynomial_in_list.c delete mode 100644 data_structures/chapter_3/stack.c delete mode 100644 data_structures/chapter_3/stack.h delete mode 100644 data_structures/chapter_3/stack.h.gch delete mode 100644 data_structures/chapter_3/stack_array.c delete mode 100644 data_structures/chapter_3/stack_array.h delete mode 100644 leetcode/1_two_sum.c delete mode 100755 leetcode/a.out delete mode 100644 scala/Scala.pdf diff --git a/Computer_Science/data_structures/Data Structure and Algorithm Analysis in C.pdf b/Computer_Science/data_structures/Data Structure and Algorithm Analysis in C.pdf new file mode 100644 index 0000000..1ceabc5 Binary files /dev/null and b/Computer_Science/data_structures/Data Structure and Algorithm Analysis in C.pdf differ diff --git a/Computer_Science/data_structures/Data Structures and algorithm analysis in C 2nd version.pdf b/Computer_Science/data_structures/Data Structures and algorithm analysis in C 2nd version.pdf new file mode 100644 index 0000000..fba68dc Binary files /dev/null and b/Computer_Science/data_structures/Data Structures and algorithm analysis in C 2nd version.pdf differ diff --git a/Computer_Science/data_structures/chapter_3/.polynomial_in_list.c.swp b/Computer_Science/data_structures/chapter_3/.polynomial_in_list.c.swp new file mode 100644 index 0000000..5906611 Binary files /dev/null and b/Computer_Science/data_structures/chapter_3/.polynomial_in_list.c.swp differ diff --git a/Computer_Science/data_structures/chapter_3/a.out b/Computer_Science/data_structures/chapter_3/a.out new file mode 100755 index 0000000..185b597 Binary files /dev/null and b/Computer_Science/data_structures/chapter_3/a.out differ diff --git a/Computer_Science/data_structures/chapter_3/linked_list.c b/Computer_Science/data_structures/chapter_3/linked_list.c new file mode 100644 index 0000000..920be0d --- /dev/null +++ b/Computer_Science/data_structures/chapter_3/linked_list.c @@ -0,0 +1,138 @@ +#include +#include + +#include "linked_list.h" + +struct node +{ + elem data; + position next; +}; + +int is_empty(list header) +{ + return header->next == NULL; +} + +int is_last(position p, list header) +{ + return p->next == NULL; +} + +position find(elem x, list header) +{ + position p; + p = header->next; + + for(; p != NULL; p = p->next) { + if(p->data == x) return p; + } + + return NULL; +} + +void delete(elem x, list header) +{ + position tmp; + position pre; + + pre = find_previous(x, header); + + if(!is_last(pre, header)) { + tmp = pre->next; + pre->next = pre->next->next; + free(tmp); + } + +} + +position find_previous(elem x, list header) +{ + position p; + + p = header; + while(p->next != NULL && p->next->data != x) + p = p->next; + + return p; +} + +void insert(elem x, list header, position p) +{ + position tmp; + + tmp = malloc(sizeof(struct node)); + + if(tmp == NULL) + printf("Out of space!\n"); + + tmp->data = x; + tmp->next = p->next; + p->next = tmp; +} + +void delete_list(list header) +{ + position p, tmp; + + p = header->next; + header->next = NULL; + + while(p != NULL) { + tmp = p; + p = p->next; + free(tmp); + } +} + +void print_list(list header) +{ + position p; + + if(is_empty(header)) + printf("empty list! \n"); + + p = header->next; + for(; p != NULL; p = p->next) + printf("%d->", p->data); + printf("\n"); +} + +int test() +{ + position p1, p2, p3; + list l1, l2, l3, l4; + l1 = malloc(sizeof(struct node)); + l1->next == NULL; + + print_list(l1); + + /* test insert foo */ + insert(5, l1, l1); + insert(4, l1, l1); + insert(3, l1, l1); + insert(2, l1, l1); + insert(1, l1, l1); + print_list(l1); + + + /* test find foo */ + p1 = find(2, l1); + printf("%d\n", p1->next->data); + + /* test delete foo */ + delete(3, l1); + delete(5, l1); + delete(1, l1); + print_list(l1); + + /* test delete_list foo */ + delete_list(l1); + print_list(l1); +} + +int main() +{ + test(); + return 0; +} diff --git a/Computer_Science/data_structures/chapter_3/linked_list.h b/Computer_Science/data_structures/chapter_3/linked_list.h new file mode 100644 index 0000000..ecd9040 --- /dev/null +++ b/Computer_Science/data_structures/chapter_3/linked_list.h @@ -0,0 +1,23 @@ +#ifndef _LIST_H + +struct node; +typedef struct node *node_ptr; +typedef node_ptr list; +typedef node_ptr position; + +/* elem to int */ +typedef int elem; + +list make_empty(list header); +int is_empty(list header); +int is_last(position p, list header); +position find(elem x, list header); +void delete(elem x, list header); +position find_previous(elem x, list header); +void insert(elem x, list header, position p); +void delete_list(list header); +position header(list header); +position first(list header); +position advance(position p); + +#endif /* _LIST_H */ diff --git a/Computer_Science/data_structures/chapter_3/polynomial.c b/Computer_Science/data_structures/chapter_3/polynomial.c new file mode 100644 index 0000000..9028758 --- /dev/null +++ b/Computer_Science/data_structures/chapter_3/polynomial.c @@ -0,0 +1,103 @@ +#include +#include + +#define MAX_DEGREE 100 + +typedef struct Polynomial* polynomial; +struct Polynomial +{ + int coeff_array[MAX_DEGREE + 1]; + int high_power; +}; + +int max(int x, int y) +{ + return x > y ? x : y; +} + +void zero_polynomial(polynomial poly) +{ + int i; + + for(i = 0; i <= MAX_DEGREE; i++) + poly->coeff_array[i] = 0; + poly->high_power == 0; +} + +void add_polynomial(const polynomial poly1, const polynomial poly2, + polynomial poly_sum) +{ + int i; + + poly_sum->high_power = max(poly1->high_power, poly2->high_power); + + for(i = 0; i <= poly_sum->high_power; i++) { + poly_sum->coeff_array[i] = poly1->coeff_array[i] + + poly2->coeff_array[i]; + } +} + +void mult_polynomial(const polynomial poly1, const polynomial poly2, + polynomial poly_prod) +{ + int i, j; + + poly_prod->high_power = poly1->high_power + poly2->high_power; + + for(i = 0; i <= poly1->high_power; i++) { + for(j = 0; j<= poly2->high_power; j++) + /* += used here */ + poly_prod->coeff_array[i + j] += poly1->coeff_array[i] + * poly2->coeff_array[j]; + } +} + +void print_poly(const polynomial poly) +{ + int i; + + for(i = 0; i <= poly->high_power; i++) + if(poly->coeff_array[i] != 0) + printf("%dx^%d + ", poly->coeff_array[i], i); + + printf("\n"); +} + +void test() +{ + int i; + + polynomial poly1, poly2, poly_sum, poly_prod; + poly1 = malloc(sizeof(struct Polynomial)); + poly2 = malloc(sizeof(struct Polynomial)); + poly_sum = malloc(sizeof(struct Polynomial)); + poly_prod = malloc(sizeof(struct Polynomial)); + + for(i = 0; i <= 20; i++) { + if(i % 2 == 0) { + poly1->coeff_array[i] = i; + poly1->high_power = i; + } else { + poly2->coeff_array[i] = i; + poly2->high_power = i; + } + } + + print_poly(poly1); + print_poly(poly2); + + /* test sum */ + add_polynomial(poly1, poly2, poly_sum); + print_poly(poly_sum); + + /* test mult foo */ + mult_polynomial(poly1, poly2, poly_prod); + print_poly(poly_prod); +} + +int main() +{ + test(); + + return 0; +} diff --git a/Computer_Science/data_structures/chapter_3/polynomial_in_list.c b/Computer_Science/data_structures/chapter_3/polynomial_in_list.c new file mode 100644 index 0000000..0ecdf33 --- /dev/null +++ b/Computer_Science/data_structures/chapter_3/polynomial_in_list.c @@ -0,0 +1,36 @@ +#include +#include + +typedef struct node* ptr_to_node; + +struct node +{ + int coefficient; + int exponent; + ptr_to_node next; +}; + + +typedef ptr_to_node polynomial; + +void add_polynomial(polynomial poly1, polynomial poly2, + polynomial poly_sum) +{ +} + +void mult_polynomial(polynomial poly1, polynomial poly2, + polynomial poly_prod) +{ +} + +void print_poly(polynomial poly) +{ + polynomial p; + + p = poly->next; + + for(; p != NULL; p = p->next) + printf("%dx^%d + ", p->coefficient, p->exponent); + + printf("\n"); +} diff --git a/Computer_Science/data_structures/chapter_3/stack.c b/Computer_Science/data_structures/chapter_3/stack.c new file mode 100644 index 0000000..a27869f --- /dev/null +++ b/Computer_Science/data_structures/chapter_3/stack.c @@ -0,0 +1,120 @@ +#include +#include + +#include "stack.h" + +/* Stack implementation is a linked list with a header */ +struct node +{ + elem data; + ptr_to_node next; +}; + +int is_empty(stack s) +{ + return s->next == NULL; +} + +stack create_stack() +{ + stack s; + + s = malloc(sizeof(struct node)); + if(s == NULL) + printf("error"); + make_empty(s); + return s; +} + +void make_empty(stack s) +{ + if(s == NULL) + printf("must create a stack first"); + else + while(!is_empty(s)) + pop(s); +} + +void push(elem x, stack s) +{ + ptr_to_node tmp; + + tmp = malloc(sizeof(struct node)); + if(tmp == NULL) + printf("out of space"); + else { + tmp->data = x; + tmp->next = s->next; + s->next = tmp; + } +} + +void pop(stack s) +{ + if(!is_empty(s)) { + ptr_to_node tmp = s->next; + s->next = s->next->next; + free(tmp); + } else + printf("Empty stack"); +} + +elem top(stack s) +{ +// if(is_empty(s)) +// printf("empty stack"); +// else +// return s->next->data; +// +// return -1; +// +/* tune version */ + if(!is_empty(s)) + return s->next->data; + + printf("empty stack"); + return 0; +} + +void print_stack(stack s) +{ + printf("--------\n"); + while(!is_empty(s)) { + printf(" %d\n", s->next->data); + s = s->next; + } + printf("--------\n"); +} + +void test() +{ + stack s; + s = create_stack(); + push(1, s); + push(2, s); + print_stack(s); + + push(3, s); + push(4, s); + push(5, s); + print_stack(s); + printf("%d\n",top(s)); + + pop(s); + print_stack(s); + + pop(s); + pop(s); + pop(s); + pop(s); + pop(s); + pop(s); + + print_stack(s); +} + +int main() +{ + test(); + return 0; +} diff --git a/Computer_Science/data_structures/chapter_3/stack.h b/Computer_Science/data_structures/chapter_3/stack.h new file mode 100644 index 0000000..f93c467 --- /dev/null +++ b/Computer_Science/data_structures/chapter_3/stack.h @@ -0,0 +1,16 @@ +#ifndef _STACK_H + +struct node; +typedef struct node *ptr_to_node; +typedef ptr_to_node stack; +typedef int elem; + +int is_empty(stack s); +stack create_stack(); +void dispose_stack(stack s); +void make_empty(stack s); +void push(elem x, stack s); +elem top(stack s); +void pop(stack s); + +#endif diff --git a/Computer_Science/data_structures/chapter_3/stack.h.gch b/Computer_Science/data_structures/chapter_3/stack.h.gch new file mode 100644 index 0000000..4083944 Binary files /dev/null and b/Computer_Science/data_structures/chapter_3/stack.h.gch differ diff --git a/Computer_Science/data_structures/chapter_3/stack_array.c b/Computer_Science/data_structures/chapter_3/stack_array.c new file mode 100644 index 0000000..4c23334 --- /dev/null +++ b/Computer_Science/data_structures/chapter_3/stack_array.c @@ -0,0 +1,90 @@ +#include +#include + +#include "stack_array.h" + +#define EMPTY_TOS (-1); +#define MIN_STACK_SIZE (5); + +struct stack_record +{ + int capacity; + int top_of_stack; + elem *array; +} + +stack create_stack(int max_elements) +{ + stack s; + + if(max_elements < MIN_STACK_SIZE) + printf("size too small.\n"); + + s = malloc(sizeof(struct stack_record)); + if(s == NULL) + printf("out of space"); + + s->array = malloc(sizeof(elem) * max_elements); + + if(s->array == NULL) + printf("out of space"); + + s->capacity = max_elements; + make_empty(s); + + return s; +} + +void dispose_stack(stack s) +{ + if(s != NULL) { + free(s->array); + free(s); + } +} + +int is_empty(stack s) +{ + return s->top_of_stack == EMPTY_TOS; +} + +void make_empty(stack s) +{ + s->top_of_stack = EMPTY_TOS; +} + +void push(elem x, stack s) +{ + if(s->top_of_stack >= capacity) + printf("full stack"); + else + s->array[++s->top_of_stack] = x; + +} + +elem top(stack s) +{ + if(!is_empty(s)) + return s->array[s->top_of_stack]; + + printf("empty stack"); + return 0; +} + +void pop(stack s) +{ + if(!is_empty(s)) + s->top_of_stack--; + + printf("empty stack"); +} + +elem top_and_pop(stack s) +{ + if(!is_empty(s)) + /* take care of this, before return, s->top_of_stack is already dec. */ + return s->array[s->top_of_stack--]; + + printf("empty stack"); + return 0; +} diff --git a/Computer_Science/data_structures/chapter_3/stack_array.h b/Computer_Science/data_structures/chapter_3/stack_array.h new file mode 100644 index 0000000..088b0cc --- /dev/null +++ b/Computer_Science/data_structures/chapter_3/stack_array.h @@ -0,0 +1,16 @@ +#ifndef _STACK_H + +struct stack_record; +typedef struct stack_record *stack; +typedef int elem; + +int is_empty(stack s); +int is_full(stack s); +stack create_stack(int max_elements); +void dispose_stack(stack s); +void push(elem x, stack s); +elem top(stack s); +void pop(stack s); +elem top_and_pop(stack s); + +#endif diff --git a/Computer_Science/leetcode/1_two_sum.c b/Computer_Science/leetcode/1_two_sum.c new file mode 100644 index 0000000..fe29ecc --- /dev/null +++ b/Computer_Science/leetcode/1_two_sum.c @@ -0,0 +1,26 @@ +#include + +int result[2]; + +int* two_sum(int* nums, int nums_size, int target) +{ + int i, j; + for(i = 0; i < nums_size; i++) + for(j = 0; j < nums_size; j++) + if(nums[i] + nums[j] == target) { + printf("%d+%d = %d\n", nums[i], nums[j], target); + result[0] = i; + result[1] = j; + return result; + } + +} + +int main() +{ + int nums[4] = {2, 7, 11, 15}; + two_sum(nums, 4, 9); + printf("%d, %d\n",result[0], result[1]); + + return 0; +} diff --git a/Computer_Science/leetcode/a.out b/Computer_Science/leetcode/a.out new file mode 100755 index 0000000..0aa8167 Binary files /dev/null and b/Computer_Science/leetcode/a.out differ diff --git a/Computer_Science/scala/Scala.pdf b/Computer_Science/scala/Scala.pdf new file mode 100644 index 0000000..1ec4ea7 Binary files /dev/null and b/Computer_Science/scala/Scala.pdf differ diff --git a/data_structures/Data Structure and Algorithm Analysis in C.pdf b/data_structures/Data Structure and Algorithm Analysis in C.pdf deleted file mode 100644 index 1ceabc5..0000000 Binary files a/data_structures/Data Structure and Algorithm Analysis in C.pdf and /dev/null differ diff --git a/data_structures/Data Structures and algorithm analysis in C 2nd version.pdf b/data_structures/Data Structures and algorithm analysis in C 2nd version.pdf deleted file mode 100644 index fba68dc..0000000 Binary files a/data_structures/Data Structures and algorithm analysis in C 2nd version.pdf and /dev/null differ diff --git a/data_structures/chapter_3/.polynomial_in_list.c.swp b/data_structures/chapter_3/.polynomial_in_list.c.swp deleted file mode 100644 index 5906611..0000000 Binary files a/data_structures/chapter_3/.polynomial_in_list.c.swp and /dev/null differ diff --git a/data_structures/chapter_3/a.out b/data_structures/chapter_3/a.out deleted file mode 100755 index 185b597..0000000 Binary files a/data_structures/chapter_3/a.out and /dev/null differ diff --git a/data_structures/chapter_3/linked_list.c b/data_structures/chapter_3/linked_list.c deleted file mode 100644 index 920be0d..0000000 --- a/data_structures/chapter_3/linked_list.c +++ /dev/null @@ -1,138 +0,0 @@ -#include -#include - -#include "linked_list.h" - -struct node -{ - elem data; - position next; -}; - -int is_empty(list header) -{ - return header->next == NULL; -} - -int is_last(position p, list header) -{ - return p->next == NULL; -} - -position find(elem x, list header) -{ - position p; - p = header->next; - - for(; p != NULL; p = p->next) { - if(p->data == x) return p; - } - - return NULL; -} - -void delete(elem x, list header) -{ - position tmp; - position pre; - - pre = find_previous(x, header); - - if(!is_last(pre, header)) { - tmp = pre->next; - pre->next = pre->next->next; - free(tmp); - } - -} - -position find_previous(elem x, list header) -{ - position p; - - p = header; - while(p->next != NULL && p->next->data != x) - p = p->next; - - return p; -} - -void insert(elem x, list header, position p) -{ - position tmp; - - tmp = malloc(sizeof(struct node)); - - if(tmp == NULL) - printf("Out of space!\n"); - - tmp->data = x; - tmp->next = p->next; - p->next = tmp; -} - -void delete_list(list header) -{ - position p, tmp; - - p = header->next; - header->next = NULL; - - while(p != NULL) { - tmp = p; - p = p->next; - free(tmp); - } -} - -void print_list(list header) -{ - position p; - - if(is_empty(header)) - printf("empty list! \n"); - - p = header->next; - for(; p != NULL; p = p->next) - printf("%d->", p->data); - printf("\n"); -} - -int test() -{ - position p1, p2, p3; - list l1, l2, l3, l4; - l1 = malloc(sizeof(struct node)); - l1->next == NULL; - - print_list(l1); - - /* test insert foo */ - insert(5, l1, l1); - insert(4, l1, l1); - insert(3, l1, l1); - insert(2, l1, l1); - insert(1, l1, l1); - print_list(l1); - - - /* test find foo */ - p1 = find(2, l1); - printf("%d\n", p1->next->data); - - /* test delete foo */ - delete(3, l1); - delete(5, l1); - delete(1, l1); - print_list(l1); - - /* test delete_list foo */ - delete_list(l1); - print_list(l1); -} - -int main() -{ - test(); - return 0; -} diff --git a/data_structures/chapter_3/linked_list.h b/data_structures/chapter_3/linked_list.h deleted file mode 100644 index ecd9040..0000000 --- a/data_structures/chapter_3/linked_list.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef _LIST_H - -struct node; -typedef struct node *node_ptr; -typedef node_ptr list; -typedef node_ptr position; - -/* elem to int */ -typedef int elem; - -list make_empty(list header); -int is_empty(list header); -int is_last(position p, list header); -position find(elem x, list header); -void delete(elem x, list header); -position find_previous(elem x, list header); -void insert(elem x, list header, position p); -void delete_list(list header); -position header(list header); -position first(list header); -position advance(position p); - -#endif /* _LIST_H */ diff --git a/data_structures/chapter_3/polynomial.c b/data_structures/chapter_3/polynomial.c deleted file mode 100644 index 9028758..0000000 --- a/data_structures/chapter_3/polynomial.c +++ /dev/null @@ -1,103 +0,0 @@ -#include -#include - -#define MAX_DEGREE 100 - -typedef struct Polynomial* polynomial; -struct Polynomial -{ - int coeff_array[MAX_DEGREE + 1]; - int high_power; -}; - -int max(int x, int y) -{ - return x > y ? x : y; -} - -void zero_polynomial(polynomial poly) -{ - int i; - - for(i = 0; i <= MAX_DEGREE; i++) - poly->coeff_array[i] = 0; - poly->high_power == 0; -} - -void add_polynomial(const polynomial poly1, const polynomial poly2, - polynomial poly_sum) -{ - int i; - - poly_sum->high_power = max(poly1->high_power, poly2->high_power); - - for(i = 0; i <= poly_sum->high_power; i++) { - poly_sum->coeff_array[i] = poly1->coeff_array[i] - + poly2->coeff_array[i]; - } -} - -void mult_polynomial(const polynomial poly1, const polynomial poly2, - polynomial poly_prod) -{ - int i, j; - - poly_prod->high_power = poly1->high_power + poly2->high_power; - - for(i = 0; i <= poly1->high_power; i++) { - for(j = 0; j<= poly2->high_power; j++) - /* += used here */ - poly_prod->coeff_array[i + j] += poly1->coeff_array[i] - * poly2->coeff_array[j]; - } -} - -void print_poly(const polynomial poly) -{ - int i; - - for(i = 0; i <= poly->high_power; i++) - if(poly->coeff_array[i] != 0) - printf("%dx^%d + ", poly->coeff_array[i], i); - - printf("\n"); -} - -void test() -{ - int i; - - polynomial poly1, poly2, poly_sum, poly_prod; - poly1 = malloc(sizeof(struct Polynomial)); - poly2 = malloc(sizeof(struct Polynomial)); - poly_sum = malloc(sizeof(struct Polynomial)); - poly_prod = malloc(sizeof(struct Polynomial)); - - for(i = 0; i <= 20; i++) { - if(i % 2 == 0) { - poly1->coeff_array[i] = i; - poly1->high_power = i; - } else { - poly2->coeff_array[i] = i; - poly2->high_power = i; - } - } - - print_poly(poly1); - print_poly(poly2); - - /* test sum */ - add_polynomial(poly1, poly2, poly_sum); - print_poly(poly_sum); - - /* test mult foo */ - mult_polynomial(poly1, poly2, poly_prod); - print_poly(poly_prod); -} - -int main() -{ - test(); - - return 0; -} diff --git a/data_structures/chapter_3/polynomial_in_list.c b/data_structures/chapter_3/polynomial_in_list.c deleted file mode 100644 index 0ecdf33..0000000 --- a/data_structures/chapter_3/polynomial_in_list.c +++ /dev/null @@ -1,36 +0,0 @@ -#include -#include - -typedef struct node* ptr_to_node; - -struct node -{ - int coefficient; - int exponent; - ptr_to_node next; -}; - - -typedef ptr_to_node polynomial; - -void add_polynomial(polynomial poly1, polynomial poly2, - polynomial poly_sum) -{ -} - -void mult_polynomial(polynomial poly1, polynomial poly2, - polynomial poly_prod) -{ -} - -void print_poly(polynomial poly) -{ - polynomial p; - - p = poly->next; - - for(; p != NULL; p = p->next) - printf("%dx^%d + ", p->coefficient, p->exponent); - - printf("\n"); -} diff --git a/data_structures/chapter_3/stack.c b/data_structures/chapter_3/stack.c deleted file mode 100644 index a27869f..0000000 --- a/data_structures/chapter_3/stack.c +++ /dev/null @@ -1,120 +0,0 @@ -#include -#include - -#include "stack.h" - -/* Stack implementation is a linked list with a header */ -struct node -{ - elem data; - ptr_to_node next; -}; - -int is_empty(stack s) -{ - return s->next == NULL; -} - -stack create_stack() -{ - stack s; - - s = malloc(sizeof(struct node)); - if(s == NULL) - printf("error"); - make_empty(s); - return s; -} - -void make_empty(stack s) -{ - if(s == NULL) - printf("must create a stack first"); - else - while(!is_empty(s)) - pop(s); -} - -void push(elem x, stack s) -{ - ptr_to_node tmp; - - tmp = malloc(sizeof(struct node)); - if(tmp == NULL) - printf("out of space"); - else { - tmp->data = x; - tmp->next = s->next; - s->next = tmp; - } -} - -void pop(stack s) -{ - if(!is_empty(s)) { - ptr_to_node tmp = s->next; - s->next = s->next->next; - free(tmp); - } else - printf("Empty stack"); -} - -elem top(stack s) -{ -// if(is_empty(s)) -// printf("empty stack"); -// else -// return s->next->data; -// -// return -1; -// -/* tune version */ - if(!is_empty(s)) - return s->next->data; - - printf("empty stack"); - return 0; -} - -void print_stack(stack s) -{ - printf("--------\n"); - while(!is_empty(s)) { - printf(" %d\n", s->next->data); - s = s->next; - } - printf("--------\n"); -} - -void test() -{ - stack s; - s = create_stack(); - push(1, s); - push(2, s); - print_stack(s); - - push(3, s); - push(4, s); - push(5, s); - print_stack(s); - printf("%d\n",top(s)); - - pop(s); - print_stack(s); - - pop(s); - pop(s); - pop(s); - pop(s); - pop(s); - pop(s); - - print_stack(s); -} - -int main() -{ - test(); - return 0; -} diff --git a/data_structures/chapter_3/stack.h b/data_structures/chapter_3/stack.h deleted file mode 100644 index f93c467..0000000 --- a/data_structures/chapter_3/stack.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef _STACK_H - -struct node; -typedef struct node *ptr_to_node; -typedef ptr_to_node stack; -typedef int elem; - -int is_empty(stack s); -stack create_stack(); -void dispose_stack(stack s); -void make_empty(stack s); -void push(elem x, stack s); -elem top(stack s); -void pop(stack s); - -#endif diff --git a/data_structures/chapter_3/stack.h.gch b/data_structures/chapter_3/stack.h.gch deleted file mode 100644 index 4083944..0000000 Binary files a/data_structures/chapter_3/stack.h.gch and /dev/null differ diff --git a/data_structures/chapter_3/stack_array.c b/data_structures/chapter_3/stack_array.c deleted file mode 100644 index 4c23334..0000000 --- a/data_structures/chapter_3/stack_array.c +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include - -#include "stack_array.h" - -#define EMPTY_TOS (-1); -#define MIN_STACK_SIZE (5); - -struct stack_record -{ - int capacity; - int top_of_stack; - elem *array; -} - -stack create_stack(int max_elements) -{ - stack s; - - if(max_elements < MIN_STACK_SIZE) - printf("size too small.\n"); - - s = malloc(sizeof(struct stack_record)); - if(s == NULL) - printf("out of space"); - - s->array = malloc(sizeof(elem) * max_elements); - - if(s->array == NULL) - printf("out of space"); - - s->capacity = max_elements; - make_empty(s); - - return s; -} - -void dispose_stack(stack s) -{ - if(s != NULL) { - free(s->array); - free(s); - } -} - -int is_empty(stack s) -{ - return s->top_of_stack == EMPTY_TOS; -} - -void make_empty(stack s) -{ - s->top_of_stack = EMPTY_TOS; -} - -void push(elem x, stack s) -{ - if(s->top_of_stack >= capacity) - printf("full stack"); - else - s->array[++s->top_of_stack] = x; - -} - -elem top(stack s) -{ - if(!is_empty(s)) - return s->array[s->top_of_stack]; - - printf("empty stack"); - return 0; -} - -void pop(stack s) -{ - if(!is_empty(s)) - s->top_of_stack--; - - printf("empty stack"); -} - -elem top_and_pop(stack s) -{ - if(!is_empty(s)) - /* take care of this, before return, s->top_of_stack is already dec. */ - return s->array[s->top_of_stack--]; - - printf("empty stack"); - return 0; -} diff --git a/data_structures/chapter_3/stack_array.h b/data_structures/chapter_3/stack_array.h deleted file mode 100644 index 088b0cc..0000000 --- a/data_structures/chapter_3/stack_array.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef _STACK_H - -struct stack_record; -typedef struct stack_record *stack; -typedef int elem; - -int is_empty(stack s); -int is_full(stack s); -stack create_stack(int max_elements); -void dispose_stack(stack s); -void push(elem x, stack s); -elem top(stack s); -void pop(stack s); -elem top_and_pop(stack s); - -#endif diff --git a/leetcode/1_two_sum.c b/leetcode/1_two_sum.c deleted file mode 100644 index fe29ecc..0000000 --- a/leetcode/1_two_sum.c +++ /dev/null @@ -1,26 +0,0 @@ -#include - -int result[2]; - -int* two_sum(int* nums, int nums_size, int target) -{ - int i, j; - for(i = 0; i < nums_size; i++) - for(j = 0; j < nums_size; j++) - if(nums[i] + nums[j] == target) { - printf("%d+%d = %d\n", nums[i], nums[j], target); - result[0] = i; - result[1] = j; - return result; - } - -} - -int main() -{ - int nums[4] = {2, 7, 11, 15}; - two_sum(nums, 4, 9); - printf("%d, %d\n",result[0], result[1]); - - return 0; -} diff --git a/leetcode/a.out b/leetcode/a.out deleted file mode 100755 index 0aa8167..0000000 Binary files a/leetcode/a.out and /dev/null differ diff --git a/scala/Scala.pdf b/scala/Scala.pdf deleted file mode 100644 index 1ec4ea7..0000000 Binary files a/scala/Scala.pdf and /dev/null differ -- cgit v1.2.3