[ create a new paste ] login | about

Link: http://codepad.org/n1XiZMZn    [ raw code | output | fork ]

C, pasted on Mar 12:
#include <stdio.h>
#include <stdlib.h>

#define N 10
#define T int
static T hairetsu[N];
static int sp = 0;

void push1(T n) {
  if (sp < N) {
    hairetsu[sp] = n;
    sp++;
  }
}
T pop1(int *n) {
  if (sp > 0) {
    sp--;
    *n = hairetsu[sp];
    return 1;
  }
  return 0;
}

struct stack {
  T data;
  struct stack *next;
};
static struct stack *root = NULL;
void push2(T n) {
  struct stack *p;
  if ((p = malloc(sizeof(struct stack)))) {
    p->data = n;
    p->next = root;
    root = p;
  }
}
int pop2(T *n) {
  struct stack *p;
  if (root) {
    *n = root->data;
    p = root;
    root = root->next;
    free(p);
    return 1;
  }
  return 0;
}

int main() {
  int n;
  push1(1);
  push1(2);
  push1(3);
  push1(4);
  push1(5);
  if (pop1(&n))
    printf("n = %d\n", n);
  if (pop1(&n))
    printf("n = %d\n", n);
  if (pop1(&n))
    printf("n = %d\n", n);
  if (pop1(&n))
    printf("n = %d\n", n);
  if (pop1(&n))
    printf("n = %d\n", n);
  if (pop1(&n))
    printf("n = %d\n", n);
  push2(1);
  push2(2);
  push2(3);
  push2(4);
  push2(5);
  if (pop2(&n))
    printf("n = %d\n", n);
  if (pop2(&n))
    printf("n = %d\n", n);
  if (pop2(&n))
    printf("n = %d\n", n);
  if (pop2(&n))
    printf("n = %d\n", n);
  if (pop2(&n))
    printf("n = %d\n", n);
  if (pop2(&n))
    printf("n = %d\n", n);
  return 0;
}
/* end */


Output:
1
2
3
4
5
6
7
8
9
10
n = 5
n = 4
n = 3
n = 2
n = 1
n = 5
n = 4
n = 3
n = 2
n = 1


Create a new paste based on this one


Comments: