Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #define N 10
- static int S[N], Sp;
- static int Q[N], Hp, Tp, Num;
- static void stack_init(void);
- static void stack_push(int v);
- static bool stack_empty(void);
- static bool stack_filled(void);
- static int stack_top(void);
- static void stack_pop(void);
- static void stack_display(void);
- static void queue_init(void);
- static void queue_push(int v);
- static bool queue_empty(void);
- static bool queue_filled(void);
- static int queue_front(void);
- static void queue_pop(void);
- static void queue_display(void);
- static void init(void);
- static void display(void);
- static int continue_or(void);
- static void stack_to_queue(void);
- static void queue_to_stack(void);
- static void move(int dir);
- int main(void) {
- init();
- display();
- for (int dir = continue_or(); dir; dir = continue_or()) {
- move(dir);
- display();
- }
- }
- static void init(void) {
- stack_init();
- {
- stack_push(45);
- stack_push(16);
- stack_push(58);
- //stack_push(1);
- //stack_push(2);
- //stack_push(3);
- //stack_push(4);
- }
- queue_init();
- {
- queue_push(0); // dummy
- queue_push(0); // dummy
- queue_push(35);
- queue_push(94);
- queue_push(67);
- queue_push(40);
- queue_pop();
- queue_pop();
- }
- }
- static void display(void) {
- stack_display();
- queue_display();
- }
- static int continue_or(void) {
- int v;
- printf("? ");
- fflush(stdout);
- scanf("%d", &v);
- return v;
- }
- static void move(int dir) {
- if (dir >= 0) {
- stack_to_queue();
- return;
- }
- queue_to_stack();
- }
- static void stack_to_queue(void) {
- if (stack_empty()) {
- return;
- }
- int v = stack_top();
- stack_pop();
- if (queue_filled()) {
- stack_push(v);
- return;
- }
- queue_push(v);
- }
- static void queue_to_stack(void) {
- if (queue_empty()) {
- return;
- }
- int v = queue_front();
- queue_pop();
- if (stack_filled()) {
- queue_push(v);
- return;
- }
- stack_push(v);
- }
- static void stack_init(void) {
- for (size_t i = 0; i < N; i++) {
- S[i] = -1;
- }
- Sp = -1;
- }
- static void stack_push(int v) {
- S[++Sp] = v;
- }
- static bool stack_empty(void) {
- return Sp < 0;
- }
- static bool stack_filled(void) {
- return Sp >= N - 1;
- }
- static int stack_top(void) {
- return S[Sp];
- }
- static void stack_pop(void) {
- S[Sp--] = -1;
- }
- static void stack_display(void) {
- printf("S: ");
- for (size_t i = 0; i < N; i++) {
- if (i > 0) {
- printf(", ");
- }
- printf("[%zd]%d", i, S[i]);
- }
- printf("\nSp = %d\n", Sp);
- }
- static void queue_init(void) {
- for (size_t i = 0; i < N; i++) {
- Q[i] = -1;
- }
- Hp = Num = 0;
- Tp = -1;
- }
- static void queue_push(int v) {
- Q[++Tp] = v;
- Tp %= N;
- Num++;
- }
- static bool queue_empty(void) {
- return Num == 0;
- }
- static bool queue_filled(void) {
- return Num >= N;
- }
- static int queue_front(void) {
- return Q[Hp];
- }
- static void queue_pop(void) {
- Q[Hp++] = -1;
- Hp %= N;
- Num--;
- if (!Num) {
- Hp = 0;
- Tp = -1;
- }
- }
- static void queue_display(void) {
- printf("Q: ");
- for (size_t i = 0; i < N; i++) {
- if (i > 0) {
- printf(", ");
- }
- printf("[%zd]%d", i, Q[i]);
- }
- printf("\nHp = %d, Tp = %d, Num = %d\n", Hp, Tp, Num);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement