-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
92 lines (65 loc) · 1.23 KB
/
stack.h
File metadata and controls
92 lines (65 loc) · 1.23 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
#include <stdio.h>
#include <stdlib.h>
//_ _ _ _ _ _ _ _ _ _ _ _ _ DEFINITION _ _ _ _ _ _ _ _ _ _ _ _ _
struct stack_node;
typedef struct stack_node stack_node;
struct stack;
typedef struct stack stack;
stack* stack_create();
size_t stack_size(stack* s);
void stack_push(stack* s, int d);
int stack_top(stack* s);
int stack_pop(stack* s);
//_ _ _ _ _ _ _ _ _ _ _ _ _ _IMPLEMENTATION_ _ _ _ _ _ _ _ _ _ _ _ _ _
struct stack
{
size_t size;
stack_node* top;
};
struct stack_node
{
int value;
stack_node* next;
};
stack* stack_create()
{
stack* newStack = (stack*)malloc(sizeof(stack));
newStack->size = 0;
newStack->top = NULL;
return newStack;
}
void stack_push(stack* s, int d)
{
if (s == NULL)
return;
stack_node* newNode = (stack_node*)malloc(sizeof(stack_node));
newNode->next = s->top;
newNode->value = d;
s->top = newNode;
s->size += 1;
}
int stack_top(stack* s)
{
if (s == NULL)
return NULL;
if (s->size == 0)
return 0;
return s->top->value;
}
int stack_pop(stack* s)
{
if (s == NULL)
return NULL;
if (s->size == 0)
return NULL;
int result = s->top->value;
s->top = s->top->next;
s->size -= 1;
return result;
}
size_t stack_size(stack* s)
{
if (s == NULL)
return NULL;
return s->size;
}