stack.c
737 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "stack.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
static int top; /* pierwsze wolne miejsce na stosie */
static int* dane;
static int size;
void init()
{
top = 0;
size = 0;
dane = 0;
}
void finalize()
{
free(dane);
}
void clear()
{
top = 0;
}
void push(int a)
{
if (top >= size) {
int newsize = (size + 1) * 2;
int* ndane = (int*)realloc(dane, newsize * sizeof(int));
if (ndane)
dane = ndane;
else {
free(dane);
abort();
}
fprintf(stderr, "Rozmiar stosu %d -> %d\n", size, newsize);
size = newsize;
}
dane[top++] = a;
}
int pop()
{
assert(top > 0);
return dane[--top];
}