-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack.c
46 lines (42 loc) · 1.1 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
/** @file stack.c
* @brief stack source file.
*
* This module implements stack data structure.
* Includes basic functions required to maintain a stack.
*
* @author Itay Keren (itaykeren)
* @author Rotem Bar (rotembar)
*
*/
/* -- Includes -- */
#include "stack.h"
#include "main_aux.h"
#include <stdio.h>
#include <stdlib.h>
/* Initializes a stack */
void stack_initialize(stack* stck ) {
stck->counter = 0;
stck->top = NULL;
}
/* Pushes an element to the top of stack by changing pointers and increasing counter*/
void push(int* data, cell** board, stack* stck) {
element *e = NULL;
e = (element*) (malloc(sizeof(element)));
if (e == NULL) {
memory_error("Memory allocation failed\n");
exit(0);
}
e->data = data;
e->board = board;
e->next = stck->top;
stck->top = e;
stck->counter++;
}
/* Gets the stack top elemnent data by fetching it and decreasing the counter */
element* pop(stack* stck) {
element *e = NULL;
e = stck->top;
stck->top = stck->top->next;
stck->counter--;
return e;
}