Skip to content

Commit 764aff6

Browse files
authored
Add files via upload
1 parent 7799aa1 commit 764aff6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+2632
-0
lines changed

Ballclock/linkqueue.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "linkqueue.h"
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
6+
linkqueue *queue_create() {
7+
linkqueue *lq;
8+
if ((lq = (linkqueue *) malloc(sizeof(linkqueue))) == NULL) {
9+
printf("malloc linkqueue failed\n");
10+
return NULL;
11+
}
12+
lq->front = lq->rear = (linklist) malloc(sizeof(listnode));
13+
if (lq->front == NULL) {
14+
printf("malloc node failed\n");
15+
return NULL;
16+
}
17+
lq->front->data = 0;
18+
lq->front->next = NULL;
19+
return lq;
20+
}
21+
22+
int enqueue(linkqueue *lq, datatype x) {
23+
if (lq == NULL) {
24+
printf("lq is NULL\n");
25+
return -1;
26+
}
27+
linklist p;
28+
if ((p = (linklist) malloc(sizeof(listnode))) == NULL) {
29+
printf("malloc node failed\n");
30+
return -1;
31+
}
32+
p->data = x;
33+
p->next = NULL;
34+
lq->rear->next = p;
35+
lq->rear = p;
36+
return 0;
37+
}
38+
39+
datatype dequeue(linkqueue *lq) {
40+
if (lq == NULL) {
41+
printf("lq is NULL\n");
42+
return -1;
43+
}
44+
linklist p;
45+
p = lq->front;
46+
lq->front = p->next;
47+
free(p);
48+
p = NULL;
49+
return lq->front->data;
50+
}
51+
52+
int queue_empty(linkqueue *lq) {
53+
if (lq == NULL) {
54+
printf("lq is NULL\n");
55+
return -1;
56+
}
57+
return (lq->front == lq->rear ? 1 : 0);
58+
}
59+
60+
int queue_clear(linkqueue *lq) {
61+
if (lq == NULL) {
62+
printf("lq is NULL\n");
63+
return -1;
64+
}
65+
linklist p;
66+
printf("clear:");
67+
while (lq->front->next)
68+
{
69+
p=lq->front;
70+
lq->front=p->next;
71+
printf("%d ",p->data);
72+
free(p);
73+
p=NULL;
74+
}
75+
return 0;
76+
}
77+
78+
linkqueue *queue_free(linkqueue *lq) {
79+
if (lq == NULL) {
80+
printf("lq is NULL\n");
81+
return NULL;
82+
}
83+
linklist p;
84+
printf("free:");
85+
while (lq->front)
86+
{
87+
p=lq->front;
88+
lq->front=p->next;
89+
printf("%d ",p->data);
90+
free(p);
91+
}
92+
free(lq);
93+
lq=NULL;
94+
return NULL;
95+
}

Ballclock/linkqueue.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef CPROJECT_LINKQUEUE_H
2+
#define CPROJECT_LINKQUEUE_H
3+
4+
typedef int datatype;
5+
6+
typedef struct node {
7+
datatype data;
8+
struct node *next;
9+
} listnode, *linklist;
10+
11+
typedef struct {
12+
linklist front;
13+
linklist rear;
14+
} linkqueue;
15+
16+
linkqueue *queue_create();
17+
//创建队列
18+
19+
int enqueue(linkqueue *lq, datatype x);
20+
//入队
21+
22+
datatype dequeue(linkqueue *lq);
23+
//出队
24+
25+
int queue_empty(linkqueue *lq);
26+
//判断队列是否为空
27+
28+
int queue_clear(linkqueue *lq);
29+
//队列清空
30+
31+
linkqueue *queue_free(linkqueue *lq);
32+
//队列释放
33+
#endif

Ballclock/sqstack.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "sqstack.h"
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
6+
sqstack *stack_create(int len) {
7+
sqstack *s;
8+
if ((s = (sqstack *) malloc(sizeof(sqstack))) == NULL) {
9+
printf("malloc sqstack failed\n");
10+
return NULL;
11+
}
12+
if ((s->data = (data_t *) malloc(len * sizeof(data_t))) == NULL) {
13+
printf("malloc data failed\n");
14+
free(s);
15+
return NULL;
16+
}
17+
memset(s->data, 0, len * sizeof(data_t));
18+
s->maxlen = len;
19+
s->top = -1;
20+
return s;
21+
}
22+
23+
int stack_push(sqstack *s, data_t value) {
24+
if (s == NULL) {
25+
printf("s is NULL\n");
26+
return -1;
27+
}
28+
if (s->top == s->maxlen - 1) {
29+
printf("stack is full\n");
30+
return -1;
31+
}
32+
s->top++;
33+
s->data[s->top] = value;
34+
return 0;
35+
}
36+
37+
data_t stack_pop(sqstack *s) {
38+
s->top--;
39+
return s->data[s->top + 1];
40+
}
41+
42+
int stack_empty(sqstack *s) {
43+
if (s == NULL) {
44+
printf("s is NULL\n");
45+
return -1;
46+
}
47+
return (s->top == -1) ? 1 : 0;
48+
}
49+
50+
int stack_full(sqstack *s) {
51+
if (s == NULL) {
52+
printf("s is NULL\n");
53+
return -1;
54+
}
55+
return (s->top == s->maxlen - 1) ? 1 : 0;
56+
}
57+
58+
data_t stack_top(sqstack *s) {
59+
return (s->data[s->top]);
60+
}
61+
62+
int stack_clear(sqstack *s) {
63+
if (s == NULL) {
64+
printf("s is NULL\n");
65+
return -1;
66+
}
67+
s->top = -1;
68+
return 0;
69+
}
70+
71+
int stack_free(sqstack *s) {
72+
if (s == NULL) {
73+
printf("s is NULL\n");
74+
return -1;
75+
}
76+
if (s->data)
77+
free(s->data);
78+
free(s);
79+
return 0;
80+
}

Ballclock/sqstack.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef CPROJECT_SQSTACK_H
2+
#define CPROJECT_SQSTACK_H
3+
4+
typedef int data_t;
5+
6+
typedef struct {
7+
data_t *data;
8+
int maxlen;
9+
int top;
10+
} sqstack;
11+
12+
sqstack *stack_create(int len);
13+
//创建一个空栈
14+
15+
int stack_push(sqstack *s, data_t value);
16+
//入栈
17+
18+
data_t stack_pop(sqstack *s);
19+
//出栈
20+
21+
int stack_empty(sqstack *s);
22+
//测试栈是否为空
23+
24+
int stack_full(sqstack *s);
25+
//测试栈是否为满栈
26+
27+
data_t stack_top(sqstack *s);
28+
//查看栈顶值
29+
30+
int stack_clear(sqstack *s);
31+
//栈清空
32+
33+
int stack_free(sqstack *s);
34+
//栈释放
35+
36+
#endif

Ballclock/test.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include "linkqueue.h"
2+
#include "sqstack.h"
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
6+
int check(linkqueue *lq) {
7+
if (lq == NULL) {
8+
printf("lq is NULL\n");
9+
return -1;
10+
}
11+
linklist p;
12+
p = lq->front->next;
13+
while (p && p->next) {
14+
if (p->data < p->next->data) {
15+
p = p->next;
16+
} else {
17+
return 0;
18+
}
19+
}
20+
return 1;
21+
}
22+
23+
int main() {
24+
int i, min = 0, value;
25+
linkqueue *lq;
26+
sqstack *s_hour, *s_five, *s_min;
27+
if ((lq = queue_create()) == NULL) {
28+
printf("lq is NULL\n");
29+
return -1;
30+
}
31+
for (i = 1; i <= 27; i++) {
32+
enqueue(lq, i);
33+
}
34+
if ((s_hour = stack_create(11)) == NULL) {
35+
printf("s_hour is NULL\n");
36+
return -1;
37+
}
38+
if ((s_five = stack_create(11)) == NULL) {
39+
printf("s_hour is NULL\n");
40+
return -1;
41+
}
42+
if ((s_min = stack_create(4)) == NULL) {
43+
printf("s_hour is NULL\n");
44+
return -1;
45+
}
46+
while (1) {
47+
min++;
48+
if (!queue_empty(lq)) {
49+
value = dequeue(lq);
50+
if (!stack_full(s_min)) {
51+
stack_push(s_min, value);
52+
} else {
53+
while (!stack_empty(s_min)) {
54+
enqueue(lq, stack_pop(s_min));
55+
}
56+
if (!stack_full(s_five)) {
57+
stack_push(s_five, value);
58+
} else {
59+
while (!stack_empty(s_five)) {
60+
enqueue(lq, stack_pop(s_five));
61+
}
62+
if (!stack_full(s_hour)) {
63+
stack_push(s_hour, value);
64+
} else {
65+
while (!stack_empty(s_hour)) {
66+
enqueue(lq, stack_pop(s_hour));
67+
}
68+
enqueue(lq, value);
69+
if (check(lq)) {
70+
break;
71+
}
72+
}
73+
}
74+
}
75+
}
76+
}
77+
printf("Time is:%d minute\n", min);
78+
while (!queue_empty(lq))
79+
printf("%d ", dequeue(lq));
80+
puts("");
81+
queue_free(lq);
82+
stack_free(s_hour);
83+
stack_free(s_five);
84+
stack_free(s_min);
85+
puts("");
86+
system("pause");
87+
return 0;
88+
}

0 commit comments

Comments
 (0)