Skip to content

Instantly share code, notes, and snippets.

@gnugat
Last active December 21, 2015 22:48
Show Gist options
  • Save gnugat/6377306 to your computer and use it in GitHub Desktop.
Save gnugat/6377306 to your computer and use it in GitHub Desktop.
Basic Stack - A C basic implementation
# Compiled file
a.out

Basic Stack

A C basic implementation (see Igor Wiedler's blog post).

Usage

Compile everything and run!

cc *.c
./a.out

Current status

  • typedef the struct
  • refactor into a struct
  • create pop instruction
  • create push instruction
  • create stack values
  • create stack index
  • create Unit Test framework

Note: The current status is a stack.

#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include "assert.h"
void assert(bool assertion)
{
char message[ASSERTION_MESSAGE_LENGTH];
strcpy(message, ASSERTION_FAILURE);
if (assertion)
{
strcpy(message, ASSERTION_SUCCESS);
}
write(STDOUT_FILENO, message, ASSERTION_MESSAGE_LENGTH);
}
#ifndef ASSERT_H_
# define ASSERT_H_
# define ASSERTION_SUCCESS "Success\n"
# define ASSERTION_FAILURE "Failure\n"
# define ASSERTION_MESSAGE_LENGTH 9
void assert(bool assertion);
#endif /* !ASSERT_H_ */
#include "assert_index_decremented.h"
void assert_index_decremented(int index_before, int index_after)
{
assert(index_after == index_before - 1);
}
#ifndef ASSERT_INDEX_DECREMENTED_H_
# define ASSERT_INDEX_DECREMENTED_H_
void assert_index_decremented(int index_before, int index_after);
#endif /* !ASSERT_INDEX_DECREMENTED_H_ */
#include "assert_index_incremented.h"
void assert_index_incremented(int index_before, int index_after)
{
assert(index_after == index_before + 1);
}
#ifndef ASSERT_INDEX_INCREMENTED_H_
# define ASSERT_INDEX_INCREMENTED_H_
void assert_index_incremented(int index_before, int index_after);
#endif /* !ASSERT_INDEX_INCREMENTED_H_ */
#include "assert_value_added.h"
void assert_value_added(int *values, int index, int value)
{
assert(value == values[index]);
}
#ifndef ASSERT_VALUE_ADDED_H_
# define ASSERT_VALUE_ADDED_H_
void assert_value_added(int *values, int index, int value);
#endif /* !ASSERT_VALUE_ADDED_H_ */
#include <stdlib.h>
#include <stdbool.h>
#define STACK_LENGTH 42
int main(int argc, char *argv[])
{
int index_before;
int index;
int values[STACK_LENGTH];
int value;
int popped_value;
index = 0;
index_before = index;
value = 1337;
stack_push(values, &index, value);
assert_index_incremented(index_before, index);
assert_value_added(values, index - 1, value);
index_before = index;
popped_value = stack_pop(values, &index);
assert_index_decremented(index_before, index);
assert(popped_value == value);
return EXIT_SUCCESS;
}
#include "stack_pop.h"
int stack_pop(int *values, int *index)
{
return values[--(*index)];
}
#ifndef STACK_POP_H_
# define STACK_POP_H_
int stack_pop(int *values, int *index);
#endif /* !STACK_POP_H_ */
#include "stack_push.h"
void stack_push(int *values, int *index, int value)
{
values[(*index)++] = value;
}
#ifndef STACK_PUSH_H_
# define STACK_PUSH_H_
void stack_push(int *values, int *index, int value);
#endif /* !STACK_PUSH_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment