-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1_stack.c
78 lines (71 loc) · 1.33 KB
/
1_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
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
#include <stdlib.h>
typedef struct s_node {
void *content;
struct s_node *next;
} Node;
typedef struct s_stack {
struct s_node *top;
} Stack;
struct s_stack *init(void)
{
Stack *s = malloc(sizeof(Stack));
s->top = NULL;
return (s);
}
void *pop(struct s_stack *stack)
{
void *data;
Node *temp;
if (stack && stack->top)
{
temp = stack->top;
stack->top = stack->top->next;
data = temp->content;
free(temp);
return (data);
} else {
return (NULL);
}
}
void push(struct s_stack *stack, void *content)
{
Node *temp = malloc(sizeof(Node));
temp->content = content;
temp->next = NULL;
if (stack && !stack->top){
stack->top = temp;
} else if (stack->top) {
temp->next = stack->top;
stack->top = temp;
}
}
void *peek(struct s_stack *stack)
{
if (stack && stack->top)
return stack->top->content;
return (NULL);
}
int isEmpty(struct s_stack *stack)
{
if (stack && !stack->top)
return (1);
return (0);
}
/*
#include <stdio.h>
int main(void)
{
Stack *s = init();
printf("I, P;%d\n", (int)pop(s));
push(s, (void *)10);
printf("P 10, P:%d\n", (int)pop(s));
push(s, (void *)100);
printf("P 100, P:%d\n", (int)peek(s));
printf("E:%d\n", isEmpty(s));
push(s, (void *)3);
printf("P 3, P:%d\n", (int)peek(s));
pop(s);
printf("pop, Peek:%d\n", (int)peek(s));
pop(s);
printf("pop, Peek:%d\n", (int)peek(s));
}*/