C example of building a linked list
Following is an example of how to build a linked list in a C program
using callable services.
/*Module/File Name: EDCLLST */
/****************************************************************
** *
**FUNCTION : CEEGTST - obtain storage from user heap *
** for a linked list. *
** : CEEFRST - free linked list storage *
** *
** This example illustrates the construction of a linked *
** list using the Language Environment storage management *
** services. *
** *
** *
** 1. Storage for each list element is allocated from the *
** user heap. *
** *
** 2. The list element is initialized and appended to the *
** list. *
** *
** 3. After three members are appended, the list traversed *
** and the count saved in each element is displayed. *
** *
** 4. The linklist storage is freed. *
** *
****************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <leawi.h>
#include <ceeedcct.h>
void main ()
{
_INT4 HEAPID;
_INT4 HPSIZE;
_INT4 LCOUNT;
_FEEDBACK FC;
_POINTER ADDRSS;
struct LIST_ITEM
{ _INT4 COUNT;
struct LIST_ITEM *NEXT_ITEM;
} ;
struct LIST_ITEM *ANCHOR;
struct LIST_ITEM *CURRENT;
_INT4 NBYTES = sizeof(struct LIST_ITEM);
printf ( "\n**********************************\n");
printf ( "\nCESCSTO C Example is now in motion\n");
printf ( "\n**********************************\n");
ANCHOR = NULL;
for ( LCOUNT = 1; LCOUNT < 4; LCOUNT++)
{
/*******************************************************
* Call CEEGTST to get storage from user heap *
*******************************************************/
CEEGTST ( &HEAPID , &NBYTES , &ADDRSS , &FC );
if ( (_FBCHECK (FC , CEE000) == 0) && ADDRSS != 0 )
/* ************************************************
* If storage is gotten successfully, the linked *
* list elements are pointed to by the pointer *
* variable CURRENT. Append element to the end of *
* the list. The list origin is pointed to by the *
* variable ANCHOR. *
* ************************************************/
{
if (ANCHOR == NULL)
{
ANCHOR =(struct LIST_ITEM *) ADDRSS;
}else{
CURRENT -> NEXT_ITEM =(struct LIST_ITEM *)ADDRSS;
}
CURRENT =(struct LIST_ITEM *) ADDRSS;
CURRENT -> NEXT_ITEM = NULL;
CURRENT -> COUNT = LCOUNT;
}else{
printf ( "Error in getting user storage\n" );
}
}
/*********************************************************
* On completion of the above loop, we have the *
* following layout: *
* *
* ANCHOR --> LIST-ITEM1 --> LIST-ITEM2 --> LIST-ITEM3*
* *
* Loop thru list items 1 thru 3 and print out the *
* identifying item number saved in the COUNT field. *
* *
* Test the LCOUNT variable to verify that three items *
* were indeed in the linked list. *
*********************************************************/
CURRENT = ANCHOR;
while (CURRENT)
{
printf("This is list item %d\n", CURRENT->COUNT) ;
ADDRSS = CURRENT;
LCOUNT = CURRENT -> COUNT;
CURRENT = CURRENT -> NEXT_ITEM;
/*******************************************************
* Call CEEFRST to free this piece of storage *
*******************************************************/
CEEFRST ( &ADDRSS , &FC );
if ( _FBCHECK (FC , CEE000) == 0 )
{
}else{
printf ( "Error freeing storage from heap\n" );
}
}
if (LCOUNT == 3)
{
printf ( "\n********************************\n" );
printf ( "\nC/370 linked list example ended.\n" );
printf ( "\n********************************\n" );
exit(0);
}else{
printf ( "Error in constructing linked list\n" );
}
exit(-1);
}