-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.h
62 lines (56 loc) · 1.43 KB
/
stack.h
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
/* STACK implemented as a linked list */
#define TRUE 1
#define FALSE 0
// stack declaration
typedef struct stack
{
int info;
struct stack *next;
// a node of the stack contains two information
}*NODEPTR;
// function prototypes
void initStack(NODEPTR *); // initialize the stack
void push(NODEPTR *,int,int *); // to push information to the stack
void pop(NODEPTR *,int *,int *); // to pop an information out of the stack
// function definition
// to initalize the stack
void initStack(NODEPTR *pStack)
{
*pStack = NULL;
}
// to push 'toPush' to the stack
void push(NODEPTR *pStack,int toPush,int *overflow)
{
NODEPTR newNode; // node to push
newNode = malloc(sizeof(struct stack));
if(newNode == NULL) // cannot create space
*overflow = TRUE;
else
{
*overflow = FALSE;
newNode->info = toPush;
if(*pStack == NULL) // stack is empty
newNode->next = NULL;
else
newNode->next = *pStack;
*pStack = newNode;
}
}
// to pop an information out of the stack
void pop(NODEPTR *pStack,int *popped,int *underflow)
{
NODEPTR delNode; // node to remove
if(*pStack == NULL) // empty stack
*underflow = TRUE;
else
{
*underflow = FALSE;
delNode = *pStack;
*popped = delNode->info;
if(delNode->next == NULL) // contains only one node
initStack(pStack);
else
*pStack = delNode->next; // make the next node as the first
free(delNode); // clear space
}
}