Skip to content

Instantly share code, notes, and snippets.

@adamgavlak
Last active October 4, 2018 16:52
Show Gist options
  • Save adamgavlak/9f7fd88f8e07e233ffafe3e1bd0c431f to your computer and use it in GitHub Desktop.
Save adamgavlak/9f7fd88f8e07e233ffafe3e1bd0c431f to your computer and use it in GitHub Desktop.
Simple dynamic stack implementation in C
//
// stack.c
//
// Created by Adam Gavlák on 26/09/16.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int Element;
typedef struct {
Element *content;
int current_size;
int top;
} Stack;
void initialize(Stack *stack)
{
Element *content;
content = (Element *) calloc(2, sizeof(Element));
if (content == NULL)
{
fprintf(stderr, "Not enough memory to initialize stack\n");
exit(1);
}
stack->content = content;
stack->current_size = 2;
stack->top = -1;
}
void destroy(Stack *stack)
{
free(stack->content);
stack->content = NULL;
stack->current_size = 0;
stack->top = -1;
}
void resize(Stack *stack)
{
Element *resized_content;
resized_content = (Element *) calloc(stack->current_size * 2, sizeof(Element));
memcpy(resized_content, stack->content, sizeof(Element) * stack->top + 1);
free(stack->content);
stack->content = resized_content;
stack->current_size = stack->current_size * 2;
}
int is_empty(Stack *stack)
{
if (stack->top < 0)
return 1;
else
return 0;
}
int is_full(Stack *stack)
{
if (stack->top == stack->current_size - 1)
return 1;
else
return 0;
}
void push(Stack *stack, Element data)
{
if (is_full(stack) == 1)
resize(stack);
stack->top = stack->top + 1;
stack->content[stack->top] = data;
}
Element peek(Stack *stack)
{
return stack->content[stack->top];
}
Element pop(Stack *stack)
{
if (is_empty(stack) == 0)
{
Element data = stack->content[stack->top];
stack->top = stack->top - 1;
return data;
}
fprintf(stderr, "Stack is empty\n");
exit(1);
}
int main(void)
{
Stack *s;
initialize(s);
for (int i = 0; i < 1024; i++) {
push(s, i);
}
printf("%i\n", peek(s));
while (1) {
printf("%i\n", pop(s));
}
return 0;
}
@kamoroso94
Copy link

You never allocated space for s in main, so shouldn't this segfault when you call initialize? Just declare like Stack s; and pass &s to the stack functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment