-
Notifications
You must be signed in to change notification settings - Fork 0
/
crack_3_2.c
108 lines (97 loc) · 1.86 KB
/
crack_3_2.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
typedef struct Stack {
int size;
Node *top;
} Stack;
Node *initNode(int);
Stack *initStack();
void push(Node *, Stack *, Stack *);
void pushMin(Node *, Stack *);
Node *pop(Stack *, Stack *);
Node *popMin(Stack *);
void printAll(Stack *);
main()
{
int test[] = {1,5,2,9,8,3,6,8,0,7,1};
int n = 0;
Stack *stack = initStack();
Stack *min = initStack();
for (; n < 11; n++) {
Node *nod = initNode(test[n]);
push(nod, stack, min);
}
for (; n > 3; n--) {
printf("%d %d\n", (stack->top)->data, (min->top)->data);
pop(stack, min);
}
printAll(min);
return 0;
}
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, Stack *min)
{
if (stack->top == NULL)
pushMin(node, min);
node->next = stack->top;
stack->top = node;
(stack->size)++;
if (node->data <= (min->top)->data) {
Node *temp = initNode(node->data);
pushMin(temp, min);
}
}
void pushMin(Node *node, Stack *min)
{
if (min->top == NULL || node->data < (min->top)->data) {
node->next = min->top;
min->top = node;
(min->size)++;
}
}
Node *pop(Stack *stack, Stack *min)
{
if (stack->top != NULL && stack->size > 0) {
Node *temp = stack->top;
stack->top = temp->next;
(stack->size)--;
if ((stack->top)->data == (min->top)->data)
popMin(min);
return temp;
}
return NULL;
}
Node *popMin(Stack *min)
{
if (min->top != NULL && min->size > 0) {
Node *temp = min->top;
min->top = temp->next;
(min->size)--;
return temp;
}
return NULL;
}
void printAll(Stack *stack)
{
Node *temp = stack->top;
while (temp != NULL) {
printf("%d\n", temp->data);
temp = temp->next;
}
}