Skip to content

Commit

Permalink
made it easier to change the stack test size, added linked-list imple…
Browse files Browse the repository at this point in the history
…mentation by Eric
  • Loading branch information
AJ ONeal committed Aug 17, 2011
1 parent 5754171 commit 41fb3a2
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 6 deletions.
2 changes: 1 addition & 1 deletion stack/Makefile
Expand Up @@ -2,6 +2,6 @@ all:
@rm -rf build; mkdir -p build
cd build; cmake ../
cd build; make
./build/stack-test
valgrind ./build/stack-test

.PHONY: all
11 changes: 6 additions & 5 deletions stack/stack-test.c
Expand Up @@ -10,6 +10,7 @@ void fail(char* msg) {
}

int main(int argc, char* argv[]) {
int max_len = 100;
char* a = "abc";
char* d = "def";
char* g = "ghijklmnopqrstuvwxyz";
Expand Down Expand Up @@ -58,7 +59,7 @@ int main(int argc, char* argv[]) {
// Test that stack can hold any number of elements
//
len = 1;
while (len < 99) {
while (len < (max_len - 1)) {
len += 1;

if (len != s_push(s, (void*) d)) {
Expand All @@ -78,11 +79,11 @@ int main(int argc, char* argv[]) {
//
// Test edge case of last element inserted and removed
//
if (100 != s_push(s, (void*) g)) {
if (max_len != s_push(s, (void*) g)) {
fail("stack length is not 100");
}

if (100 != s_length(s)) {
if (max_len != s_length(s)) {
fail("stack is not length 100 after adding 100 items");
}

Expand All @@ -94,15 +95,15 @@ int main(int argc, char* argv[]) {
fail("stack item 1 doesn't match peek");
}

if (99 != s_length(s)) {
if ((max_len - 1) != s_length(s)) {
fail("stack is not length 99 after removing 1 item");
}


//
// Test that the stack can be emptied as it was filled
//
len = 99;
len = max_len - 1;
while (len > 1) {
len -= 1;

Expand Down
81 changes: 81 additions & 0 deletions stack/stack.reference.eric.c
@@ -0,0 +1,81 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* *
* Name: Eric Jacobs *
* What: Stack Implementation *
* Date: Aug 16, 2011 *
* File: stack.c *
* Version:0.1 *
* *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <stdio.h>
#include <stdlib.h>
#include "include/stack.h"

// the implementation of the stack
struct Stack {
struct Node* top;
int length;
};

// Node for the linked list which will make the Stack.
struct Node {
struct Node* prev;
void* v;
};

// Initialize a Node.
struct Node* node_create(struct Node* prev, void* content);

struct Node* node_create(struct Node* prev, void* v) {
struct Node* n = (struct Node*)malloc(sizeof(struct Node));
n->prev = prev;
n->v = v;
return n;
}

struct Stack* s_create() {
struct Stack* s = (struct Stack*)malloc(sizeof(struct Stack));

s->top = NULL;
s->length = 0;
return s;
}

int s_length(struct Stack* s) {
return s->length;
}

int s_push(struct Stack* s, void* v) {
struct Node* new_node = node_create(s->top, v);
s->top = new_node;
return ++s->length;
}

void* s_pop(struct Stack* s) {
if(s->top == NULL) {
return NULL;
}
struct Node* node_tbd = s->top;
void* v = node_tbd->v;
s->top = node_tbd->prev;
free(node_tbd);
node_tbd = NULL;
s->length--;
return v;
}

void* s_peek(struct Stack* s) {
if(s->top == NULL) {
return NULL;
}
return s->top->v;
}

void s_destroy(struct Stack* s) {
while(s->length > 0) {
s_pop(s);
}
free(s);
s = NULL;
}

0 comments on commit 41fb3a2

Please sign in to comment.