diff --git a/2.c b/2.c index 67c49c2..1f35bb2 100644 --- a/2.c +++ b/2.c @@ -82,9 +82,10 @@ int main() { delete (&head, 2); reverse(&head); - while (head != NULL) { - printf("%d ", head->sid); - head = head->next; + student *cur = head; + while (cur != NULL) { + printf("%d ", cur->sid); + cur = cur->next; } // free diff --git a/3.c b/3.c new file mode 100644 index 0000000..e12f5aa --- /dev/null +++ b/3.c @@ -0,0 +1,76 @@ +#include +#include + +typedef struct Employee employee; +struct Employee { + int eid; + struct Employee *next; + struct Employee *prev; +}; + +typedef struct Employees employees; +struct Employees { + uint len; + struct Employee *head; + struct Employee *tail; +}; + +employees *init() { + employees *list = malloc(sizeof(employees)); + list->len = 0; + list->head = NULL; + list->tail = NULL; + + return list; +} + +void insert(employees *list, uint eid) { + employee *new = malloc(sizeof(employee)); + new->eid = eid; + new->prev = NULL; + new->next = list->head; + + if (list->head) + list->head->prev = new; + else + list->tail = new; + + list->head = new; + list->len++; +} + +void delete (employees *list) { + employee *prev = list->tail->prev; + + free(list->tail); + + if (prev) { + prev->next = NULL; + if (prev->prev == NULL) + list->head = prev; + } + + list->tail = prev; + list->len--; +} + +int main() { + employees *list = init(); + insert(list, 4); + insert(list, 20); + insert(list, 300); + insert(list, 44); + delete (list); + + employee *cur = list->head; + while (cur != NULL) { + printf("%d ", cur->eid); + cur = cur->next; + } + + // free + while (list->tail != NULL) + delete (list); + + free(list); +} diff --git a/3.png b/3.png new file mode 100644 index 0000000..29e18cd Binary files /dev/null and b/3.png differ diff --git a/5.c b/5.c new file mode 100644 index 0000000..5b08256 --- /dev/null +++ b/5.c @@ -0,0 +1,72 @@ +#include +#include + +typedef struct Stacks stacks; +struct Stacks { + int *stack; + uint s1, s2; + uint l1, l2; +}; + +stacks *init(uint s1, uint s2) { + stacks *s = malloc(sizeof(stacks)); + s->stack = malloc(sizeof(int) * (s1 + s2)); + s->s1 = s1, s->s2 = s2; + s->l1 = s->l2 = 0; + + return s; +} + +void push(uint stack, stacks *s, int elem) { + if (stack != 1 && stack != 2) + fprintf(stderr, "Invalid stack number\n"); + + if (stack == 1) { + if (s->l1 == s->s1) { + fprintf(stderr, "Stack 1 Overflow"); + return; + } + s->stack[s->l1++] = elem; + return; + } + + if (s->l2 == s->s2) { + fprintf(stderr, "Stack 2 Overflow"); + return; + } + s->stack[s->s1 + s->l2++] = elem; +} + +int pop(uint stack, stacks *s) { + if (stack != 1 && stack != 2) + fprintf(stderr, "Invalid stack number\n"); + + if (stack == 1) { + if (!s->l1) { + fprintf(stderr, "Stack 1 Underflow"); + exit(1); + } + return s->stack[--s->l1]; + } + + if (!s->l2) { + fprintf(stderr, "Stack 2 Underflow"); + exit(1); + } + return s->stack[s->s1 + --s->l2]; +} + +int main() { + int s1 = 8, s2 = 4; // sizes + stacks *s = init(s1, s2); + push(1, s, 3); + push(1, s, 99); + push(2, s, 6); + push(1, s, 4); + + printf("%d ", pop(1, s)); + printf("%d", pop(2, s)); + + free(s->stack); + free(s); +} diff --git a/5.png b/5.png new file mode 100644 index 0000000..c77221e Binary files /dev/null and b/5.png differ diff --git a/6.c b/6.c new file mode 100644 index 0000000..2b77b1e --- /dev/null +++ b/6.c @@ -0,0 +1,48 @@ +#include +#include + +typedef struct Node node; +struct Node { + int id; + struct Node *next; +}; + +int peek(node *stack) { return stack->id; } + +void push(node **stack, int id) { + node *new = malloc(sizeof(node)); + new->id = id; + + new->next = *stack; + *stack = new; +} + +int pop(node **stack) { + int id = (*stack)->id; + node *next = (*stack)->next; + free(*stack); + *stack = next; + return id; +} + +void traverse(node *stack) { + while (stack != NULL) { + printf("%d ", stack->id); + stack = stack->next; + } + printf("\n"); +} + +int main() { + node *stack = NULL; + push(&stack, 4); + push(&stack, 99); + push(&stack, 23); + printf("Pop: %d\n", pop(&stack)); + printf("Traverseal: "); + traverse(stack); + + // free + while (stack != NULL) + pop(&stack); +} diff --git a/6.png b/6.png new file mode 100644 index 0000000..7388627 Binary files /dev/null and b/6.png differ diff --git a/7.c b/7.c new file mode 100644 index 0000000..471c022 --- /dev/null +++ b/7.c @@ -0,0 +1,66 @@ +#include +#include + +typedef struct Node node; +struct Node { + int id; + struct Node *next; +}; + +typedef struct Queue queue; +struct Queue { + node *head; + node *tail; +}; + +int peek(queue *q) { return q->head->id; } + +void insert(queue *q, int id) { + node *new = malloc(sizeof(node)); + new->id = id; + new->next = NULL; + + if (q->tail) + q->tail->next = new; + else + q->head = new; + + q->tail = new; +} + +void delete (queue *q) { + if (!q->head) + return; + + node *next = q->head->next; + + free(q->head); + + q->head = next; + + if (!q->head) + q->tail = NULL; +} + +void display(queue *q) { + node *cur = q->head; + while (cur != NULL) { + printf("%d ", cur->id); + cur = cur->next; + } + printf("\n"); +} + +int main() { + queue *q = malloc(sizeof(queue)); + insert(q, 56); + insert(q, 3); + insert(q, 99); + delete(q); + display(q); + + // free + while (q->head != NULL) + delete(q); + free(q); +} diff --git a/7.png b/7.png new file mode 100644 index 0000000..730f0cd Binary files /dev/null and b/7.png differ