Skip to content

Instantly share code, notes, and snippets.

@b4284
Created November 30, 2015 16:27
Show Gist options
  • Select an option

  • Save b4284/435e04bdd3f9d96663e1 to your computer and use it in GitHub Desktop.

Select an option

Save b4284/435e04bdd3f9d96663e1 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
// tos_curr is piled with shit
// tos_other is always empty (what a waste)
int *tos_curr, n_curr, *tos_other, n_other;
void stack_push(int **tos, int *n, int k) {
**tos = k;
*tos += 1;
*n += 1;
}
int *stack_pop(int **tos, int *n) {
if (*n > 0) {
*tos -= 1;
*n -= 1;
return *tos;
} else {
return NULL;
}
}
void stack_ptr_swap(int **tos1, int *n1, int **tos2, int *n2) {
int *tos3 = *tos2;
*tos2 = *tos1;
*tos1 = tos3;
int n3 = *n1;
*n1 = *n2;
*n2 = n3;
}
void stack_invert() {
int *k = stack_pop(&tos_curr, &n_curr);
if (NULL == k) {
return;
}
while (k != NULL) {
stack_push(&tos_other, &n_other, *k);
k = stack_pop(&tos_curr, &n_curr);
}
stack_ptr_swap(&tos_curr, &n_curr, &tos_other, &n_other);
}
void queue_put(int k) {
stack_push(&tos_curr, &n_curr, k);
}
int *queue_get() {
stack_invert();
int *k = stack_pop(&tos_curr, &n_curr);
stack_invert();
return k;
}
void queue_init() {
tos_curr = calloc(10000, sizeof *tos_curr);
tos_other = calloc(10000, sizeof *tos_other);
}
void main() {
queue_init();
queue_put(1);
queue_put(2);
queue_put(3);
queue_put(4);
queue_put(5);
printf("%d\n", *queue_get());
printf("%d\n", *queue_get());
queue_put(6);
queue_put(7);
printf("%d\n", *queue_get());
printf("%d\n", *queue_get());
printf("%d\n", *queue_get());
queue_put(8);
printf("%d\n", *queue_get());
printf("%d\n", *queue_get());
printf("%d\n", *queue_get());
printf("%p\n", queue_get());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment