数据结构的球钟问题(采用的是顺序栈和链式队列)

#include <stdio.h>  
2.#include <stdlib.h>  
3.  
4.#define MAX 20  
5.  
6.#define _SEGMENT_  
7.#define _DEBUG_  
8.  
9.//栈设计  
10.typedef struct  
11.{  
12.    int data[MAX];  
13.    int num;  
14.}Stack;  
15.  
16.//队列设计  
17.struct _node_  
18.{  
19.    int data;  
20.    struct _node_ *next;  
21.};  
22.  
23.typedef struct  
24.{  
25.    struct _node_ *front;  
26.    struct _node_ *rear;  
27.      
28.}ListQueue;  
29.  
30.//栈操作  
31.int stack_init(Stack **p)  
32.{  
33.    *p = (Stack *)malloc(sizeof(Stack));  
34.    (*p)->num = -1;  
35.  
36.    return 0;  
37.}  
38.  
39.int is_empty_stack(Stack *p)  
40.{  
41.    if(p->num == -1)  
42.        return 1;  
43.    else  
44.        return 0;  
45.}  
46.  
47.int is_full_stack(Stack *p)  
48.{  
49.    if(p->num == MAX -1)  
50.        return 1;  
51.    else  
52.        return 0;  
53.}  
54.  
55.int push_stack(Stack *p,int data)  
56.{  
57.    if(is_full_stack(p))  
58.    {  
59.        printf("Error!Stack is full.\n");  
60.        return -1;  
61.    }  
62.      
63.    p->num ++;  
64.    p->data[p->num] = data;  
65.  
66.    return 0;  
67.}  
68.  
69.int pop_stack(Stack *p)  
70.{  
71.    int data;  
72.      
73.    if(is_empty_stack(p))  
74.    {  
75.        printf("Error!Stack is empty.\n");  
76.        return -1;  
77.    }  
78.  
79.    data = p->data[p->num];  
80.    p->num --;  
81.  
82.    return data;  
83.}  
84.  
85.//队列操作  
86.int queue_init(ListQueue **p)  
87.{  
88.    struct _node_ *head;  
89.  
90.    //创建一个头结点  
91.    head = (struct _node_ *)malloc(sizeof(struct _node_));  
92.    head->next = NULL;  
93.  
94.    //让队列的头尾都指向它  
95.    *p = (ListQueue *)malloc(sizeof(ListQueue));  
96.    (*p)->front = (*p)->rear = head;  
97.  
98.    return 0;  
99.}  
100.  
101.int is_empty_queue(ListQueue *p)  
102.{  
103.    if(p->front->next == NULL && p->rear->next == NULL)  
104.        return 1;  
105.    else  
106.        return 0;  
107.}  
108.  
109.int push_queue(ListQueue *p,int data)  
110.{  
111.    struct _node_ *temp;  
112.  
113.    //分配结点  
114.    temp = (struct _node_ *)malloc(sizeof(struct _node_));  
115.    temp->data = data;  
116.    temp->next = NULL;  
117.  
118.    //尾部插入结点  
119.    p->rear->next = temp;  
120.  
121.    //更新尾部指针  
122.    p->rear = temp;  
123.  
124.    return 0;  
125.}  
126.  
127.int pop_queue(ListQueue *p)  
128.{  
129.    struct _node_ *temp;  
130.    int data;  
131.  
132.    if(is_empty_queue(p))  
133.    {  
134.        printf("Error!,queue is empty...\n");  
135.        return 0;  
136.    }  
137.      
138.    //指向头结点的下一个结点  
139.    temp = p->front->next;  
140.    data = temp->data;  
141.      
142.    //删除结点  
143.    p->front->next = temp->next;  
144.    free(temp);  
145.    temp = NULL;  
146.  
147.    //最后一个结点处理  
148.    if(p->front->next == NULL)  
149.        p->rear = p->front;  
150.  
151.    return data;  
152.}  
153.  
154.int print_queue(ListQueue *p)  
155.{  
156.    if(is_empty_queue(p))  
157.    {  
158.        printf("Error!,queue is empty...\n");  
159.        return 0;  
160.    }  
161.      
162.    struct _node_ *q = p->front->next;  
163.  
164.    while(q)  
165.    {  
166.        printf("%d ",q->data);  
167.  
168.        q = q->next;  
169.    }  
170.  
171.    printf("\n");  
172.  
173.    return 0;  
174.}  
175.  
176.int true_ballqueue(ListQueue *p)  
177.{  
178.    struct _node_ *q = p->front->next;  
179.    int i = 0;  
180.      
181.    for(i = 1;i <= 27;i ++)  
182.    {  
183.        if(q->data != i)  
184.            return 0;  
185.        q = q->next;  
186.    }  
187.  
188.    return 1;  
189.}  
190.  
191.//解决球钟问题  
192.int main()  
193.{  
194.    Stack *mStack,*fmStack,*hStack;  
195.    ListQueue *ballQueue;  
196.    int data;  
197.    int time = 0;  
198.  
199.    //队列、栈初始化  
200.    stack_init(&mStack);  
201.    stack_init(&fmStack);  
202.    stack_init(&hStack);  
203.    queue_init(&ballQueue);  
204.  
205.    //给队列装球  
206.    int i = 0;  
207.    for(i = 1;i <= 27;i ++)  
208.    {  
209.        push_queue(ballQueue,i);  
210.    }  
211.      
212.  
213.    while(1)  
214.    {  
215.        //从球队列出球进入分钟指示器  
216.        data = pop_queue(ballQueue);  
217.        if(mStack->num == 3)  
218.        {  
219.            int i = 0;  
220.            int temp;  
221.          
222.            //分钟指示器的球进入球队列  
223.            for(i = 0;i < 4;i ++)  
224.            {  
225.                temp = pop_stack(mStack);  
226.                push_queue(ballQueue,temp);  
227.            }  
228.              
229.            if(fmStack->num == 10)  
230.            {  
231.                //5分钟指示器的球进入球队列  
232.                for(i = 0;i < 11;i ++)  
233.                {  
234.                    temp = pop_stack(fmStack);  
235.                    push_queue(ballQueue,temp);  
236.                }  
237.  
238.                if(hStack->num == 10)  
239.                {  
240.                      
241.                    //小时指示器的球进入球队列  
242.                    for(i = 0;i < 11;i ++)  
243.                    {  
244.                        temp = pop_stack(hStack);  
245.                        push_queue(ballQueue,temp);  
246.                    }  
247.                      
248.                    push_queue(ballQueue,data);  
249.                      
250.                    time ++;  
251.  
252.                    if(true_ballqueue(ballQueue))  
253.                    {  
254.                        break;  
255.                    }  
256.                  
257.                }else{  
258.                    push_stack(hStack,data);  
259.                }  
260.              
261.            }else{  
262.                push_stack(fmStack,data);  
263.            }  
264.          
265.        }else{  
266.              
267.            push_stack(mStack,data);  
268.  
269.        }  
270.  
271.    }  
272.  
273.    printf("time = %d\n",time);  
274.      
275.    return 0;  
276.}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值