-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
117 lines (102 loc) · 1.75 KB
/
Copy pathstack.h
File metadata and controls
117 lines (102 loc) · 1.75 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
*
* NUMBERS STACK FUNCTIONS
*
*/
typedef struct Node* pointer;
typedef pointer numStack;
typedef pointer Position;
typedef struct Node
{
float number;
Position next;
};
numStack createNumStack()
{
numStack s = (numStack) malloc(sizeof(struct Node));
if (s!=NULL)
s->next = NULL;
else
printf("Out Of Memory!\n");
return s;
}
void pushNumber(numStack s,float x)
{
Position p = (Position) malloc(sizeof(struct Node));
p->number = x;
p->next = s-> next;
s->next = p;
}
float popNumber(numStack s)
{
Position nodeToPop = s->next;
s-> next = nodeToPop->next;
float number = nodeToPop->number;
free(nodeToPop);
return number;
}
float getTopNumber(numStack s)
{
return s->next->number;
}
int isNumStackEmpty(numStack s)
{
return (s->next == NULL);
}
void deleteNumStack(numStack s)
{
while (!isNumStackEmpty(s))
popNumber(s);
free(s);
}
/**
*
* FUNCTIONS FOR CHAR STACKS
*
*/
typedef struct node* ptr;
typedef ptr stack ;
typedef ptr position;
typedef struct node
{
char element ;
position next;
};
stack createStack()
{
stack s = (stack) malloc(sizeof(struct node));
if (s!=NULL)
s->next= NULL;
else
printf("out of memory\n");
return s;
}
void push(stack s,char x)
{
position p = (position) malloc(sizeof(struct node));
p->element = x;
p->next = s->next;
s->next = p;
}
float pop(stack s)
{
position nodeToPop = s->next;
s-> next = nodeToPop->next;
char x = nodeToPop->element;
free(nodeToPop);
return x;
}
char getTop(stack s)
{
return s->next->element;
}
int isEmpty(stack s)
{
return (s->next == NULL);
}
void deleteStack(stack s)
{
while (!isEmpty(s))
pop(s);
free(s);
}