diff --git a/semester2/challenge1/main.c b/semester2/challenge1/main.c new file mode 100644 index 0000000..102fa4b --- /dev/null +++ b/semester2/challenge1/main.c @@ -0,0 +1,194 @@ +#include +#include +#include + +#define NUM_USERS 611 +#define NUM_MOVIES 9743 +#define NUM_GENRES 20 + +typedef struct Movies +{ + int movieID; + struct Movies *next; +} Movies; + +typedef struct Genres +{ + int genreID; + struct Genres *next; +} Genres; + +//输入电影风格返回其映射的id +int movie_style_map(char *style) +{ + + int index = 0; + const char *movies[] = {"Action", "Adventure", "Animation", "Children's", + "Comedy", "Crime", "Documentary", "Drama", + "Fantasy", "Film-Noir", "Horror", "IMAX", "Musical", + "Mystery", "Romance", "Sci-Fi", "Thriller", + "War", "Western"}; + + while (strcmp(movies[index], style) != 0) + { + index++; + } + return index; +} + +// 函数原型 +Movies *appendMovies(Movies *last, int id); +int getGenreID(int movieID, Genres ** movies); // 传入movieID,输出风格下标 +void clearArrInt(int* arr, int size); // 将所有元素设为0 + +int main(void) +{ + // Firstly open the ratings.csv + FILE *fp; + fp = fopen("./ratings.csv", "r"); + + char *buf = malloc(256 * sizeof(char)); + + // Declare the users table and init with null. + Movies *users[NUM_USERS]; + for (int i = 0; i < NUM_USERS; i++) + { + users[i] = NULL; + } + + fgets(buf, 256 * sizeof(char), fp); + + // 看每个用户喜欢什么电影 + int userID, movieID; + char input[64]; + int loc, loc1; + while (fgets(buf, 256 * sizeof(char), fp) != NULL) + { + loc = 0; + input[0] = '\0'; // clear the array + // get userID + while (buf[loc] != ',') + { + input[loc] = buf[loc]; + loc++; + } + input[loc++] = '\0'; + userID = atoi(input); + // printf("UserID: %d\n", userID); + + // get movieID + loc1 = 0; + input[0] = '\0'; + while (buf[loc] != ',') + { + input[loc1++] = buf[loc]; + loc++; + } + input[loc1] = '\0'; + movieID = atoi(input); + // printf("movieID: %d\n", movieID); + users[userID] = appendMovies(users[userID], movieID); + } + + fclose(fp); + + fp = fopen("./movies.csv", 'r'); + + // 下标表示电影id + Genres *movies[NUM_MOVIES]; + + + // TODO: 仿照我的代码填充*movies数组 + // 李王子写 ok 你写吧 + // + + // + for(int i=0;imovieID, movies)] ++; + ptr = ptr->next; + } + } + + //那个 就是你帮我写的那个数组 我感觉开个一维数组就可以了啊 + + fclose(fp); + + return 0; +} + +Movies *appendMovies(Movies *last, int id) +{ + Movies *new = malloc(sizeof(Movies)); + + new->movieID = id; + new->next = NULL; + + // 1.如果是null + if (last == NULL) + { + return new; + } + // 2.不是null,在最后加上新节点 + Movies *ptr = last; + + while (ptr->next != NULL) + { + ptr = ptr->next; + } + + ptr->next = new; + + return last; +} + + +int getGenreID(int movieID, Genres **movies) { + Genres *ptr = movies[movieID]; +} + +void clearArrInt(int* arr, int size) { + for (int i = 0; i < size; i++) { + arr[i] = 0; + } +} \ No newline at end of file diff --git a/semester2/pset2/3-subLinkedList.c b/semester2/pset2/3-subLinkedList.c new file mode 100644 index 0000000..d7ba740 --- /dev/null +++ b/semester2/pset2/3-subLinkedList.c @@ -0,0 +1,99 @@ +#include +#include + +typedef struct Node { + int data; + struct Node *next; +} Node; + +Node *constructLinkedList(); +Node *appendLinkedList(Node *ptr, int data); +void deleteLinkedList(Node *head); + +int main(void) { + Node *listA = constructLinkedList(); + Node *listB = constructLinkedList(); + + Node *ptrA = listA, *ptrB, *ptrC; + + int matched = 0; + + while (ptrA != NULL) { + matched = 0; + ptrB = listB; + + if (ptrA->data == ptrB->data) { + matched = 1; + ptrC = ptrA; + + while (ptrB != NULL) { + if (ptrC == NULL) { + matched = 0; + break; + } + if (ptrC->data != ptrB->data) { + matched = 0; + break; + } + ptrB = ptrB->next; + ptrC = ptrC->next; + + } + + } + + if (matched == 1) { + break; + } + + ptrA = ptrA->next; + } + + if (matched) { + printf("ListB is the sub sequence of ListA."); + } else { + printf("ListB is not the sub sequence of ListA."); + } + + deleteLinkedList(listA); + deleteLinkedList(listB); + return 0; +} + +Node *constructLinkedList() { + Node *head = malloc(sizeof(struct Node)); + int buf; + scanf("%d", &buf); + + head->data = buf; + head->next = NULL; + Node *ptr = head; + + scanf("%d", &buf); + while (buf != -1) { + ptr = appendLinkedList(ptr, buf); + scanf("%d", &buf); + } + + return head; +} + +Node *appendLinkedList(Node *ptr, int data) { + Node *new = malloc(sizeof(Node)); + + new->data = data; + new->next = NULL; + + ptr->next = new; + + return new; +} + +void deleteLinkedList(Node *head) { + if (head == NULL) { + return; + } else { + deleteLinkedList(head->next); + free(head); + } +} diff --git a/semester2/pset2/4-splitNShift.c b/semester2/pset2/4-splitNShift.c new file mode 100644 index 0000000..5608fc0 --- /dev/null +++ b/semester2/pset2/4-splitNShift.c @@ -0,0 +1,154 @@ +#include +#include + +typedef struct Node { + int data; + struct Node *next; +} Node; + +Node *constructLinkedList(); +Node *appendLinkedList(Node *node, int data); +void deleteLinkedList(Node *head); +Node *segmentLinkedList(Node *head, Node **nextHead, int segLen); +// returns the head of segmented Linked list, writes the head of next linked +// list to *nextHead +void connectLinkedList(Node *head1, Node *head2); +// appends list2 to the end of list1 +void printLinkedList(Node *head); + +int main(void) { + Node *head = constructLinkedList(); + /* printLinkedList(head); */ + + int s1, t1, s2, t2; + scanf("%d%d%d%d", &s1, &t1, &s2, &t2); + + Node *tmp; + Node *ptr = head; + // start to iterate to s1 + for (int i = 1; i < s1; i++) { + if (i == s1 - 1) { + tmp = ptr->next; + ptr->next = NULL; + ptr = tmp; + } else { + ptr = ptr->next; + } + } + Node *seg1, *seg2 = NULL, *seg3, *seg4 = NULL; + // get the first segment + seg1 = segmentLinkedList(ptr, &seg2, t1 - s1); + ptr = seg2; + + for (int i = 0; i < s2 - t1 - 1; i++) { + if (i == s2 - t1 - 2) { + tmp = (Node*)(ptr->next); + ptr->next = NULL; + ptr = tmp; + } else { + ptr = ptr->next; + } + } + seg3 = segmentLinkedList(ptr, &seg4, t2 - s2); + + // special cases: + // head == seg1 + // seg2 == seg3 + // seg4 == NULL + + Node *newHead = head; + + if (head != seg1) { + connectLinkedList(head, seg3); + } else { + newHead = seg3; + } + if (seg2 != seg3) { + connectLinkedList(seg3, seg2); + } + connectLinkedList(seg2, seg1); + if (seg4 != NULL) { + connectLinkedList(seg1, seg4); + } + + printf("The new list is:"); + printLinkedList(newHead); + + deleteLinkedList(newHead); + return 0; +} + +Node *constructLinkedList() { + int buf; + Node *head = malloc(sizeof(Node)); + scanf("%d", &buf); + head->data = buf; + head->next = NULL; + + Node *ptr = head; + + scanf("%d", &buf); + while (buf != -1) { + ptr = appendLinkedList(ptr, buf); + scanf("%d", &buf); + } + + return head; +} + +Node *appendLinkedList(Node *node, int data) { + Node *new = malloc(sizeof(Node)); + + new->next = NULL; + new->data = data; + + node->next = new; + + return new; +} + +void deleteLinkedList(Node *head) { + if (head == NULL) { + return; + } else { + deleteLinkedList(head->next); + free(head); + } +} + +Node *segmentLinkedList(Node *head, Node **nextHead, int segLen) { + Node *ptr = head; + for (int i = 0; i < segLen; i++) { + ptr = ptr->next; + } + + if (ptr->next != NULL) { + *nextHead = ptr->next; + } + ptr->next = NULL; + + return head; +} + +void connectLinkedList(Node *head1, Node *head2) { + Node *ptr = head1; + + while (ptr->next != NULL) { + ptr = ptr->next; + } + + ptr->next = head2; +} + +void printLinkedList(Node *head) { + if (head != NULL) + printf("%d", head->data); + + Node *ptr = head; + ptr = head->next; + + while (ptr != NULL) { + printf(" %d", ptr->data); + ptr = ptr->next; + } +}