Example #1
0
/* ------------------------------ print_list ----------------------------- */
static void print_list(const CList *list) {

    CListElmt          *element;

    int                *data,
		       size,
		       i;

    /* Display the circular list */
    fprintf(stdout, "List size is %d (circling twice)\n", clist_size(list));

    size = clist_size(list);
    element = clist_head(list);

    /* Iterate twice through the circular list to verify the circular links */
    i = 0;

    while (i < size * 2) {

       data = clist_data(element);
       fprintf(stdout, "list[%03d]=%03d\n", (i % size), *data);
       element = clist_next(element);
       i++;

    }
}
Example #2
0
/* --------------------------------- main -------------------------------- */
int main(int argc, char **argv) {

    CList              list;
    CListElmt          *element;

    int                *data,
		       i;

    /* Initialize the circular list */
    clist_init(&list, free);

    /* Perform some circular list operations */
    element = clist_head(&list);

    for (i = 0; i < 10; i++) {

       if ((data = (int *)malloc(sizeof(int))) == NULL)
	  return 1;

       *data = i + 1;

       if (clist_ins_next(&list, element, data) != 0)
	  return 1;

       if (element == NULL)
	  element = clist_next(clist_head(&list));
       else
	  element = clist_next(element);

    }

    print_list(&list);

    element = clist_head(&list);

    for (i = 0; i < 10; i++)
       element = clist_next(element);

    data = clist_data(element);
    fprintf(stdout, "Circling and removing an element after the one containing "
       "%03d\n",*data);

    if (clist_rem_next(&list, element, (void **)&data) != 0)
       return 1;

    free(data);
    print_list(&list);

    element = clist_head(&list);

    for (i = 0; i < 15; i++)
       element = clist_next(element);

    data = clist_data(element);
    fprintf(stdout, "Circling and inserting 011 after the element containing "
       "%03d\n", *data);

    if ((data = (int *)malloc(sizeof(int))) == NULL)
       return 1;

    *data = 11;
    if (clist_ins_next(&list, element, data) != 0)
       return 1;

    print_list(&list);

    /* Destroy the circular list */
    fprintf(stdout, "Destroying the list\n");
    clist_destroy(&list);

    return 0;

}
Example #3
0
int main()
{
	CList list;
	CListElmt *current;

	Page *p;
	int i;

	/* Initialize the circilar list. */
	clist_init(&list, free);

	/* Load the pages into the list. */
	current = NULL;
	for (i = 0; i < 10; ++i) {
		if ((p = (Page *)malloc(sizeof(Page))) == NULL) {
			return 1;
		}

		if (i < 5) {
			p->reference = 1;
		} else {
			p->reference = 0;
		}

		p->number = i;

		if (clist_ins_next(&list, current, p) != 0) {
			return 1;
		}

		if (current == NULL) {
			current = clist_next(clist_head(&list));
		} else {
			current = clist_next(current);
		}
	}

	current = clist_head(&list);

	for (i = 0; i < 10; ++i) {
		p = clist_data(current);

		fprintf(stdout, "p[%d].number=%d, p[%d].reference=%d\n",i, p->number,
				i, p->reference);

		current = clist_next(current);
	}

	/* Get the number of the page to replace. */
	current = clist_head(&list);
	i = replace_page(&current);
	fprintf(stdout, "Selected %d\n", i);

	current = clist_head(&list);

	for (i = 0; i < 10; ++i) {
		p = clist_data(current);
		fprintf(stdout, "p[%d].number=%d, p[%d].reference=%d\n",i, p->number,
				i, p->reference);

		current = clist_next(current);
	}

	/* Destroy the circular list. */
	fprintf(stdout, "Destroying the list\n");
	clist_destroy(&list);

	return 0;
}