@@ -56,6 +56,10 @@ int linkStackPop(LinkStack *link_stack, int *value) {
56
56
return 0 ;
57
57
}
58
58
59
+ int linkStackIsEmpty (LinkStack * link_stack ) {
60
+ return LINK_STACK_IS_EMPTY (link_stack );
61
+ }
62
+
59
63
void linkStackDump (LinkStack * link_stack ) {
60
64
LinkStackNode * top = link_stack -> top ;
61
65
int index = 0 ;
@@ -322,13 +326,107 @@ void testStackUseQueue() {
322
326
queueStackDestroy (queue_stack );
323
327
}
324
328
329
+ typedef struct {
330
+ LinkStack * main_stack ; // queue sequence
331
+ LinkStack * temp_stack ; // stack sequence
332
+ } StackQueue ;
333
+
334
+ StackQueue * stackQueueCreate () {
335
+ StackQueue * stack_queue = (StackQueue * )malloc (sizeof (StackQueue ));
336
+ if (stack_queue == NULL ) {
337
+ return NULL ;
338
+ }
339
+
340
+ stack_queue -> main_stack = linkStackCreate ();
341
+ stack_queue -> temp_stack = linkStackCreate ();
342
+
343
+ return stack_queue ;
344
+ }
345
+
346
+ void exchangeStack (StackQueue * stack_queue ) {
347
+ int value ;
348
+ while (!linkStackIsEmpty (stack_queue -> temp_stack )) {
349
+ linkStackPop (stack_queue -> temp_stack , & value );
350
+ linkStackPush (stack_queue -> main_stack , value );
351
+ }
352
+ }
353
+
354
+ int stackQueueEnqueue (StackQueue * stack_queue , int value ) {
355
+ return linkStackPush (stack_queue -> temp_stack , value );
356
+ }
357
+
358
+ int stackQueueDequeque (StackQueue * stack_queue , int * value ) {
359
+ if (linkStackIsEmpty (stack_queue -> main_stack )) {
360
+ exchangeStack (stack_queue );
361
+ }
362
+ return linkStackPop (stack_queue -> main_stack , value );
363
+ }
364
+
365
+ int stackQueuePeek (StackQueue * stack_queue ) {
366
+ if (linkStackIsEmpty (stack_queue -> main_stack )) {
367
+ exchangeStack (stack_queue );
368
+ }
369
+ return stack_queue -> main_stack -> top -> data ;
370
+ }
371
+
372
+ int stackQueueIsEmpty (StackQueue * stack_queue ) {
373
+ return linkStackIsEmpty (stack_queue -> main_stack ) && linkStackIsEmpty (stack_queue -> temp_stack );
374
+ }
375
+
376
+ void stackQueueDump (StackQueue * stack_queue ) {
377
+ if (linkStackIsEmpty (stack_queue -> main_stack )) {
378
+ exchangeStack (stack_queue );
379
+ }
380
+ return linkStackDump (stack_queue -> main_stack );
381
+ }
382
+
383
+ void stackQueueDestory (StackQueue * stack_queue ) {
384
+ linkStackDestroy (stack_queue -> temp_stack );
385
+ linkStackDestroy (stack_queue -> main_stack );
386
+ free (stack_queue );
387
+ }
388
+
325
389
void testQueueUseStack () {
390
+ StackQueue * stack_queue = stackQueueCreate ();
391
+ if (stack_queue == NULL ) {
392
+ printf ("stack queue create failed\n" );
393
+ return ;
394
+ }
395
+
396
+ for (int i = 0 ; i < 4 ; i ++ ) {
397
+ int res = stackQueueEnqueue (stack_queue , i );
398
+ if (res < 0 ) {
399
+ printf ("stack queue enqueue %d failed\n" , i );
400
+ }
401
+ }
402
+ stackQueueDump (stack_queue );
403
+
404
+ int value ;
405
+ int res = stackQueueDequeque (stack_queue , & value );
406
+ if (res < 0 ) {
407
+ printf ("stack queue dequeue failed\n" );
408
+ } else {
409
+ printf ("stack queue dequeue value = %d\n" , value );
410
+ }
411
+
412
+ value = stackQueuePeek (stack_queue );
413
+ printf ("stack queue peek value = %d\n" , value );
414
+ stackQueueDump (stack_queue );
415
+
416
+ for (int i = 0 ; i < 3 ; i ++ ) {
417
+ stackQueueDequeque (stack_queue , & value );
418
+ }
419
+
420
+ int is_empty = stackQueueIsEmpty (stack_queue );
421
+ printf ("stack queue is empty %d\n" , is_empty );
326
422
423
+ stackQueueDestory (stack_queue );
327
424
}
328
425
329
426
int main () {
330
427
// testLinkStack();
331
428
// testLinkQueue();
332
- testStackUseQueue ();
429
+ // testStackUseQueue();
430
+ testQueueUseStack ();
333
431
return 0 ;
334
432
}
0 commit comments