-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
72 lines (55 loc) · 1.26 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
/*
* stack.c - Source File
* Implementation of the functions declared in the header file
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "stack.h"
#include "graph.h"
#include "errorHandler.h"
/* --------Functions Deceleration--------- */
void push(graph* , stack* );
graph* pop(stack* );
bool empty(const stack* );
stack* initialize();
/* --------Functions Implementation---------*/
stack* initialize()
{
stack *stk = (stack *)malloc(sizeof(stack));
if(stk == NULL) returnErrorByType(10);
stk -> cnt = 0;
stk -> top = NULL;
return stk;
}
void push(graph* group, stack* stk)
{
elem *newGraphElem;
newGraphElem = (elem *)malloc(sizeof(elem));
if(stk == NULL) returnErrorByType(11);
newGraphElem -> group = group;
newGraphElem -> next = stk -> top;
stk -> top = newGraphElem;
stk -> cnt++;
}
graph* pop(stack* stk)
{
graph *group;
elem *newGraphElem;
if(stk == NULL) returnErrorByType(11);
if(empty(stk)) return NULL;
group = (stk -> top) -> group;
newGraphElem = stk -> top;
stk -> top = (stk -> top) -> next;
stk -> cnt--;
free(newGraphElem);
return group;
}
bool empty(const stack* stk)
{
return stk -> cnt == EMPTY;
}
bool full(const stack *stk)
{
return stk -> cnt == FULL;
}