-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.c
52 lines (48 loc) · 1.49 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
#include <stdio.h>
#include "Stack.h"
#include "MainAux.h"
#include <stdlib.h>
/*Inserting the elements using push function*/
void push_ele(struct curr_board next_board){
int i,j,k;
struct rec_stack *m;
/* allocate memory for stack */
m = (struct rec_stack*) malloc(sizeof( struct rec_stack));
if (m == NULL) {
memory_error("malloc");
}
/* allocate memory for top board */
m->top.board = (struct cell **) calloc(next_board.len, sizeof(struct cell *));
if (m->top.board == NULL) {
memory_error("calloc");
}
for (i = 0; i < next_board.len; ++i) {
m->top.board[i] = (struct cell *) calloc(next_board.len, sizeof(struct cell));
if (m->top.board[i] == NULL) {
memory_error("calloc");
}
}
for (j = 0; j < next_board.len; ++j) {
for (k = 0; k < next_board.len; ++k)
m->top.board[j][k].value = next_board.board[j][k].value;
}
m->top.len = next_board.len;
m->top.block_height = next_board.block_height;
m->top.block_width = next_board.block_width;
m->next_stack = stack; /*receives all previous elements stack*/
stack = m;
}
/*Removing the elements using pop function*/
struct curr_board* pop_ele()
{ struct curr_board* my_board;
if (stack == NULL) {
printf("The stack is Empty.");
my_board = (struct curr_board*)NULL;
return my_board;
}
else {
my_board = &(stack->top);
stack = stack->next_stack;
return my_board;
}
}