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

作业社区

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

0 提交作业
0 布置作业
0 满分作业
得分 98
学习任务

慕尼黑0001808 的学生作业:

#ifndef _HEAD_H_ #define _HEAD_H_ #include #include #include #define N 123 typedef int data_t; typedef struct node { data_t data; struct node *next; }linknode_t; //链式栈栈头类型 typedef struct { linknode_t *top; int n; }linkstack_t; //链式队列的队列头的类型 typedef struct { linknode_t *front; linknode_t *rear; }linkqueue_t; //链式栈的操作 extern linkstack_t *create_empty_linkstack(); extern int is_empty_linkstack(linkstack_t *s); extern int push_linkstack(linkstack_t *s,data_t data); extern data_t pop_linkstack(linkstack_t *s); extern data_t get_top_data(linkstack_t *s); //链式队列的操作 extern linkqueue_t *create_empty_linkqueue(); extern int is_empty_linkqueue(linkqueue_t *q); extern int enter_linkqueue(linkqueue_t *q,data_t data); extern data_t delete_linkqueue(linkqueue_t *q); #endif #include "head.h" linkqueue_t *create_empty_linkqueue() { linknode_t *head = NULL; linkqueue_t *q = NULL; head = (linknode_t *)malloc(sizeof(linknode_t)); head->next = NULL; q = (linkqueue_t *)malloc(sizeof(linkqueue_t)); q->front = q->rear = head; return q; } int is_empty_linkqueue(linkqueue_t *q) { return q->front == q->rear; } int enter_linkqueue(linkqueue_t *q,data_t data) { linknode_t *temp = NULL; temp = (linknode_t *)malloc(sizeof(linknode_t)); temp->data = data; /*temp->next = q->rear->next;*/ temp->next = NULL; q->rear->next = temp; q->rear = temp; return 0; } data_t delete_linkqueue(linkqueue_t *q) { linknode_t *temp = NULL; temp = q->front; q->front = temp->next; free(temp); temp = NULL; return q->front->data; } #include "head.h" linkstack_t *create_empty_linkstack() { linkstack_t *s = NULL; s = (linkstack_t *)malloc(sizeof(linkstack_t)); s->top = NULL; s->n = 0; return s; } int is_empty_linkstack(linkstack_t *s) { return s->top == NULL; } int push_linkstack(linkstack_t *s,data_t data) { linknode_t *temp = NULL; temp = (linknode_t *)malloc(sizeof(linknode_t)); temp->data = data; temp->next = s->top; s->top = temp; s->n++; return 0; } data_t pop_linkstack(linkstack_t *s) { linknode_t *temp = NULL; data_t data; temp = s->top; data = temp->data; s->top = temp->next; free(temp); temp = NULL; s->n--; return data; } data_t get_top_data(linkstack_t *s) { return s->top->data; } #include "head.h" int print_linklist(linknode_t *head) { linknode_t *p = head->next; while(p) { /*printf("%-3d",p->data);*/ printf("%-3d\t",p->data); p = p->next; } putchar('\n'); return 0; } int is_orginal_queue(linkqueue_t *q) { int i = 1; linknode_t *p = q->front->next; for(i = 1;i data) return 0; p = p->next; } return 1; } int ball_clock() { linkstack_t *min_stack = NULL, *min5_stack = NULL, *hour_stack = NULL; linkqueue_t *ball_queue = NULL; int half_day = 0; int ball = 0; min_stack = create_empty_linkstack(); min5_stack = create_empty_linkstack(); hour_stack = create_empty_linkstack(); ball_queue = create_empty_linkqueue(); for(ball = 1;ball front); while(1) { ball = delete_linkqueue(ball_queue); if(min_stack->n < 4) { push_linkstack(min_stack,ball); continue; } while(!is_empty_linkstack(min_stack)) { enter_linkqueue(ball_queue,pop_linkstack(min_stack)); } if(min5_stack->n < 11) { push_linkstack(min5_stack,ball); continue; } while(!is_empty_linkstack(min5_stack)) { enter_linkqueue(ball_queue,pop_linkstack(min5_stack)); } if(hour_stack->n < 11) { push_linkstack(hour_stack,ball); continue; } while(!is_empty_linkstack(hour_stack)) { enter_linkqueue(ball_queue,pop_linkstack(hour_stack)); } enter_linkqueue(ball_queue,ball); half_day++; if(is_orginal_queue(ball_queue)) break; } return half_day / 2; } int main() { int day_count = 0; day_count = ball_clock(); printf("Restoring orignal queue need %d days\n",day_count); return 0; }

得分 100
讨论题

慕尼黑0001808 的学生作业:

/* 大家写代码,完成约瑟夫问题。 设编号分别为:1,2,…,n的n个人围坐一圈。约定序号为k(1≤k≤n)的人从1开始计数,数到 m的那个人出列,他的下一位又从1开始计数,数到m的那个人又出列,依次类推,直到所有人出列为止。 假设:设n=8,k=3,m=4时,出列序列为:(6,2,7,4,3,5,1,8) */ //main.c #include "looplist.h" int main() { int n = 8,k = 3,m = 4; int * res = NULL; looplist_t * head = create_cirde_link(n); res = josephu(head,n,k,m); printf("出列顺序:"); for(int i = 0;i < n;i++) { printf("%d ",res[i]); } printf("\n"); free(res); res = NULL; return 0; } //looplist.h #ifndef __LOOPLIST_H__ #define __LOOPLIST_H__ #include #include typedef int data_type_t; typedef struct node{ data_type_t data; struct node * next; }looplist_t; extern looplist_t * create_cirde_link(int n); extern int * josephu(looplist_t * head,int n,int k,int m); #endif /looplist.c #include "looplist.h" //创建单向循环链表 looplist_t * create_cirde_link(int n) { looplist_t * head = NULL; head = (looplist_t *)malloc(sizeof(looplist_t)); if(NULL == head) { printf("malloc head fail\n"); exit(EXIT_FAILURE); } head->data = 1; head->next = head; looplist_t * tail = head; for(int i = 2;i data = i; tail->next = newnode; tail = newnode; } tail->next = head; return head; } // 约瑟夫处理 int * josephu(looplist_t * head,int n,int k,int m) { int * result = (int *)malloc(n * sizeof(int)); if(0==n || NULL == head)return result; looplist_t * prev = NULL; looplist_t * current = head; for(int i=1;inext; } for(int i = 0;i < n;i++) { for(int j = 1;j < m;j++) { prev = current; current = current->next; } result[i] = current->data; prev->next = current->next; free(current); current = prev->next; } return result; }

微信客服

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

帮助反馈 APP下载

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

公众号

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