-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added my first night's work. The README, ignore file, linked list, an…
…d linked list tests.
- Loading branch information
Stephen Brennan
committed
Sep 13, 2013
0 parents
commit 7ba2657
Showing
11 changed files
with
511 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
syntax: glob | ||
obj/* | ||
bin/* | ||
*~ | ||
syntax: regexp | ||
(.*/)?\#[^/]*\#$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# libstephen makefile | ||
|
||
CC=gcc | ||
FLAGS= | ||
CFLAGS=$(FLAGS) -c -std=c99 | ||
LFLAGS=$(FLAGS) | ||
LIBOBJECTS=obj/linkedlist.o obj/common_include.o | ||
TESTOBJECTS=obj/main.o obj/linkedlisttest.o | ||
|
||
.PHONY: all test lib clean | ||
|
||
# Main targets | ||
|
||
all: test lib | ||
|
||
test: bin/test | ||
|
||
lib: bin/libstephen.a | ||
|
||
clean: | ||
rm -rf obj/* bin/* | ||
|
||
# Main Binaries | ||
|
||
bin/test: $(LIBOBJECTS) $(TESTOBJECTS) | ||
$(CC) $(LFLAGS) $(LIBOBJECTS) $(TESTOBJECTS) -o bin/test | ||
|
||
bin/libstephen.a: $(LIBOBJECTS) | ||
ar rcs bin/libstephen.a $(LIBOBJECTS) | ||
|
||
# Library objects | ||
|
||
obj/linkedlist.o: linkedlist/linkedlist.c linkedlist/linkedlist.h generic.h | ||
$(CC) $(CFLAGS) linkedlist/linkedlist.c -o obj/linkedlist.o | ||
|
||
obj/common_include.o: common_include.c common_include.h | ||
$(CC) $(CFLAGS) common_include.c -o obj/common_include.o | ||
|
||
# Test objects | ||
|
||
obj/main.o: test/main.c test/tests.h | ||
$(CC) $(CFLAGS) test/main.c -o obj/main.o | ||
|
||
obj/linkedlisttest.o: test/linkedlisttest.c test/tests.h linkedlist/linkedlist.h common_include.h | ||
$(CC) $(CFLAGS) test/linkedlisttest.c -o obj/linkedlisttest.o | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
################################################################################ | ||
#################################### README #################################### | ||
################################################################################ | ||
|
||
Library: libstephen | ||
|
||
Description: a static library containing basic generic data structures, and also | ||
some headers that are useful for general development. Some other stuff too, if | ||
I think about it. If I do think of anything else, I'll document it. | ||
|
||
############################### DATA STRUCTURES ################################ | ||
|
||
Linked List | ||
|
||
Function prefix: "ll_" | ||
|
||
The linked list data structure is a bunch of chained nodes. Each node holds my | ||
"generic" data structure and pointers to the next and previous nodes. The | ||
generic data structure can hold the following data types: | ||
|
||
- long long int | ||
- double | ||
- void * | ||
|
||
This is just because using a char, short int, int, long int, or a float would | ||
fill this out, so it would be a little redundant. Type "checking" is all up to | ||
the programmer (i.e. making sure that the values are not too big for the smaller | ||
versions). | ||
|
||
Functions (remember the prefix): | ||
|
||
create() - Returns a pointer to a new linked list. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/******************************************************************************* | ||
File: common_include.c | ||
Auhtor: Stephen Brennan | ||
Date Created: 9/1/2013 | ||
Description: Contains macros and useful things for general purpose | ||
programming. | ||
*******************************************************************************/ | ||
#include "common_include.h" | ||
|
||
static size_t mallocs; | ||
|
||
void smb___helper_inc_malloc_counter(size_t number_of_mallocs) | ||
{ | ||
mallocs += number_of_mallocs; | ||
} | ||
|
||
size_t smb___helper_get_malloc_counter() | ||
{ | ||
return mallocs; | ||
return 0; | ||
} | ||
|
||
void smb___helper_dec_malloc_counter(size_t number_of_frees) | ||
{ | ||
mallocs -= number_of_frees; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/******************************************************************************* | ||
File: common_include.h | ||
Author: Stephen Brennan | ||
Date Created: 9/1/2013 | ||
Description: Contains macros and useful things for general purpose | ||
programming. | ||
*******************************************************************************/ | ||
|
||
#ifndef SMB_MACROS | ||
#define SMB_MACROS 1 | ||
|
||
#include <stdlib.h> | ||
|
||
/******************************************************************************* | ||
Configuration Section | ||
Comment/Uncomment the #defines to enable or disable functionality | ||
*******************************************************************************/ | ||
|
||
#define SMB_ENABLE_MEMORY_DIAGNOSTICS | ||
#define SMB_ENABLE_DIAGNOSTIC_CODE | ||
#define SMB_ENABLE_DIAGNOSTIC_PRINTING | ||
|
||
/******************************************************************************* | ||
SMB_ENABLE_MEMORY_DIAGNOSTICS | ||
All this functionality is enabled when SMB_MEMORY_DIAGNOSTICS is defined. | ||
*******************************************************************************/ | ||
|
||
// These functions should NOT be called on their own. The macros should be | ||
// used, as they are macro controlled. | ||
void smb___helper_inc_malloc_counter(size_t number_of_mallocs); | ||
void smb___helper_dec_malloc_counter(size_t number_of_frees); | ||
size_t smb___helper_get_malloc_counter(); | ||
|
||
/** | ||
Call this function with the number of bytes every time you allocate memory. | ||
*/ | ||
#ifdef SMB_ENABLE_MEMORY_DIAGNOSTICS | ||
#define SMB_INCREMENT_MALLOC_COUNTER(x) smb___helper_inc_malloc_counter(x) | ||
#else | ||
#define SMB_INCREMENT_MALLOC_COUNTER(x) | ||
#endif // SMB_ENABLE_MEMORY_DIAGNOSTICS | ||
|
||
/** | ||
Call this function with the number of bytes every time you free memory. | ||
*/ | ||
#ifdef SMB_ENABLE_MEMORY_DIAGNOSTICS | ||
#define SMB_DECREMENT_MALLOC_COUNTER(x) smb___helper_dec_malloc_counter(x) | ||
#else | ||
#define SMB_DECREMENT_MALLOC_COUNTER(x) | ||
#endif // SMB_ENABLE_MEMORY_DIAGNOSTICS | ||
|
||
/** | ||
Call this function to get the number of bytes currently allocated by my code. | ||
*/ | ||
#ifdef SMB_ENABLE_MEMORY_DIAGNOSTICS | ||
#define SMB_GET_MALLOC_COUNTER smb___helper_get_malloc_counter() | ||
#else | ||
#define SMB_GET_MALLOC_COUNTER | ||
#endif // SMB_ENABLE_MEMORY_DIAGNOSTICS | ||
|
||
/******************************************************************************* | ||
SMB_ENABLE_DIAGNOSTIC_CODE | ||
Allows writing diagnostic code. Enabled by SMB_DIAGNOSTIC_CODE | ||
*******************************************************************************/ | ||
|
||
/** | ||
For running diagnostic code. | ||
*/ | ||
#ifdef SMB_ENABLE_DIAGNOSTIC_CODE | ||
#define SMB_DIAG_ONLY(x) x | ||
#else | ||
#define SMB_DIAG_ONLY(x) | ||
#endif // SMB_ENABLE_DIAGNOSTIC_CODE | ||
|
||
/******************************************************************************* | ||
SMB_ENABLE_DIAGNOSTIC_PRINTING | ||
*******************************************************************************/ | ||
|
||
/** | ||
Pass the given parameters to printf if printing is enabled. | ||
*/ | ||
#ifdef SMB_ENABLE_DIAGNOSTIC_PRINTING | ||
#define SMB_DIAG_PRINT(x...) printf(x) | ||
#else | ||
#define SMB_DIAG_PRINT(x...) | ||
#endif // SMB_ENABLE_DIAGNOSTIC_PRINTING | ||
|
||
#endif //SMB_MACROS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/******************************************************************************* | ||
File: generic.h | ||
Author: Stephen Brennan | ||
Date Created: Thursday, 12 September 2013 | ||
Description: A generic data type used in data structures | ||
*******************************************************************************/ | ||
|
||
union long_data { | ||
long long int data_llint; | ||
double data_dbl; | ||
void * data_ptr; | ||
}; | ||
|
||
typedef union long_data DATA; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/******************************************************************************* | ||
File: linkedlist.c | ||
Author: Stephen Brennan | ||
Date Created: Thursday, 12 September 2013 | ||
Description: A linked list data structure | ||
*******************************************************************************/ | ||
|
||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include "linkedlist.h" | ||
#include "../common_include.h" | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// PRIVATE FUNCTIONS | ||
|
||
/** | ||
Removes the given node, reassigning the links to and from it. | ||
*/ | ||
void ll_remove_node(LINKED_LIST *list, NODE *theNode) | ||
{ | ||
NODE *previous = theNode->prev; | ||
NODE *next = theNode->next; | ||
if (previous) { | ||
previous->next = next; | ||
} else { | ||
list->head = next; | ||
} | ||
if (next) { | ||
next->prev = previous; | ||
} else { | ||
list->tail = previous; | ||
} | ||
free(theNode); | ||
SMB_DECREMENT_MALLOC_COUNTER(1); | ||
} | ||
|
||
/** | ||
Navigates to the given index in the list. | ||
LINKED_LIST *list - the list | ||
int index - the index to find in the list | ||
returns - a pointer to that node. null if the index was out of range. | ||
*/ | ||
NODE * ll_navigate(LINKED_LIST *list, int index) | ||
{ | ||
NODE *node = list->head; | ||
if (index < 0) return NULL; | ||
int i = 0; | ||
while (node && i < index) { | ||
node = node->next; | ||
i++; | ||
} | ||
return node; | ||
} | ||
|
||
/** | ||
Navigates to the end of the list. Returns a pointer to the last node. | ||
*/ | ||
NODE * ll_navigate_end(LINKED_LIST *list) | ||
{ | ||
return list->tail; | ||
} | ||
|
||
NODE *ll_create_node(DATA data) | ||
{ | ||
NODE *newNode = (NODE*) malloc(sizeof(NODE)); | ||
newNode->data = data; | ||
SMB_INCREMENT_MALLOC_COUNTER(1); | ||
return newNode; | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
// PUBLIC METHODS | ||
|
||
LINKED_LIST * ll_create(DATA newData) | ||
{ | ||
LINKED_LIST *newList = (LINKED_LIST *) malloc(sizeof(LINKED_LIST)); | ||
SMB_INCREMENT_MALLOC_COUNTER(1); | ||
NODE *newNode = ll_create_node(newData); | ||
newList->head = newNode; | ||
newList->tail = newNode; | ||
newList->length = 1; | ||
return newList; | ||
} | ||
|
||
void ll_append(LINKED_LIST *list, DATA newData) | ||
{ | ||
NODE *newNode = ll_create_node(newData); | ||
NODE *lastNode = ll_navigate_end(list); | ||
lastNode->next = newNode; | ||
newNode->prev = lastNode; | ||
list->tail = newNode; | ||
list->length++; | ||
} | ||
|
||
DATA ll_get(LINKED_LIST *list, int index) | ||
{ | ||
NODE * theNode = ll_navigate(list, index); | ||
if (theNode) | ||
return theNode->data; | ||
DATA mockData; | ||
return mockData; | ||
} | ||
|
||
int ll_remove(LINKED_LIST *list, int index) | ||
{ | ||
NODE *theNode = ll_navigate(list, index); | ||
if (!theNode) | ||
return 0; | ||
ll_remove_node(list, theNode); | ||
list->length--; | ||
return 1; | ||
} | ||
|
||
void ll_delete(LINKED_LIST *list) | ||
{ | ||
NODE *iter = list->head; | ||
NODE *temp; | ||
while (iter) { | ||
temp = iter->next; | ||
ll_remove_node(list, iter); | ||
iter = temp; | ||
} | ||
free(list); | ||
SMB_DECREMENT_MALLOC_COUNTER(1); | ||
} |
Oops, something went wrong.