作业社区
探索学习新天地,共享知识资源!
慕尼黑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; }
+11