-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.h
More file actions
135 lines (114 loc) · 3.21 KB
/
stack.h
File metadata and controls
135 lines (114 loc) · 3.21 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
#define true 1
#define false 0
#define initSize 4
typedef int bool;
typedef struct{
int *arr;
int top;
int size;
}Stack;
void createStack(Stack **stack){
(*stack) = malloc(sizeof(Stack));
(*stack)->size = initSize;
(*stack)->top = -1;
(*stack)->arr = malloc((*stack)->size * sizeof(int));
}
void increaseStackSize(Stack *stack){
stack->size = stack->size * 2;
stack->arr = realloc(stack->arr, sizeof(int) * stack->size);
}
void decreaseStackSize(Stack *stack){
stack->size = stack->size / 2;
stack->arr = realloc(stack->arr, sizeof(int) * stack->size);
}
void push(Stack *stack, int element){
if(isStackFull(stack))
increaseStackSize(stack);
stack->top = stack->top + 1;
stack->arr[stack->top] = element;
}
int pop(Stack *stack){
if(!isStackEmpty(stack)){
if(stack->size / 2 > stack->top)
decreaseStackSize(stack);
int toBeReturned = stack->arr[stack->top];
stack->top = stack->top - 1;
return toBeReturned;
}
return NULL;
}
int peek(Stack *stack){
if(!isStackEmpty(stack)){
return stack->arr[stack->top];
}
return NULL;
}
bool isStackFull(Stack *stack){
return stack->top == stack->size - 1 ? true : false;
}
bool isStackEmpty(Stack *stack){
return stack->top == -1 ? true : false;
}
void printStack(Stack *stack){
for(int i=0; i<=stack->top; i++){
printf("%d ", stack->arr[i]);
}
printf("\n");
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////// ALGORITHMS ////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void _insertAtBottom(Stack *stack, int element){
if(isStackEmpty(stack))
push(stack, element);
else{
int temp = pop(stack);
_insertAtBottom(stack, element);
push(stack, temp);
}
}
void reverseStack(Stack *stack){
if(!isStackEmpty(stack)){
int temp = pop(stack);
reverseStack(stack);
_insertAtBottom(stack, temp);
}
}
void _sortedInsert(Stack *stack, int element){
if(isStackEmpty(stack) || element > peek(stack)){
push(stack, element);
return;
}
int temp = pop(stack);
_sortedInsert(stack, element);
push(stack, temp);
}
void sortStackRecursively(Stack *stack){
if(!isStackEmpty(stack)){
int temp = pop(stack);
sortStackRecursively(stack);
_sortedInsert(stack, temp);
}
}
void sortStack(Stack **stack){
Stack *tmpStack;
createStack(&tmpStack);
while(!isStackEmpty(*stack)){
int temp = pop(*stack);
while(!isStackEmpty(tmpStack) && peek(tmpStack) > temp)
push(*stack, pop(tmpStack));
push(tmpStack, temp);
}
*stack = tmpStack;
}
void deleteMiddleValueFromStack(Stack *stack, int curr, int n){
if(isStackEmpty(stack) || curr == n)
return;
int temp = pop(stack);
deleteMiddleValueFromStack(stack, curr+1, n);
if(curr != n/2)
push(stack, temp);
}
#endif // STACK_H_INCLUDED