compile.sh 0000755 0000764 0000144 00000000056 11071361164 012603 0 ustar giovanni users #! /bin/sh
gcc -o genlist genlist.c main.c
deftypes.h 0000644 0000764 0000144 00000002746 11071103501 012605 0 ustar giovanni users /* Generic C List Copyright (C) 2008 Giovanni Santostefano
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
.
*/
#ifndef _DEFTYPES_
#define _DEFTYPES_
#include "usertypes.h" /*user defined types*/
/* With this enum the programmer defines which types
* the generic list can contains
*/
enum DEFTYPES
{
TYPE_INT=0, /*generic list can contain int*/
TYPE_FLOAT, /*can also contain float*/
TYPE_SPEED /*can also contain user defined structures*/
};
#endif
genlist.c 0000644 0000764 0000144 00000005227 11071356237 012437 0 ustar giovanni users /* Generic C List Copyright (C) 2008 Giovanni Santostefano
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
.
*/
#include "genlist.h"
/*Adds a node to the list
* returns the new head of the list
*/
struct GenList* GList_addNode(void *item, /*the item void ptr*/
enum DEFTYPES tp, /*the type of the item*/
struct GenList *head /*the head of the list*/
)
{
struct GenList *temp=NULL;
//allocate the new node
temp=malloc(sizeof(struct GenList));
if(!temp)
{
fprintf(stderr,"Could not allocate memory");
return NULL;
}
temp->next=head;
temp->itemtype=tp;
temp->itemptr=item;
return temp;
}
/*Removes the head node
*pass the head of the list
*returns the next node
*/
struct GenList* GList_removeHeadNode(struct GenList *head)
{
struct GenList *temp=head->next;
free(head);
return temp;
}
/*Removes a generic node of the list
*pass the previous node
*/
void GList_removeNode(struct GenList *prev)
{
if(!prev->next)
{
fprintf(stderr,"Could not remove NULL element\n");
}
struct GenList *temp=prev->next;
prev->next=temp->next;
free(temp);
temp=NULL;
}
/*Inserts a node in the list*/
void GList_insertNode(void *item, /*the item void ptr*/
enum DEFTYPES tp, /*the type of the item*/
struct GenList *prev /*the item where to
attatch the new node*/
)
{
struct GenList *temp=NULL;
//allocate the new node
temp=malloc(sizeof(struct GenList));
if(!temp)
{
fprintf(stderr,"Could not allocate memory\n");
return;
}
temp->next=prev->next;
prev->next=temp;
temp->itemtype=tp;
temp->itemptr=item;
}
genlist.h 0000644 0000764 0000144 00000004235 11071355670 012442 0 ustar giovanni users /* Generic C List Copyright (C) 2008 Giovanni Santostefano
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
.
*/
#ifndef _GENLIST_
#define _GENLIST_
#include
#include
#include "deftypes.h"
struct GenList
{
struct GenList *next; /*next element of the list*/
enum DEFTYPES itemtype; /*the type of te item from the enum list*/
void *itemptr; /*pointer to the item memory*/
};
/*Adds a node to the list
* returns the new head of the list
*/
struct GenList* GList_addNode(void *item, /*the item void ptr*/
enum DEFTYPES tp, /*the type of the item*/
struct GenList *head /*the head of the list*/
);
/*Removes the head node
*pass the head of the list
*returns the next node
*/
struct GenList* GList_removeHeadNode(struct GenList *head);
/*Removes a generic node of the list
*pass the previous node
*/
void GList_removeNode(struct GenList *prev);
/*Inserts a node in the list*/
void GList_insertNode(void *item, /*the item void ptr*/
enum DEFTYPES tp, /*the type of the item*/
struct GenList *prev /*the item where to
attatch the new node*/
);
#endif
main.c 0000644 0000764 0000144 00000007635 11071360430 011711 0 ustar giovanni users /* Generic C List Copyright (C) 2008 Giovanni Santostefano
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
.
*/
/* Test program for the generic list.
* Here is shown a mixed element list usage.
* A list with mixed types element is a BAD programming style IMHO.
* You can implement similar list interface to make a programmer able to
* construct generic lists without mixed types elements.
* A simple way to do that is to make a list struct that contains informations
* about the type and a pointer to an head node and a node structure
* that contains only the node item pointer
* and the next pointer.
* In this way you also save the node type info for each node instance.
*/
#include "genlist.h"
#include "usertypes.h"
/* Fill the list */
void ListFill(struct GenList **list)
{
int *age=NULL;
float *height=NULL;
struct Speed *sp=NULL;
age=malloc(sizeof(int));
height=malloc(sizeof(float));
sp=malloc(sizeof(struct Speed));
/* Test if some malloc has failed */
if(!age || !height || !sp)
{
fprintf(stderr,"Some malloc has failed!!!\n");
if(age)free(age);
if(height)free(height);
if(sp)free(sp);
return;
}
/* value filling */
*age=35;
*height=1.89f;
sp->value=101.3f;
sp->direction=341.2f;
/* node creation */
*list=GList_addNode((void*)age,TYPE_INT,*list);
*list=GList_addNode((void*)height,TYPE_FLOAT,*list);
*list=GList_addNode((void*)sp,TYPE_SPEED,*list);
}
int main(int argc, char *argv[])
{
struct GenList *list=NULL;
struct GenList *temp=NULL;
int example=0;
ListFill(&list);
/* Print the list */
temp=list;
while(temp)
{
/* we pass through the list.
* For each node we test the type.
* When we find the correct type we use the right code.
*/
if(temp->itemtype==TYPE_INT) /* With this test we find that the
* current node is of int type
*/
{
/* Here in the example we take the int item of the list
* and we store it in an int variable.
* To take the element we first make a cast of the pointer
* to the item to his correct type (int*) and then we use
* the dereference operator * to take the value pointed by
* itemptr
*/
example=*(int*)temp->itemptr;
/* In this example we print directly the value */
printf("This element is an int\n");
printf("It is: %d\n",*(int*)temp->itemptr);
}
else if(temp->itemtype==TYPE_FLOAT)
{
printf("This element is a float\n");
printf("It is: %f\n",*(float*)temp->itemptr);
}
else if(temp->itemtype==TYPE_SPEED)
{
printf("This element is a struct Speed\n");
printf("It's value is: %f\n",((struct Speed*)temp->itemptr)->value);
printf("It's direction is: %f\n",
((struct Speed*)temp->itemptr)->direction);
}
temp=temp->next;
}
/* Let's clear the list */
while(list)
{
list=GList_removeHeadNode(list);
}
/* end the execution */
return 0;
}
usertypes.h 0000644 0000764 0000144 00000002413 11071103431 013016 0 ustar giovanni users /* Generic C List Copyright (C) 2008 Giovanni Santostefano
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".
You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
.
The GNU General Public License does not permit incorporating your program
into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
.
*/
#ifndef _USERTYPES_
#define _USERTYPES_
/*example generic structure*/
struct Speed
{
float value;
float direction;
};
#endif