为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 100
讨论题

jelasin 的学生作业:

//SCList.h #ifndef __SCLIST_H__ #define __SCLIST_H__ #include #include #include #include #include #define RED "\033[31m" #define NONE "\033[0m" typedef int datatype_t; typedef struct sclist_node { datatype_t data; struct sclist_node * next; } sclist_node; typedef struct sclist_head { uint32_t n; sclist_node *next; } sclist_head; extern sclist_head* create_empyt_sclist(); extern int insert_head_sclist(sclist_head* head, datatype_t value); extern void print_sclist(const sclist_head* head); extern void delete_node_sclist(sclist_head* head, datatype_t value); extern void destyroy_sclist(sclist_head* head); extern bool is_empty_sclist(sclist_head* head); extern uint32_t num_of_node_sclist(sclist_head* head); extern sclist_node* find_node_sclist(const sclist_head* head, datatype_t value); #endif //SCist.c #include "SCList.h" sclist_head* create_empyt_sclist() { sclist_head * l = (sclist_head*)malloc(sizeof(sclist_head)); if (NULL == l) { perror("Failed to allocate memory for sclist_head"); return NULL; } memset(l, 0, sizeof(sclist_head)); l->n = 0; l->next = NULL; return l; } int insert_head_sclist(sclist_head* head, datatype_t value) { sclist_node* new_node = (sclist_node*)malloc(sizeof(sclist_node)); if (NULL == new_node) { perror("Failed to allocate memory for new node"); return -1; } new_node->data = value; if (NULL != head->next) { sclist_node* p = head->next; new_node->next = p->next; p->next = new_node; head->n++; } else { head->next = new_node; new_node->next = new_node; head->n++; } return 0; } sclist_node* find_node_sclist(const sclist_head* head, const datatype_t value) { sclist_node* current = head->next; for (size_t i = 0; i n; i++) { if (current->data != value) { current = current->next; } else { return current; } } return NULL; } void delete_node_sclist(sclist_head* head, datatype_t value) { while (NULL != find_node_sclist(head, value)) { sclist_node * p = head->next; sclist_node * q = NULL; for (size_t i = 0; i n; i++) { if (p->data != value) { q = p; p = p->next; } } // 边界判断 if (NULL != q) { q->next = p->next; free(p); p = NULL; head->n--; } else { q = head->next; head->next = p->next; for (size_t i = 1; i n; i++) { q = q->next; } q->next = p->next; free(p); p = NULL; head->n--; if (head->n == 0) { head->next = NULL; } } } } void destyroy_sclist(sclist_head* head) { if (head->next == NULL) { free(head); head = NULL; return; } sclist_node* p = head->next; sclist_node* q = NULL; for (size_t i = 1; i n; i++) { q = p; p = p->next; free(q); q = NULL; } free(p); p = NULL; free(head); head = NULL; } bool is_empty_sclist(sclist_head* head) { return head->next == NULL ? true : false; } uint32_t num_of_node_sclist(sclist_head* head) { return head->n; } void print_sclist(const sclist_head* head) { const sclist_node* current = head->next; for (size_t i = 0; i n; i++) { printf("%d -> ", current->data); current = current->next; } printf("%d\n", head->next->data); } //main.c #include "SCList.h" #include int test() { sclist_head* l = create_empyt_sclist(); int n = 8, k = 3, m = 4; // scanf("%d %d %d", &n, &k, &m); for (size_t i = n; i > 0; i--) { insert_head_sclist(l, i); } for (size_t i = 0; i next = l->next->next; } printf("num_of_list == > %d\n", num_of_node_sclist(l)); printf("k == > %d\n", l->next->data); while (0 != l->n) { for (size_t i = 1; i next = l->next->next; } printf("m == > %d\n", l->next->data); delete_node_sclist(l, l->next->data); printf("num_of_list == > %d\n", num_of_node_sclist(l)); } destyroy_sclist(l); } int main(int argc, char const *argv[]) { return test(); } ➜ 3 ./sclist num_of_list == > 8 k == > 3 m == > 6 num_of_list == > 7 m == > 2 num_of_list == > 6 m == > 7 num_of_list == > 5 m == > 4 num_of_list == > 4 m == > 3 num_of_list == > 3 m == > 5 num_of_list == > 2 m == > 1 num_of_list == > 1 m == > 8 num_of_list == > 0

得分 100
学习任务

jelasin 的学生作业:

//SList.h #ifndef __SLIST_H__ #define __SLIST_H__ #include #include #include #include #include #define RED "\033[31m" #define NONE "\033[0m" typedef int datatype_t; typedef struct slist_node { datatype_t data; struct slist_node * next; } slist_node; typedef struct slist_head { uint32_t n; slist_node *next; } slist_head; extern slist_head* create_empyt_slist(); extern int insert_head_slist(slist_head* head, datatype_t value); extern void print_slist(const slist_head* head); extern int insert_tail_slist(slist_head* head, datatype_t value); extern int insert_order_slist(slist_head* head, datatype_t value); extern void delete_node_slist(slist_head* head, datatype_t value); extern void destyroy_slist(slist_head* head); extern bool is_empty_slist(slist_head* head); extern uint32_t num_of_node_slist(slist_head* head); extern slist_node* find_node_slist(const slist_head* head, datatype_t value); extern void reverse_node_slist(slist_head* head); #endif //SList.c #include "SList.h" slist_head* create_empyt_slist() { slist_head * l = (slist_head*)malloc(sizeof(slist_head)); if (NULL == l) { perror("Failed to allocate memory for slist_head"); return NULL; } memset(l, 0, sizeof(slist_head)); l->n = 0; l->next = NULL; return l; } int insert_head_slist(slist_head* head, datatype_t value) { slist_node* new_node = (slist_node*)malloc(sizeof(slist_node)); if (NULL == new_node) { perror("Failed to allocate memory for new node"); return -1; } new_node->data = value; new_node->next = head->next; head->next = new_node; head->n++; return 0; } int insert_tail_slist(slist_head* head, datatype_t value) { slist_node* new_node = (slist_node*)malloc(sizeof(slist_node)); if (NULL == new_node) { perror("Failed to allocate memory for new node"); return -1; } new_node->data = value; slist_node* temp = head->next; if (NULL != temp) { while(NULL != temp->next) { temp = temp->next; } } else // 只有首个元素会调用,放在else block { head->next = new_node; new_node->next = NULL; head->n++; return 0; } temp->next = new_node; new_node->next = NULL; head->n++; return 0; } int insert_order_slist(slist_head* head, datatype_t value) { slist_node* new_node = (slist_node*)malloc(sizeof(slist_node)); if (NULL == new_node) { perror("Failed to allocate memory for new node"); return -1; } new_node->data = value; slist_node* temp = head->next; if (NULL != temp) { while (NULL != temp->next && temp->next->data next; } } else // 只有首个元素会调用,放在else block { head->next = new_node; new_node->next = NULL; head->n++; return 0; } new_node->next = temp->next; temp->next = new_node; head->n++; return 0; } slist_node* find_node_slist(const slist_head* head, const datatype_t value) { slist_node* current = head->next; if (NULL != current) { while (value != current->data) { if (NULL != current->next) { current = current->next; } else { return NULL; } } } return current; } void delete_node_slist(slist_head* head, datatype_t value) { while (NULL != find_node_slist(head, value)) { slist_node * p = head->next; slist_node * q = NULL; if (NULL != p) { while (p->data != value) { if (NULL != p->next) { q = p; p = p->next; } else { return; } } q->next = p->next; free(p); p = NULL; } } } void destyroy_slist(slist_head* head) { slist_node* current = head->next; slist_node* p = NULL; if (NULL != current) { while (NULL != current->next) { p = current; current = current->next; printf("%d ", p->data); free(p); p = NULL; } // current != NULL && current->next == NULL printf("%d \n", current->data); free(current); current = NULL; } free(head); head = NULL; } bool is_empty_slist(slist_head* head) { return head->next == NULL ? true : false; } uint32_t num_of_node_slist(slist_head* head) { return head->n; } void reverse_node_slist(slist_head* head) { slist_node *prev = NULL; slist_node *current = head->next; slist_node *next = NULL; // 只需要想办法把指针转过来即可,最简单的就是迭代法,记住前后位置,然后改变当前node的指针即可. while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } // 最后head指向原tail. head->next = prev; } void print_slist(const slist_head* head) { const slist_node* current = head->next; while (current != NULL) { printf("%d -> ", current->data); current = current->next; } printf("NULL\n"); } //main.c #include "SList.h" int test() { slist_head * l = create_empyt_slist(); if (NULL == l) { perror("Failed to create list"); return -1; } // 创建一个单向链表,把1,5,3,7,9,5,8,5,3无序数据要求按从大到小的方式利用有序插入的方式插入链表,并输出。然后删除链表中所有值为3的数据 insert_head_slist(l, 1); insert_head_slist(l, 5); insert_head_slist(l, 3); insert_head_slist(l, 7); insert_head_slist(l, 9); print_slist(l); reverse_node_slist(l); print_slist(l); destyroy_slist(l); } int main(int argc, char const *argv[]) { return test(); } ➜ 2 ./slist 9 -> 7 -> 3 -> 5 -> 1 -> NULL 1 -> 5 -> 3 -> 7 -> 9 -> NULL 1 5 3 7 9

得分 100
讨论题

jelasin 的学生作业:

//Slist.h #ifndef __SLIST_H__ #define __SLIST_H__ #include #include #include #include #include #define RED "\033[31m" #define NONE "\033[0m" typedef int datatype_t; typedef struct slist_node { datatype_t data; struct slist_node * next; } slist_node; typedef struct slist_head { uint32_t n; slist_node *next; } slist_head; extern slist_head* create_empyt_slist(); extern int insert_head_slist(slist_head* head, datatype_t value); extern void print_slist(const slist_head* head); extern int insert_tail_slist(slist_head* head, datatype_t value); extern int insert_order_slist(slist_head* head, datatype_t value); extern void delete_node_slist(slist_head* head, datatype_t value); extern void destyroy_slist(slist_head* head); extern bool is_empty_slist(slist_head* head); extern uint num_of_node_slist(slist_head* head); extern slist_node* find_node_slist(const slist_head* head, datatype_t value); #endif //SList.c #include "SList.h" slist_head* create_empyt_slist() { slist_head * l = (slist_head*)malloc(sizeof(slist_head)); if (NULL == l) { perror("Failed to allocate memory for slist_head"); return NULL; } memset(l, 0, sizeof(slist_head)); l->n = 0; l->next = NULL; return l; } int insert_head_slist(slist_head* head, datatype_t value) { slist_node* new_node = (slist_node*)malloc(sizeof(slist_node)); if (NULL == new_node) { perror("Failed to allocate memory for new node"); return -1; } new_node->data = value; new_node->next = head->next; head->next = new_node; head->n++; return 0; } int insert_tail_slist(slist_head* head, datatype_t value) { slist_node* new_node = (slist_node*)malloc(sizeof(slist_node)); if (NULL == new_node) { perror("Failed to allocate memory for new node"); return -1; } new_node->data = value; slist_node* temp = head->next; if (NULL != temp) { while(NULL != temp->next) { temp = temp->next; } } else // 只有首个元素会调用,放在else block { head->next = new_node; new_node->next = NULL; head->n++; return 0; } temp->next = new_node; new_node->next = NULL; head->n++; return 0; } int insert_order_slist(slist_head* head, datatype_t value) { slist_node* new_node = (slist_node*)malloc(sizeof(slist_node)); if (NULL == new_node) { perror("Failed to allocate memory for new node"); return -1; } new_node->data = value; slist_node* temp = head->next; if (NULL != temp) { while (NULL != temp->next && temp->next->data next; } } else // 只有首个元素会调用,放在else block { head->next = new_node; new_node->next = NULL; head->n++; return 0; } new_node->next = temp->next; temp->next = new_node; head->n++; return 0; } slist_node* find_node_slist(const slist_head* head, const datatype_t value) { slist_node* current = head->next; if (NULL != current) { while (value != current->data) { if (NULL != current->next) { current = current->next; } else { return NULL; } } } return current; } void delete_node_slist(slist_head* head, datatype_t value) { while (NULL != find_node_slist(head, value)) { slist_node * p = head->next; slist_node * q = NULL; if (NULL != p) { while (p->data != value) { if (NULL != p->next) { q = p; p = p->next; } else { return; } } if (NULL != q) { q->next = p->next; free(p); p = NULL; head->n--; } else { head->next = p->next; free(p); p = NULL; head->n--; if (head->n == 0) { head->next = NULL; } } } } } void destyroy_slist(slist_head* head) { slist_node* current = head->next; slist_node* p = NULL; if (NULL != current) { while (NULL != current->next) { p = current; current = current->next; printf("%d ", p->data); free(p); p = NULL; } // current != NULL && current->next == NULL printf("%d \n", current->data); free(current); current = NULL; } free(head); head = NULL; } bool is_empty_slist(slist_head* head) { return head->next == NULL ? true : false; } uint32_t num_of_node_slist(slist_head* head) { return head->n; } void print_slist(const slist_head* head) { const slist_node* current = head->next; while (current != NULL) { printf("%d -> ", current->data); current = current->next; } printf("NULL\n"); } //main.c #include "SList.h" int test() { slist_head * l = create_empyt_slist(); if (NULL == l) { perror("Failed to create list"); return -1; } // 创建一个单向链表,把1,5,3,7,9,5,8,5,3无序数据要求按从大到小的方式利用有序插入的方式插入链表,并输出。然后删除链表中所有值为3的数据 insert_order_slist(l, 1); insert_order_slist(l, 5); insert_order_slist(l, 3); insert_order_slist(l, 7); insert_order_slist(l, 9); insert_order_slist(l, 5); insert_order_slist(l, 8); insert_order_slist(l, 5); insert_order_slist(l, 3); print_slist(l); delete_node_slist(l, 3); print_slist(l); destyroy_slist(l); } int main(int argc, char const *argv[]) { return test(); } ➜ 2 ./slist 1 -> 3 -> 3 -> 5 -> 5 -> 5 -> 7 -> 8 -> 9 -> NULL 1 -> 5 -> 5 -> 5 -> 7 -> 8 -> 9 -> NULL 1 5 5 5 7 8 9

得分 100
讨论题

jelasin 的学生作业:

//Slist.h #ifndef __SLIST_H__ #define __SLIST_H__ #include #include #include #include #include #define RED "\033[31m" #define NONE "\033[0m" typedef int datatype_t; typedef struct slist_node { datatype_t data; struct slist_node * next; } slist_node; typedef struct slist_head { uint n; slist_node *next; } slist_head; extern slist_head* create_empyt_slist(); extern int insert_head_slist(slist_head* head, datatype_t value); extern void print_slist(const slist_head* head); extern int insert_tail_slist(slist_head* head, datatype_t value); extern int insert_order_slist(slist_head* head, datatype_t value); #endif //Slist.c #include "SList.h" slist_head* create_empyt_slist() { slist_head * l = (slist_head*)malloc(sizeof(slist_head)); if (NULL == l) { perror("Failed to allocate memory for slist_head"); return NULL; } memset(l, 0, sizeof(slist_head)); l->n = 0; l->next = NULL; return l; } int insert_head_slist(slist_head* head, datatype_t value) { slist_node* new_node = (slist_node*)malloc(sizeof(slist_node)); if (NULL == new_node) { perror("Failed to allocate memory for new node"); return -1; } new_node->data = value; new_node->next = head->next; head->next = new_node; head->n++; return 0; } int insert_tail_slist(slist_head* head, datatype_t value) { slist_node* new_node = (slist_node*)malloc(sizeof(slist_node)); if (NULL == new_node) { perror("Failed to allocate memory for new node"); return -1; } new_node->data = value; slist_node* temp = head->next; if (NULL != temp) { while(NULL != temp->next) { temp = temp->next; } } else // 只有首个元素会调用,放在else block { head->next = new_node; new_node->next = NULL; head->n++; return 0; } temp->next = new_node; new_node->next = NULL; head->n++; return 0; } int insert_order_slist(slist_head* head, datatype_t value) { slist_node* new_node = (slist_node*)malloc(sizeof(slist_node)); if (NULL == new_node) { perror("Failed to allocate memory for new node"); return -1; } new_node->data = value; slist_node* temp = head->next; if (NULL != temp) { while (NULL != temp->next && temp->next->data next; } } else // 只有首个元素会调用,放在else block { head->next = new_node; new_node->next = NULL; head->n++; return 0; } new_node->next = temp->next; temp->next = new_node; head->n++; return 0; } void print_slist(const slist_head* head) { const slist_node* current = head->next; while (current != NULL) { printf("%d -> ", current->data); current = current->next; } printf("NULL\n"); } //main.c #include "SList.h" int test() { slist_head * l = create_empyt_slist(); if (NULL == l) { perror("Failed to create list"); return -1; } // 创建一个单向链表,把1,5,3,7,9无序数据要求按从大到小的方式利用有序插入的方式插入链表,并输出。 insert_order_slist(l, 1); insert_order_slist(l, 5); insert_order_slist(l, 3); insert_order_slist(l, 8); insert_order_slist(l, 9); print_slist(l); } int main(int argc, char const *argv[]) { return test(); } ➜ 2 ./slist 1 -> 3 -> 5 -> 8 -> 9 -> NULL

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号