Quick guide


This short guide is by no means a comprehensive tutorial, it does however provide a quick introduction to the library's features, and some examples on how to use them. There will also be some info on how to get most out of it, in terms of efficiency.

Basic use

The following example will demonstrate how to create a list, add several items to it, read from the list, and clean up afterwards:


#include <stdio.h> 
#include <dynlist.h> 

#define NUM	10	/*create 10 items*/ 

int main()
{
	dynlist(list, int);	/*create a list called list for items of type int*/
	int *temp;
	int i;

	for(i=0; i<NUM; ++i)
	{
		temp=newitem(&list);	/*create new item and get a pointer to it*/
		*temp=i;
	}		

	for(i=0; i<NUM; ++i)
	{
		temp=getitem(&list, i);
		if(temp)	/*if item exists*/
			printf("%d\n", *temp);
	}

	clearlist(&list);
	return 0;
}




This code can be compiled with the following command (assuming dynlist is correctly installed on your system, and you're using GCC)
gcc foo.c -o foo -ldynlist

As seen above, the function dynlist(list, item) will create a list. Since this is a rather inflexible macro function, some people might be inclined not to use it. This is no problem, as you can simply use the function called by dynlist(), createlist().


int main()
{
	dynlist list;	/*create dynlist variable*/
	list=createlist(sizeof(int));	/*initialize list to accomodate items of type int*/




Advanced use

The following code will display several more advanced topics;

  • Adding previously created items to an existing list
  • Appending items to the end of a list
  • Erasing items from a list
  • Reversing a list
  • swappping 2 items in a list
  • Retrieving the amount of items in a list

#include <stdio.h> 
#include <dynlist.h> 

#define NUM	10	/*create 10 items*/ 

int main()
{
	int i;
	int *temp;
	dynlist(list, int);	/*create a list called list for items of type int*/
	
	for(i=0; i<NUM; ++i)
		additem(&list, &i, 0);	/*add the value of i at position 0 in list*/

	i=12345;	/*lets add this value to the end of the list*/
	appenditem(&list, &i);

	eraseitem(&list, 3);	/*erase the item at index 3*/
	eraseitem(&list, -2);	/*erase the second-to last item*/

	reverse(&list);	/*reverse the list*/
	swap(&list, 2, 5);	/*swap items currently at index 2 and 5*/

	for(i=0; i<getlen(list); ++i)	/*retrieve the current length of the list and use in the for loop*/
	{
		temp=getitem(&list, i);
		if(temp)	/*if item really exists*/
			printf("%d\n", *temp);
	}

	clearlist(&list);	/*clean up*/
	return 0;
}



This program should yield the following output;
12345
1
5
3
4
2
7
8
9

Sorting a list

In the following example we will create and populate a list, and sort this using two sorting user-defined functions:


#include <stdio.h> 
#include <dynlist.h> 

typedef struct {
	int alpha;
	int beta;
} data;		/*This is what our datablocks will look like*/

int sort_by_alpha(void *first, void *second) {
/*This function defines how to sort 2 items by the alpha value, so dynlist can do the rest*/
	data *d1 = (data*)first;
	data *d2 = (data*)second;

	if(d1->alpha > d2->alpha)
		return 1;
	
	else
		return 0;
}

int sort_by_beta(void *first, void *second) { 
/*Same thing, for the beta value. Notice that the return value and parameter types are always the same for sorting functions*/
	data *d1 = (data*)first;
	data *d2 = (data*)second;

	if(d1->beta > d2->beta)
		return 1;
	
	else
		return 0;
}

print_data_list(dynlist list) { 
/*function to display the contents of the list on screen*/
	data *temp;
	int i, len=getlen(list);

	puts(" alpha,  beta");

	for(i=0; i<len; ++i)
	{
		temp=getitem(&list, i);
		printf("(%5d, %5d)\n", temp->alpha, temp->beta);
	}
}

void add_data(dynlist *list, int alpha, int beta) { 
/*function to add a new item to the list*/
	data *temp = newitem(list);
	temp->alpha = alpha;
	temp->beta = beta;
}

int main() {
	dynlist(list, data);

	add_data(&list, 34, 21);	/*add some random datablocks to the list*/
	add_data(&list, 21, 3);
	add_data(&list, 2, 34);
	add_data(&list, 43, 2);
	add_data(&list, 543, 2);
	add_data(&list, 1, 342);
	add_data(&list, 0, 23421);

	puts("\nBefore sorting:");
	print_data_list(list);	/*display the list*/

	puts("\nAfter sorting by alpha:");
	sortlist(&list, sort_by_alpha);		/*(obviously) sort by alpha*/
	print_data_list(list);

	puts("\nAfter sorting by beta:");
	sortlist(&list, sort_by_beta);	/*sort by beta*/
	print_data_list(list);

	clearlist(&list);	/*clean up*/
	return 0;
}



This program should yield the following output:
Before sorting:
 alpha,  beta
(   34,    21)
(   21,     3)
(    2,    34)
(   43,     2)
(  543,     2)
(    1,   342)
(    0, 23421)

After sorting by alpha:
 alpha,  beta
(    0, 23421)
(    1,   342)
(    2,    34)
(   21,     3)
(   34,    21)
(   43,     2)
(  543,     2)

After sorting by beta:
 alpha,  beta
(   43,     2)
(  543,     2)
(   21,     3)
(   34,    21)
(    2,    34)
(    1,   342)
(    0, 23421)