-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
108 lines (81 loc) · 1.93 KB
/
stack.c
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
#include <stdio.h>
#include <stdlib.h>
#include "matrix.h"
#include "stack.h"
/*======== struct stack * new_stack()) ==========
Inputs:
Returns:
Creates a new stack and puts an identity
matrix at the top.
04/19/12 11:25:19
jdyrlandweaver
====================*/
struct stack * new_stack() {
struct stack *s;
struct matrix **m;
struct matrix *i;
s = (struct stack *)malloc(sizeof(struct stack));
m = (struct matrix **)malloc( STACK_SIZE * sizeof(struct matrix *));
i = new_matrix(4, 4);
ident( i );
s->size = STACK_SIZE;
s->top = 0;
s->data = m;
s->data[ s->top ] = i;
return s;
}
/*======== void push() ==========
Inputs: struct stack *s
Returns:
Puts a new matrix on top of s
The new matrix should be a copy of the curent
top matrix
04/19/12 11:29:04
jdyrlandweaver
====================*/
void push( struct stack *s ) {
struct matrix *m;
m = new_matrix(4, 4);
if ( s->top == s->size - 1 ) {
s->data = (struct matrix **)realloc( s->data, (s->size + STACK_SIZE)
* sizeof(struct matrix *));
s->size = s->size + STACK_SIZE;
}
copy_matrix( s->data[ s->top ], m);
s->top++;
s->data[ s->top ] = m;
}
/*======== void pop() ==========
Inputs: struct stack * s
Returns:
Remove and free the matrix at the top
Note you do not need to return anything.
04/19/12 11:33:12
jdyrlandweaver
====================*/
void pop( struct stack * s) {
free_matrix(s->data[s->top]);
s->top--;
}
/*======== void free_stack() ==========
Inputs: struct stack *s
Returns:
Deallocate all the memory used in the stack
04/19/12 11:34:17
jdyrlandweaver
====================*/
void free_stack( struct stack *s) {
int i;
for (i=0; i <= s->top; i++) {
free_matrix(s->data[i]);
}
free(s->data);
free(s);
}
void print_stack(struct stack *s) {
int i;
for (i=s->top; i >= 0; i--) {
print_matrix(s->data[i]);
printf("\n");
}
}