aboutsummaryrefslogtreecommitdiffstats
path: root/stack.c
blob: 8393a2a239d8b129ef1571443bd82714d9918ab9 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

stack* stack_new() {
    stack* s = (stack*)malloc(sizeof(stack));
    s->size = 0;
    s->maxsize = 1024;
    s->start = (stackitem*)malloc(sizeof(stackitem) * s->maxsize);
    return s;
}

void exitunderflow() {
    fprintf(stderr, "Error Stack Underflow");
    exit(1);
}

stackitem stack_pop(stack* s) {
    if (s->size > 0) {
        s->size = s->size - 1;
        stackitem si = s->start[s->size];
        return si;
    } else {
        // tried to pop empty stack
        exitunderflow();
        return 0;
    }
}

stackitem stack_peek(stack* s) {
    if (s->size > 0) {
        return s->start[s->size - 1];
    } else {
        // tried to pop empty stack
        exitunderflow();
        return 0;
    }
}

void stack_push(stack *s, stackitem si) {
//        fprintf(stderr, "pushing %d", si);
    if (s->size >= s->maxsize) {
        fprintf(stderr, "Error: Stack Overflow");
        exit(1);
    } else {
        s->start[s->size] = si;
        s->size = s->size + 1;
    }
}

int stack_depth(stack* s) {
    return s->size;
}

/**
 * Appends string representing whole stack to the string buffer passed in
 */
void stack_tostringappend(stack* s, int sbmaxlen, char* sb) {
    int i = strlen(sb);
    if (i >= sbmaxlen) {
        fprintf(stderr, "Error: Output would overflow buffer if printed");
        exit(0); 
    }
    sprintf(&(sb[i]), "<%d>", s->size);
    i = strlen(sb);
    if (i >= sbmaxlen) {
        fprintf(stderr, "Error: Output would overflow Buffer if printed");
        exit(0); 
    }
    for (int j = 0; j < s->size; j++) {
        sprintf(&(sb[i]), " %d", s->start[j]);
        i = strlen(sb);
        if (i >= sbmaxlen) {
            fprintf(stderr, "Error: Output would overflow Buffer if printed");
            exit(0); 
        }
    }
    sprintf(&(sb[i]), "\n");
}

void stack_roll(stack* s) {
    int posfromtop = stack_pop(s); // top is 0, bottom is s->size - 1
    int maxindex = s->size - 1;
    int i = maxindex - posfromtop;
    if (i >= 0) {
        int newtop = s->start[i];
        for (; i + 1 < s->size; i++) {
            s->start[i] = s->start[i+1];
        }
        s->start[i] = newtop;
    } else {
        // tried to pop empty stack
        exitunderflow();
    }
}

void stack_pick(stack *s) {
    int posfromtop = stack_pop(s); // top is 0, bottom is s->size - 1
    int maxindex = s->size - 1;
    if (s->size > posfromtop) {
        stack_push(s, s->start[maxindex - posfromtop]);
    } else {
        // tried to pop empty stack
        exitunderflow();
    }
}

void stack_clear(stack* s) {
    s->size = 0;
}