Good afternoon everyone. I already studied some illustrations from the book for the Circular lists. Therefore, my problem is that I don't know what's the purpose of writing
Code:
old_element = element->next;
inside clist_rem_next (a function to remove the node after the specified one).
Code:
#ifndef CLIST_H
#define CLIST_H
#include <stdlib.h>
typedef struct CListElmt_{
void* data;
struct CListElmt_* next;
} CListElmt;
typedef struct CList_ {
int size;
int (*match)(const void *key1, const void* key2);
void (*destroy)(void* data);
CListElmt *head;
} CList;
/* public interface */
void clist_init(CList* list, void (*destroy)(void* data));
void clist_destroy(CList* list);
int clist_ins_next(CList* list, CListElmt* element, const void* data);
int clist_rem_next(CList* list, CListElmt* element, void** data);
#define clist_size(list) ((list)->size)
#define clist_head(list) ((list)->head)
#define clist_data(element) ((element)->data)
#define clist_next(element) ((element)->next)
#endif
Code:
#include <stdlib.h>
#include <string.h>
#include "Headers/clist.h"
void clist_init(CList* list, void (*destroy)(void* data))
{
/* initialize the list */
list->size = 0;
list->destroy = destroy;
list->head = NULL;
return;
}
void clist_destroy(CList* list)
{
void* data;
/* remove each element */
while(clist_size(list) > 0)
{
if(clist_rem_next(list, list->head, (void**)&data) == 0
&& list->destroy != NULL)
{
/* call a user-defined function to free dinamically allocated data */
list->destroy(data);
}
}
/* no operations are allowed now, but clear the structure as a precaution */
memset(list, 0, sizeof(CList));
return;
}
int clist_ins_next(CList* list, CListElmt* element, const void* data)
{
CListElmt* new_element;
/* allocate storage for the element */
if((new_element = (CListElmt*)malloc(sizeof(CListElmt))) == NULL)
return -1;
/* insert the element into the list */
new_element->data = (void*) data;
if(clist_size(list) == 0)
{
/* handle insertion when the list is empty */
new_element->next = new_element;
list->head = new_element;
}
else
{
/* handle insertion when the list is not empty */
new_element->next = element->next;
element->next = new_element;
}
/* adjust the size of the list to account for the inserted element */
list->size++;
return 0;
}
int clist_rem_next(CList* list, CListElmt* element, void** data)
{
CListElmt* old_element;
/* don't allow removal from an empty list */
if(clist_size(list) == 0)
return -1;
/* remove the element from the list */
*data = element->next->data;
if(element->next == element)
{
/* handle removing the last element */
old_element = element->next;
list->head = NULL;
}
else
{
/* handle removing other than the last element. */
old_element = element->next;
element->next = element->next->next;
if(old_element == clist_head(list))
list->head = old_element->next;
}
free(old_element);
list->size--;
return 0;
}
Code:
/*****************************************************************************
* *
* ex-1.c *
* ====== *
* *
* Description: Illustrates using a circular list (see Chapter 5). *
* *
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "Headers/clist.h"
/*****************************************************************************
* *
* ------------------------------ 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++;
}
return;
}
/*****************************************************************************
* *
* --------------------------------- 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;
}