-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
39 lines (36 loc) · 790 Bytes
/
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
#include "stack.h"
#include <stdlib.h>
stack createEmptyStack()
{
stack s;
s.header=malloc(sizeof(struct stackNode));
s.header->dlvl=-1;
s.header->v=0;
s.header->next=NULL;
return s;
}
// push a variable to the stack with its decision level
void push(stack s, Variable v, int dlevel)
{
dvar node=malloc(sizeof(struct stackNode));
node->next=s.header->next;
node->v=v;
node->dlvl=dlevel;
s.header->next=node;
}
// pop a variable from the stack
int pop(stack s)
{
dvar temp=s.header->next;
int var=temp->v;
s.header->next=temp->next;
free(temp);
return var;
}
// get the decision level of the top variable in the stack
int getTop(stack s)
{
if (s.header->next==NULL)
return -1;
return s.header->next->dlvl;
}