-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.c
More file actions
47 lines (41 loc) · 673 Bytes
/
stack.c
File metadata and controls
47 lines (41 loc) · 673 Bytes
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
#include <stdlib.h>
#include"stack.h"
void init(stack *s) {
*s = NULL;
}
int isempty(stack *s) {
return *s == NULL;
}
int isfull(stack *s) {
return 0;
}
void push(stack *s, int num) {
node *temp = (node *) malloc(sizeof(node));
node *tempnode = *s;
temp->val = num;
temp->next = NULL;
if(!*s)
*s = temp;
else {
while(tempnode->next)
tempnode = tempnode->next;
tempnode->next = temp;
}
}
int pop(stack *s) {
int ret;
node *temp = *s;
if((*s)->next == NULL) {
ret = (*s)->val;
free(*s);
*s = NULL;
}
else {
while(temp->next->next)
temp = temp->next;
ret = temp->next->val;
free(temp->next);
temp->next = NULL;
}
return ret;
}