-
Notifications
You must be signed in to change notification settings - Fork 0
/
crack_3_1_list.c
91 lines (80 loc) · 1.34 KB
/
crack_3_1_list.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
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
typedef struct Stack {
int size;
Node *top;
} Stack;
Node *newNode();
Node *initNode(int);
Stack *initStack();
void push(Node *, Stack *);
Node *pop(Stack *);
void clear(Stack *);
void printAll(Stack *);
main()
{
int test[] = {1,2,3,4,5};
int n = 0;
Stack *stack = initStack();
for (; n < 5; n++) {
Node *nod = initNode(test[n]);
push(nod, stack);
}
pop(stack);
printAll(stack);
return 0;
}
Node *newNode()
{
return (Node *)malloc(sizeof(Node));
}
Node *initNode(int data)
{
Node *nod = (Node *)malloc(sizeof(Node));
nod->data = data;
return nod;
}
Stack *initStack()
{
Stack *stack = (Stack *)malloc(sizeof(Stack));
stack->top = NULL;
stack->size = 0;
return stack;
}
void push(Node *node, Stack *stack)
{
node->next = stack->top;
stack->top = node;
(stack->size)++;
}
Node *pop(Stack *stack)
{
if (stack->top != NULL && stack->size > 0) {
Node *temp = stack->top;
stack->top = temp->next;
(stack->size)--;
return temp;
}
return NULL;
}
void clear(Stack *stack)
{
while (stack->top != NULL) {
Node *temp = stack->top;
free(stack->top);
stack->top= temp->next;
(stack->size)--;
}
}
void printAll(Stack *stack)
{
Node *temp = stack->top;
while (temp != NULL) {
printf("%d\n", temp->data);
temp = temp->next;
}
}