From d85fc37aca4f2375880406602291082b0b30e8ad Mon Sep 17 00:00:00 2001 From: juan Date: Tue, 8 Mar 2022 14:55:11 +0800 Subject: [PATCH] New semester! --- semester2/pset1/1-linkedList.c | 67 +++++++++++++++++++ semester2/pset1/2-reverseLinkedList.c | 96 +++++++++++++++++++++++++++ semester2/pset1/README.md | 5 ++ 3 files changed, 168 insertions(+) create mode 100644 semester2/pset1/1-linkedList.c create mode 100644 semester2/pset1/2-reverseLinkedList.c create mode 100644 semester2/pset1/README.md diff --git a/semester2/pset1/1-linkedList.c b/semester2/pset1/1-linkedList.c new file mode 100644 index 0000000..e10e4a3 --- /dev/null +++ b/semester2/pset1/1-linkedList.c @@ -0,0 +1,67 @@ +#include +#include + +typedef struct Node { + int data; + struct Node *next; +} Node; + +Node *appendNode(Node *n) { + int data; + scanf("%d", &data); + Node *node = malloc(sizeof(struct Node)); + node->data = data; + node->next = NULL; + n->next = node; + return node; +} + +Node *makeList(int col) { + Node *head, *ptr; + head = malloc(sizeof(Node)); + head->next = NULL; + + ptr = head; + for (int i = 0; i < col; i++) { + ptr = appendNode(ptr); + } + return head; +} + +void delete(Node *head) { + if (head->next == NULL) { + free(head); + } else { + delete(head->next); + } +} + +int main(void) { + int rows; + int col; + int count; + scanf("%d", &rows); + Node *ptr; + + for (int i = 0; i < rows; i++) { + scanf("%d", &col); + Node *head = makeList(col); + + count = 0; + for (ptr = head->next; ptr != NULL; ptr = ptr->next) { + if (count % 2 == 1) { + if (count == 1) { + printf("%d", ptr->data); + } else { + printf(" %d", ptr->data); + } + } + count++; + } + printf("\n"); + + delete(head); + } + + return 0; +} diff --git a/semester2/pset1/2-reverseLinkedList.c b/semester2/pset1/2-reverseLinkedList.c new file mode 100644 index 0000000..68daf3f --- /dev/null +++ b/semester2/pset1/2-reverseLinkedList.c @@ -0,0 +1,96 @@ +#include +#include + +typedef struct Node { + struct Node *next; + struct Node *prev; + int data; +} Node; + +// Returns the head of the linked list +Node *newLinkedList(); +// Returns a pointer to the next node +Node *appendNode(Node *prev); +// Prints the linked list in reverse order +void printListReverse(Node *head); +// Frees the list from memory +void freeList(Node *head); + +int main(void) { + // Get rows of input + int rows; + scanf("%d", &rows); + + Node *head; + for (int i = 0; i < rows; i++) { + head = newLinkedList(); + printListReverse(head); + freeList(head); + } + + return 0; +} + +Node *newLinkedList() { + Node *ptr, *head; + + // Initialize head + head = malloc(sizeof(Node)); + head->prev = NULL; + head->next = NULL; + + ptr = head; + + // Initialize list + int data; + scanf("%d", &data); + while (data != -1) { + ptr->data = data; + ptr = appendNode(ptr); + scanf("%d", &data); + } + + return head; +} + +Node *appendNode(Node *prev) { + Node *node; + node = malloc(sizeof(Node)); + + prev->next = node; + + node->prev = prev; + node->next = NULL; + + return node; +} + +void printListReverse(Node *head) { + // Can't use recursion because the output should be formatted + Node *ptr = head; + while (ptr->next != NULL) { + ptr = ptr->next; + } + // ptr now points to the tail. + + ptr = ptr->prev; + if (ptr != NULL) { + printf("%d", ptr->data); + ptr = ptr->prev; + } + while (ptr != NULL) { + printf(" %d", ptr->data); + ptr = ptr->prev; + } + + printf("\n"); +} + +void freeList(Node *head) { + if (head->next != NULL) { + freeList(head->next); + free(head); + } else { + free(head); + } +} diff --git a/semester2/pset1/README.md b/semester2/pset1/README.md new file mode 100644 index 0000000..1e63f51 --- /dev/null +++ b/semester2/pset1/README.md @@ -0,0 +1,5 @@ +# Pset 1 + +## Info + +the problem set can be found [here](https://pintia.cn/problem-sets/1499923457155141632)