made it easier to change the stack test size, added linked-list implementation by Eric

This commit is contained in:
AJ ONeal 2011-08-17 16:10:36 +00:00
parent 5754171207
commit 41fb3a2c92
3 changed files with 88 additions and 6 deletions

View File

@ -2,6 +2,6 @@ all:
@rm -rf build; mkdir -p build @rm -rf build; mkdir -p build
cd build; cmake ../ cd build; cmake ../
cd build; make cd build; make
./build/stack-test valgrind ./build/stack-test
.PHONY: all .PHONY: all

View File

@ -10,6 +10,7 @@ void fail(char* msg) {
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
int max_len = 100;
char* a = "abc"; char* a = "abc";
char* d = "def"; char* d = "def";
char* g = "ghijklmnopqrstuvwxyz"; char* g = "ghijklmnopqrstuvwxyz";
@ -58,7 +59,7 @@ int main(int argc, char* argv[]) {
// Test that stack can hold any number of elements // Test that stack can hold any number of elements
// //
len = 1; len = 1;
while (len < 99) { while (len < (max_len - 1)) {
len += 1; len += 1;
if (len != s_push(s, (void*) d)) { if (len != s_push(s, (void*) d)) {
@ -78,11 +79,11 @@ int main(int argc, char* argv[]) {
// //
// Test edge case of last element inserted and removed // 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"); 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"); fail("stack is not length 100 after adding 100 items");
} }
@ -94,7 +95,7 @@ int main(int argc, char* argv[]) {
fail("stack item 1 doesn't match peek"); 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"); fail("stack is not length 99 after removing 1 item");
} }
@ -102,7 +103,7 @@ int main(int argc, char* argv[]) {
// //
// Test that the stack can be emptied as it was filled // Test that the stack can be emptied as it was filled
// //
len = 99; len = max_len - 1;
while (len > 1) { while (len > 1) {
len -= 1; len -= 1;

View File

@ -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;
}