Skip to content
Snippets Groups Projects
Verified Commit 8e944960 authored by orestis.malaspin's avatar orestis.malaspin
Browse files

added stack live code

parent ae282f32
No related branches found
No related tags found
No related merge requests found
Pipeline #36435 passed
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#define MIN_CAPACITY 5
struct stack {
int top;
int capacity;
int *data;
};
void stack_init(struct stack *s, int capacity) {
s->top = -1;
s->data = malloc(capacity * sizeof(int));
s->capacity = capacity;
}
bool stack_is_empty(struct stack s) {
return s.top == -1;
}
void stack_push(struct stack *s, int val) {
if (s->top != s->capacity - 1) {
s->top += 1;
s->data[s->top] = val;
} else {
s->capacity *= 2;
s->data = realloc(s->data, s->capacity * sizeof(int));
s->top += 1;
s->data[s->top] = val;
}
}
/*int stack_pop(struct stack *s) {*/
/* if (!stack_is_empty(*s)) {*/
/* int tmp = s->data[s->top];*/
/* s->top -= 1;*/
/* return tmp;*/
/* }*/
/*}*/
/*void stack_pop(struct stack *s, int *val) {*/
/* if (!stack_is_empty(*s)) {*/
/* *val = s->data[s->top];*/
/* s->top -= 1;*/
/* }*/
/*}*/
int *stack_pop(struct stack *s) {
if (!stack_is_empty(*s)) {
int *val = &(s->data[s->top]);
s->top -= 1;
if (s->top < s->capacity / 4 && (s->capacity / 2) > MIN_CAPACITY) {
s->capacity /= 2;
s->data = realloc(s->data, s->capacity * sizeof(int));
}
return val;
} else {
return NULL;
}
}
int *stack_peek(struct stack s) {
if (!stack_is_empty(s)) {
int *val = &(s.data[s.top]);
return val;
} else {
return NULL;
}
}
void stack_print(struct stack s) {
printf("capacity = %d\n", s.capacity);
for (int i = s.top; i >= 0; --i) {
printf("%d\n", s.data[i]);
}
}
int main() {
struct stack s;
stack_init(&s, 5);
stack_print(s);
printf("s.top = %d\n", s.top);
printf("is_empty(): %s\n", stack_is_empty(s) ? "True" : "False");
stack_push(&s, 10);
stack_push(&s, 20);
stack_push(&s, 30);
stack_push(&s, 40);
stack_push(&s, 50);
stack_push(&s, 60);
stack_push(&s, 70);
stack_push(&s, 80);
stack_push(&s, 90);
stack_push(&s, 100);
stack_push(&s, 110);
printf("is_empty(): %s\n", stack_is_empty(s) ? "True" : "False");
stack_print(s);
/*int val = -1;*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*stack_pop(&s, &val);*/
/*printf("popped value = %d\n", val);*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
/*printf("popped value = %d\n", stack_pop(&s));*/
printf("peeked value = %d\n", *stack_peek(s));
printf("peeked value = %d\n", *stack_peek(s));
stack_print(s);
printf("popped value = %d\n", *stack_pop(&s));
printf("popped value = %d\n", *stack_pop(&s));
printf("popped value = %d\n", *stack_pop(&s));
printf("popped value = %d\n", *stack_pop(&s));
printf("popped value = %d\n", *stack_pop(&s));
printf("popped value = %d\n", *stack_pop(&s));
printf("popped value = %d\n", *stack_pop(&s));
printf("popped value = %d\n", *stack_pop(&s));
/*printf("popped value = %d\n", *stack_pop(&s));*/
stack_print(s);
printf("is_empty(): %s\n", stack_is_empty(s) ? "True" : "False");
return EXIT_SUCCESS;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#define MAX_CAPACITY 5
......@@ -18,6 +19,7 @@ bool stack_is_empty(struct stack s) {
}
void stack_push(struct stack *s, int val) {
assert(s->top < MAX_CAPACITY - 1);
if (s->top != MAX_CAPACITY - 1) {
s->top += 1;
s->data[s->top] = val;
......@@ -40,6 +42,8 @@ void stack_push(struct stack *s, int val) {
/*}*/
int *stack_pop(struct stack *s) {
assert(!stack_is_empty(*s));
if (!stack_is_empty(*s)) {
int *val = &(s->data[s->top]);
s->top -= 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment