作业社区
探索学习新天地,共享知识资源!
SamstagBaron 的学生作业:
因为11:59时需要26个球,进位还需要一个,所以需要27个球,用时33120min回归原始状态【图片】 #include #include #include const int N = 27; typedef int data_t; typedef struct nodelist{ data_t value; struct nodelist* next; }intlist; typedef struct nodestack{ intlist* top; }intstack; typedef struct nodedeque{ intlist* head; intlist* tail; }intdeque; intstack* create_stack(){ intstack *res = (intstack*)malloc(sizeof(intstack)); res->top = NULL; return res; } data_t pop_stack(intstack* stc){ intlist *res = stc->top; stc->top = stc->top->next; data_t value =res->value; free(res); return value; } void push_stack(intstack* stc,data_t value){ intlist* curr = (intlist*)malloc(sizeof(intlist)); curr->value = value; curr->next = stc->top; stc->top = curr; } int empty_stack(intstack* stc){ return stc->top==NULL?1:0; } intdeque* create_deque(){ intdeque *res = (intdeque*)malloc(sizeof(intdeque)); res->head = (intlist*)malloc(sizeof(intlist)); res->tail = res->head; res->head->next = NULL; return res; } data_t pop_deque(intdeque* stc){ intlist *res = stc->head->next; stc->head->next = res->next; if(stc->head->next==NULL){ stc->tail = stc->head; } data_t value = res->value; free(res); return value; } void push_deque(intdeque* stc,data_t value){ intlist* curr = (intlist*)malloc(sizeof(intlist)); curr->value = value; stc->tail->next = curr; stc->tail = curr; } int empty_deque(intdeque* stc){ return stc->head==stc->tail?1:0; } int check(intdeque* q){ int cnt = 0; intlist* head= q->head->next; while(head){ //printf("%d ",head->value); if(head->value!=cnt+1){ cnt = -1; } cnt+=1; head = head->next; } // printf("\n"); return cnt==N; } int main(){ intdeque* ballque = create_deque(); printf("Build ballque\n"); for(int ii=1;ii
+12
贾文泽 的学生作业:
#include #include struct student{ char name[20]; int id; int score; }st1={“jack”,1,100}; //st1为已经初始化为非0的全局变量,存储在.data中 int main(int argc, const char *argv[]) //argc和argv是main()函数中的局部变量,存储在栈区中 { struct student st[4]={st1,{“rose”,2,70},{“lilei”,3,60},{“hmm”,4,50}}; //i,id,st是局部变量,存储在栈区中 int i,id; printf(“name\tid\tscore\n”); for(i=0;i
+13
jelasin 的学生作业:
练习 1 int depth_of_binary_tree(BTNode_t* root) { if (NULL == root) { return 0; } int left = depth_of_binary_tree(root->left); int right = depth_of_binary_tree(root->right); return left > right ? left + 1 : right + 1; } 练习 2 slist_node* find_by_idx_slist(const slist_head* head, int idx) { if (NULL == head->next || idx head->n) { return NULL; } slist_node* current = head->next; for (size_t i = 1; i next; } } return current; } int sort_linklist(slist_head* head) { slist_node* p = NULL; slist_node* q = NULL; if (NULL == head->next) { return -1; } #define SWAP(x, y) ({ \ typeof(*x) _x = *x; \ typeof(*y) _y = *y; \ (void) (&_x == &_y); \ *x = _y; *y = _x; \ }) printf("Sorting...\n"); bool flag; int last_idx = head->n; int flag_idx = 0; while (true) { flag = true; for (size_t j = 1; j data > q->data) { SWAP(&(p->data), &(q->data)); print_slist(head); flag_idx = j; flag = false; } } if (flag) { break; } last_idx = flag_idx; } return 0; } // main.c #include "SList.h" int test() { slist_head * l = create_empyt_slist(); if (NULL == l) { perror("Failed to create list"); return -1; } 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); insert_head_slist(l, 8); insert_head_slist(l, 2); insert_head_slist(l, 4); insert_head_slist(l, 6); print_slist(l); sort_linklist(l); print_slist(l); destyroy_slist(l); return 0; } int main(int argc, char const *argv[]) { return test(); } ➜ sList clang *.c -o slist ➜ sList ./slist 6 -> 4 -> 2 -> 8 -> 9 -> 7 -> 3 -> 5 -> 1 -> NULL Sorting... 4 -> 6 -> 2 -> 8 -> 9 -> 7 -> 3 -> 5 -> 1 -> NULL 4 -> 2 -> 6 -> 8 -> 9 -> 7 -> 3 -> 5 -> 1 -> NULL 4 -> 2 -> 6 -> 8 -> 7 -> 9 -> 3 -> 5 -> 1 -> NULL 4 -> 2 -> 6 -> 8 -> 7 -> 3 -> 9 -> 5 -> 1 -> NULL 4 -> 2 -> 6 -> 8 -> 7 -> 3 -> 5 -> 9 -> 1 -> NULL 4 -> 2 -> 6 -> 8 -> 7 -> 3 -> 5 -> 1 -> 9 -> NULL 2 -> 4 -> 6 -> 8 -> 7 -> 3 -> 5 -> 1 -> 9 -> NULL 2 -> 4 -> 6 -> 7 -> 8 -> 3 -> 5 -> 1 -> 9 -> NULL 2 -> 4 -> 6 -> 7 -> 3 -> 8 -> 5 -> 1 -> 9 -> NULL 2 -> 4 -> 6 -> 7 -> 3 -> 5 -> 8 -> 1 -> 9 -> NULL 2 -> 4 -> 6 -> 7 -> 3 -> 5 -> 1 -> 8 -> 9 -> NULL 2 -> 4 -> 6 -> 3 -> 7 -> 5 -> 1 -> 8 -> 9 -> NULL 2 -> 4 -> 6 -> 3 -> 5 -> 7 -> 1 -> 8 -> 9 -> NULL 2 -> 4 -> 6 -> 3 -> 5 -> 1 -> 7 -> 8 -> 9 -> NULL 2 -> 4 -> 3 -> 6 -> 5 -> 1 -> 7 -> 8 -> 9 -> NULL 2 -> 4 -> 3 -> 5 -> 6 -> 1 -> 7 -> 8 -> 9 -> NULL 2 -> 4 -> 3 -> 5 -> 1 -> 6 -> 7 -> 8 -> 9 -> NULL 2 -> 3 -> 4 -> 5 -> 1 -> 6 -> 7 -> 8 -> 9 -> NULL 2 -> 3 -> 4 -> 1 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL 2 -> 3 -> 1 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL 2 -> 1 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> NULL 1 2 3 4 5 6 7 8 9 练习3 int merge_sorted_linklist(slist_head* head_1, slist_head* head_2) { if (NULL == head_1 || NULL == head_2) { return -1; } slist_node* cmp_node = NULL; slist_node* insert_node = NULL; for (size_t i = 1; i n; i++) { insert_node = find_by_idx_slist(head_2, i); printf("Inserting %d\n", insert_node->data); for (size_t j = 1; j n; j++) { cmp_node = find_by_idx_slist(head_1, j); printf("Comparing %d and %d\n", cmp_node->data, insert_node->data); if (insert_node->data > cmp_node->data && j != head_1->n) { continue; } else if (unlikely(j == head_1->n)) { insert_tail_slist(head_1, insert_node->data); break; } else if (insert_node->data data) { if (unlikely(1 == j)) { insert_head_slist(head_1, insert_node->data); } else { slist_node* temp = cmp_node->next; 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 = cmp_node->data; cmp_node->data = insert_node->data; cmp_node->next = new_node; new_node->next = temp; head_1->n++; print_slist(head_1); } break; } } } return 0; } ➜ sList ./slist 1 -> 3 -> 5 -> 7 -> 9 -> NULL 2 -> 4 -> 6 -> 8 -> 10 -> NULL Inserting 2 Comparing 1 and 2 Comparing 3 and 2 1 -> 2 -> 3 -> 5 -> 7 -> 9 -> NULL Inserting 4 Comparing 1 and 4 Comparing 2 and 4 Comparing 3 and 4 Comparing 5 and 4 1 -> 2 -> 3 -> 4 -> 5 -> 7 -> 9 -> NULL Inserting 6 Comparing 1 and 6 Comparing 2 and 6 Comparing 3 and 6 Comparing 4 and 6 Comparing 5 and 6 Comparing 7 and 6 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 9 -> NULL Inserting 8 Comparing 1 and 8 Comparing 2 and 8 Comparing 3 and 8 Comparing 4 and 8 Comparing 5 and 8 Comparing 6 and 8 Comparing 7 and 8 Comparing 9 and 8 Inserting 10 Comparing 1 and 10 Comparing 2 and 10 Comparing 3 and 10 Comparing 4 and 10 Comparing 5 and 10 Comparing 6 and 10 Comparing 7 and 10 Comparing 9 and 10 Comparing 8 and 10 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 9 -> 8 -> 10 -> NULL 1 2 3 4 5 6 7 9 8 10 2 4 6 8 10 练习4【图片】
+8