作业社区
探索学习新天地,共享知识资源!
慕斯卡4428351 的学生作业:
head.h #ifndef __HEAD_H__ #define __HEAD_H__ #include #include #include #define N 27 typedef int data_t; //node typedef struct node { data_t data; struct node *next; }linknode_t; //linkqueue typedef struct { linknode_t *front; linknode_t *rear; }linkqueue_t; //linkstack typedef struct { linknode_t *top; int n; }linkstack_t; extern linkstack_t *create_empty_linkstack(); extern int is_empty_linkstack(linkstack_t *l); extern int push_linkstack(linkstack_t *l,data_t data); extern data_t pop_linkstack(linkstack_t *l); extern data_t get_top_linkstack(linkstack_t *l); extern linkqueue_t *create_empty_linkqueue(); extern int is_empty_linkqueue(linkqueue_t *l); extern int enter_linkqueue(linkqueue_t *l,data_t data); extern data_t leave_linkqueue(linkqueue_t *l); #endif linkstack.c #include "head.h" linkstack_t *create_empty_linkstack() { linkstack_t *t = NULL; t = (linkstack_t *)malloc(sizeof(linkstack_t)); if(NULL == t) { printf("malloc is failed\n"); return NULL; } memset(t,0,sizeof(linkstack_t)); return t; } int is_empty_linkstack(linkstack_t *l) { return l->top == NULL ? 1 : 0; } int push_linkstack(linkstack_t *l,data_t data) { linknode_t *temp = NULL; temp = (linknode_t *)malloc(sizeof(linknode_t)); if(NULL == temp) { printf("malloc is failed\n"); return -1; } temp->data = data; temp->next = l->top; l->top = temp; l->n++; return 0; } data_t pop_linkstack(linkstack_t *l) { data_t data; linknode_t *temp = NULL; temp = l->top; data = temp->data; l->top = temp->next; l->n--; free(temp); temp = NULL; return data; } data_t get_top_linkstack(linkstack_t *l) { return l->top->data; } linkqueue.c #include "head.h" linkqueue_t *create_empty_linkqueue() { linkqueue_t *q = NULL; linknode_t *head = NULL; head = (linknode_t *)malloc(sizeof(linknode_t)); if(NULL == head) { printf("malloc is failed\n"); return NULL; } memset(head,0,sizeof(linknode_t)); q = (linkqueue_t *)malloc(sizeof(linkqueue_t)); if(NULL == q) { printf("malloc is failed\n"); return NULL; } q->front = head; q->rear = head; return q; } int is_empty_linkqueue(linkqueue_t *l) { return l->front == l->front ? 1 : 0; } extern int enter_linkqueue(linkqueue_t *l,data_t data) { linknode_t *temp = NULL; temp = (linknode_t *)malloc(sizeof(linknode_t)); if(NULL == temp) { printf("malloc is failed\n"); return -1; } temp->data = data; temp->next = l->rear->next; l->rear->next = temp; l->rear = temp; return 0; } data_t leave_linkqueue(linkqueue_t *l) { data_t data; linknode_t *temp = NULL; temp = l->front->next; data = temp->data; l->front->next = temp->next; if(temp->next == NULL) l->rear = l->front; free(temp); temp = NULL; return data; } ballblock.c #include "head.h" int is_initial_ballstate(linkqueue_t *l) { int i = 0; linknode_t *temp = NULL; temp = l->front->next; for(i = 1; i data != i) { return 0; } temp = temp->next; } return 1; } void printf_data_ballqueue(linkqueue_t *l) { linknode_t *temp = NULL; temp = l->front->next; while(temp != NULL) { printf("%d ",temp->data); temp = temp->next; } printf("\n"); } void ballclock() { linkqueue_t *ball_queue = NULL; linkstack_t *min_stack = NULL; linkstack_t *fmin_stack = NULL; linkstack_t *hour_stack = NULL; ball_queue = create_empty_linkqueue(); min_stack = create_empty_linkstack(); fmin_stack = create_empty_linkstack(); hour_stack = create_empty_linkstack(); int i = 0; int j = 0; data_t ball_num = 0; data_t leave_ball_num = 0; //initial for(i = 1;i