使用两个栈实现一个队列:
如图所示数据入栈,队列是队尾进数据,队头删数据,而在栈中,我们无法在对头删除数据,因此,需要把S1里的n-1个数据放到S2中,然后删除S1中的数据,在将S2中的数据放回到S1中。
stack.c
#include"stack.h" void stackinit(stack *s) { assert(s); s->_size = 0; } void stackpush(stack *s, DataType data) { assert(s); if (s->_size == MAX_SZIE) return; s->_arr[s->_size] = data; s->_size += 1; } void stackpop(stack *s) { assert(s); if (s->_size == 0) return; s->_size -= 1; } DataType stacktop(stack *s) { assert(s); if (s->_size == 0) exit(0); return s->_arr[s->_size - 1]; } void stackprint(stack *s) { int i = 0; assert(s); if (s->_size == 0) return; for (i = 0;i < s->_size;i++) { printf("%d-> ",s->_arr[i]); } printf("\n"); } ////////////////////////////////////// void queuestackinit(queuestack *s) { assert(s); stackinit(&s->s1); stackinit(&s->s2); } void queuestackpop(queuestack *s) { int i = 0; assert(s); while(s->s2._size) { stackpush(&s->s1,stacktop(&s->s2)); stackpop(&s->s2); } while(s->s1._size > 1) { stackpush(&s->s2,stacktop(&s->s1)); stackpop(&s->s1); } printf("%d->",stacktop(&s->s1)); stackpop(&s->s1); } void queuestackpush(queuestack *s, int data) { assert(s); if (s->s1._size == MAX_SZIE) return; stackpush(&s->s1,data); }
stack.h
#include<stdlib.h> #include<assert.h> #define MAX_SZIE 10 typedef int DataType; typedef struct stack { DataType _arr[MAX_SZIE]; int _size; }stack; typedef struct minstack { struct stack s1; struct stack s2; }queuestack; void stackinit(stack *s); void stackpush(stack *s, DataType data); void stackpop(stack *s); DataType stacktop(stack *s); void stackprint(stack *s); ////////////////////////////////////// void queuestackinit(queuestack *s); void queuestackpop(queuestack *s); void queuestackpush(queuestack *s, int data);
test.c
#include"stack.h" test() { queuestack s; queuestackinit(&s); queuestackpush(&s, 1); queuestackpush(&s, 2); queuestackpush(&s, 3); queuestackpush(&s, 4); queuestackpush(&s, 5); queuestackpush(&s, 6); queuestackpop(&s); queuestackpop(&s); queuestackpop(&s); queuestackpop(&s); queuestackpop(&s); queuestackpop(&s); } int main() { test(); system("pause"); return 0; }
2:使用两个队列实现一个栈
H
#pragma once #include<stdio.h> #include<stdlib.h> #include<assert.h> #define MAX_QUEUE 100 typedef int DataType; typedef struct queue { DataType _arr[MAX_QUEUE]; int _front; int _rear; }queue; typedef struct stack { struct queue q1; struct queue q2; }stack; void queue_init(queue *q);//初始化 void queue_en(queue *q, DataType data);//入队列 void queue_de(queue *q);//出队列 int queue_Empty(queue *q);//队列判空 get_head(queue *q);//获取队头元素 void queue_print(queue *q); ////////////////////////////////////////////// void stackqueue2init(stack *q); void stackqueue2push(stack *q,DataType data); void stackqueue2pop(stack *q); void stackqueue2top(stack *q); int stackqueue2empty(stack *q);
C
#include"stackqueue2.h" void queue_init(queue *q) { //构造一个空队列 q->_front = q->_rear = 0; printf("初始化已完成!\n"); } void queue_en(queue *q, DataType data) { if (q->_rear == MAX_QUEUE) { printf("队列已满!\n"); return; } q->_arr[q->_rear] = data; q->_rear += 1; } void queue_de(queue *q)//出队列 { if (!queue_Empty(q)) { printf("队列为空!"); return; } q->_front += 1; } int queue_Empty(queue *q) { if (q->_front == q->_rear) { return 0; } return 1; } int get_head(queue *q)//获取队头元素 { if (!queue_Empty(q)) { printf("队列为空"); return; } return q->_arr[q->_front]; } void queue_print(queue *q) { int i = 0; if (q->_front == q->_rear) { printf("队列为空!\n"); return; } printf("开始打印:\n"); for (i = q->_front;i<q->_rear;i++) { printf("%d ", q->_arr[i]); } printf("\n"); } ////////////////////////////////////// void stackqueue2init(stack *q) { assert(q); queue_init(&(q->q1)); queue_init(&(q->q2)); } void stackqueue2push(stack *q, DataType data) { assert(q); queue_en(&(q->q1), data); } void stackqueue2pop(stack *q) { assert(q); if (stackqueue2empty(q)) { printf("队列为空!!!"); return; } else { if (q->q2._front == q->q2._rear) { while (q->q1._front +1 != q->q1._rear) { queue_en(&q->q2,get_head(&q->q1)); queue_de(&q->q1); } printf("%d->",get_head(&q->q1)); queue_de(&q->q1); } else { while (q->q2._front + 1 != q->q2._rear) { queue_en(&q->q1, get_head(&q->q2)); queue_de(&q->q2); } printf("%d->", get_head(&q->q2)); queue_de(&q->q2); } } } int stackqueue2empty(stack *q) { assert(q); if (queue_Empty(&q->q1) && queue_Empty(&q->q2)) { return 1; } return 0; } void stackqueue2top(stack *q); int main() { stack q; stackqueue2init(&q); stackqueue2push(&q, 1); stackqueue2push(&q, 2); stackqueue2push(&q, 3); stackqueue2push(&q, 4); stackqueue2push(&q, 5); stackqueue2pop(&q); stackqueue2pop(&q); stackqueue2pop(&q); stackqueue2pop(&q); stackqueue2pop(&q); system("pause"); return 0; }
3 判断入栈顺序的合法性
h
void InOutisvalid(Stack *S,int *Inorder,int Insize,int *Outorder,int Outsize);
c
void InOutisvalid(Stack *S,int *Inorder,int Insize,int *Outorder,int Outsize) { int In = 0; int Out = 0; while (Out < Outsize) { while (StackEmpty(S) || StackTop(S) != Outorder[Out]) { StackTop(S); printf("Out元素为:%d\n", Outorder[Out]); if ((In < Insize)) { StackPush(S, Inorder[In++]); } else { printf("不合理!\n"); return; } } printf("此时栈顶元素为:%d\n", S->_arr[S->_top]); StackPop(S); Out += 1; } printf("合理!\n"); }
test
test() { Stack S; StackInit(&S); int Inorder[] = {1,2,3,4,5,}; int Outorder[] = {4,5,3,2,1}; int Insize = sizeof(Inorder) / sizeof(Inorder[0]); int Outsize = sizeof(Outorder) / sizeof(Outorder[0]); InOutisvalid(&S,&Inorder,Insize,&Outorder, Outsize); } int main() { test(); system("pause"); }