|
|
@ -1,23 +1,28 @@
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
#include "stack.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Stack {
|
|
|
|
void** data;
|
|
|
|
void** data;
|
|
|
|
int top_index;
|
|
|
|
int top_index;
|
|
|
|
int capacity;
|
|
|
|
int capacity;
|
|
|
|
int curr_size;
|
|
|
|
int curr_size;
|
|
|
|
} Stack;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Stack* new_stack(int stack_size) {
|
|
|
|
Stack* new_stack(int stack_size) {
|
|
|
|
Stack* stack = malloc(sizeof(Stack));
|
|
|
|
assert(stack_size > 0);
|
|
|
|
stack->capacity = stack_size;
|
|
|
|
Stack* my_stack = malloc(sizeof(Stack));
|
|
|
|
stack->data = malloc(stack_size);
|
|
|
|
my_stack->capacity = stack_size;
|
|
|
|
stack->top_index = 0;
|
|
|
|
my_stack->data = malloc(stack_size);
|
|
|
|
stack->curr_size = 0;
|
|
|
|
my_stack->top_index = 0;
|
|
|
|
return stack;
|
|
|
|
my_stack->curr_size = 0;
|
|
|
|
|
|
|
|
return my_stack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void push(Stack* stack,void* element) {
|
|
|
|
void stack_push(Stack* stack,void* element) {
|
|
|
|
if (stack->curr_size >= stack->capacity-1) {
|
|
|
|
if (stack->curr_size >= stack->capacity-1) {
|
|
|
|
stack->data = realloc(stack->data, stack->capacity*2);
|
|
|
|
stack->data = realloc(stack->data, stack->capacity*2);
|
|
|
|
stack->capacity *= 2;
|
|
|
|
stack->capacity *= 2;
|
|
|
@ -29,7 +34,8 @@ void push(Stack* stack,void* element) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* pop(Stack* stack) {
|
|
|
|
void* stack_pop(Stack* stack) {
|
|
|
|
|
|
|
|
assert( !(stack_isEmpty(stack)) );
|
|
|
|
stack->top_index--;
|
|
|
|
stack->top_index--;
|
|
|
|
void* to_return = *(stack->data + stack->top_index);
|
|
|
|
void* to_return = *(stack->data + stack->top_index);
|
|
|
|
*(stack->data + stack->top_index) = NULL;
|
|
|
|
*(stack->data + stack->top_index) = NULL;
|
|
|
@ -39,25 +45,18 @@ void* pop(Stack* stack) {
|
|
|
|
return to_return;
|
|
|
|
return to_return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void* peek(Stack* stack) {
|
|
|
|
void* stack_peek(Stack* stack) {
|
|
|
|
void* to_return = *(stack->data + stack->top_index - 1);
|
|
|
|
void* to_return = *(stack->data + stack->top_index - 1);
|
|
|
|
return to_return;
|
|
|
|
return to_return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int size(Stack* stack) {
|
|
|
|
int stack_size(Stack* stack) {
|
|
|
|
return (stack->curr_size);
|
|
|
|
return (stack->curr_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
bool stack_isEmpty(Stack* stack) {
|
|
|
|
Stack* stack = new_stack(10);
|
|
|
|
if (stack_size(stack) == 0) {
|
|
|
|
int a = 2;
|
|
|
|
return true;
|
|
|
|
int b = 3;
|
|
|
|
}
|
|
|
|
int c = 4;
|
|
|
|
return false;
|
|
|
|
push(stack,(void *)&a);
|
|
|
|
|
|
|
|
push(stack,(void *)&b);
|
|
|
|
|
|
|
|
push(stack,(void *)&c);
|
|
|
|
|
|
|
|
printf("%d\n",*( (int *) pop(stack)));
|
|
|
|
|
|
|
|
printf("%d\n",*( (int *) pop(stack)));
|
|
|
|
|
|
|
|
printf("%d\n",*( (int *) pop(stack)));
|
|
|
|
|
|
|
|
// printf("%d\n",size(stack));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|