-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrack_3_3.c
More file actions
61 lines (53 loc) · 1.07 KB
/
crack_3_3.c
File metadata and controls
61 lines (53 loc) · 1.07 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
#include <stdio.h>
#include <stdlib.h>
#define MAXSTACK 100
#define MAXARRAY 2
typedef struct Stack {
int size;
int array[MAXARRAY];
} Stack;
Stack *stacks[MAXSTACK];
int position;
Stack *initStack();
void push(int);
int pop();
int popAt(int);
main()
{
stacks[0] = initStack();
position = 0;
push(0);
push(1);
push(2);
printf("%d\n", position);
int temp = popAt(0);
printf("%d %d\n", temp, position);
return 0;
}
Stack *initStack()
{
Stack *stack = (Stack *)malloc(sizeof(Stack));
stack->size = 0;
return stack;
}
void push(int data)
{
if (stacks[position]->size == MAXARRAY)
stacks[++position] = initStack();
(stacks[position]->array)[stacks[position]->size] = data;
(stacks[position]->size)++;
}
int pop()
{
int temp = position;
if (stacks[position]->size == 1)
return (temp = (stacks[position--]->array)[--(stacks[temp]->size)]);
return (temp = stacks[position]->array[--(stacks[position]->size)]);
}
int popAt(int index)
{
if (index > position) return -1;
while (stacks[index]->size == 0)
--index;
return (stacks[index]->array)[--(stacks[index]->size)];
}