diff --git a/Chapter11/ds/line/list/linklist/simple/josephu/jose.c b/Chapter11/ds/line/list/linklist/simple/josephu/jose.c new file mode 100644 index 0000000..3fa1587 --- /dev/null +++ b/Chapter11/ds/line/list/linklist/simple/josephu/jose.c @@ -0,0 +1,117 @@ +#include +#include + +#define JOSE_NUM 8 + +typedef struct node_st +{ + int data; + struct node_st *next; +} list; + +// try something +#if 0 +list *jose_create(int a[], int n) +{ + list *me, *p; + me = malloc(sizeof(*me)); + if (NULL == me) + return NULL; + + for (int i = 0; i < n; i++) + { + me->data = me; + + p = malloc(sizeof(*p)); + p = me; + p->data = a[i]; + me->next = p; + } + + return me; +} +#endif + +list *jose_create(int n) +{ + list *me, *newnode, *cur; + int i = 1; + + me = malloc(sizeof(*me)); + if (NULL == me) + return NULL; + me->data = i; + me->next = me; + i++; + + cur = me; + + for (; i <= n; i++) + { + newnode = malloc(sizeof(*newnode)); + if (NULL == newnode) + return NULL; + newnode->data = i; + newnode->next = me; + + cur->next = newnode; + cur = newnode; + } + + return me; +} + +void jose_show(list *me) +{ + list *list; + + for (list = me; list->next != me; list = list->next) + { + // sleep(1); + printf("%d ", list->data); + // fflush(NULL); + } + printf("%d\n", list->data); +} + +void jose_kill(list **me, int n) +{ + list *cur = *me, *node; + int i = 1; + + while (cur != cur->next) + { + while (i < n) + { + node = cur; + cur = cur->next; + i++; + } + + printf("%d ", cur->data); + node->next = cur->next; + free(cur); + + cur = node->next; + i = 1; + } + + *me = cur; + + printf("\n"); +} + +int main() +{ + list *list; + + list = jose_create(JOSE_NUM); + + jose_show(list); + + jose_kill(&list, 3); + + jose_show(list); + + exit(0); +} \ No newline at end of file diff --git a/Chapter11/ds/line/list/linklist/simple/polynomial/poly.c b/Chapter11/ds/line/list/linklist/simple/polynomial/poly.c new file mode 100644 index 0000000..c587baf --- /dev/null +++ b/Chapter11/ds/line/list/linklist/simple/polynomial/poly.c @@ -0,0 +1,114 @@ +#include +#include + +typedef struct node_st +{ + int coef; + int exp; + struct node_st *next; +} list; + +list *poly_create(int a[][2], int n) +{ + list *me, *newnode, *cur; + + me = malloc(sizeof(me)); + if (NULL == me) + return NULL; + me->next = NULL; + + cur = me; + + for (int i = 0; i < n; i++) + { + newnode = malloc(sizeof(*newnode)); + if (NULL == newnode) + return NULL; + + newnode->coef = a[i][0]; + newnode->exp = a[i][1]; + newnode->next = NULL; + + cur->next = newnode; + cur = newnode; + } + + return me; +} + +void poly_show(list *me) +{ + list *cur; + + for (cur = me->next; NULL != cur; cur = cur->next) + { + printf("(%d %d) ", cur->coef, cur->exp); + } + printf("\n"); +} + +void poly_union(list *p1, list *p2) +{ + list *p, *q, *r; + + p = p1->next; + q = p2->next; + r = p1; + + while (p && q) + { + if (p->exp < q->exp) + { + r->next = p; + r = p; + p = p->next; + } + else if (p->exp > q->exp) + { + r->next = q; + r = q; + q = q->next; + } + else + { + p->coef += q->coef; + if (p->coef) + { + r->next = p; + r = p; + } + + p = p->next; + q = q->next; + } + } + + if (NULL == p) + r->next = q; + else + r->next = p; +} + +int main() +{ + int a[][2] = {{5, 0}, {2, 1}, {8, 8}, {3, 16}}; + int b[][2] = {{6, 1}, {16, 6}, {-8, 8}}; + + list *p1, *p2; + + p1 = poly_create(a, 4); + if (NULL == p1) + exit(1); + + p2 = poly_create(b, 3); + if (NULL == p2) + exit(1); + + poly_show(p1); + poly_show(p2); + + poly_union(p1, p2); + poly_show(p1); + + exit(0); +} \ No newline at end of file