Mail Archives: djgpp/1997/06/16/12:19:24
sar AT maties DOT sun DOT ac DOT za wrote:
>
> I'm a newbie to DJGPP and C itself. Can anybody provide me with some
> sample code as to how to create, modify and erase linked lists under
> DJGPP? I know it has something to do with pointers but I just can't seem
> to get the syntax and the * in the right places.
comp.lang.c and comp.lang.c.moderated are better places to ask this
question. This newsgroup is not for basic programming questions. Also,
if your C textbook doesn't tell you how to make a linked list, you
should burn it and buy a better one.
Just so you don't go away mad:
/* basic linked list */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct item_data ITEM;
typedef struct node_data NODE;
typedef NODE * LIST;
struct item_data
{
char name[30];
char address[40];
};
struct node_data
{
ITEM item;
NODE *next;
};
int Add_Node( LIST *list, ITEM item );
int Delete_Node( LIST *list, NODE *node );
/* adds node to the list containing the data in item.
returns nonzero on success; 0 on failure */
int Add_Node( LIST *list, ITEM item )
{
NODE *new;
if ( ( new = (NODE *) malloc( sizeof(NODE) ) ) == NULL )
{
fprintf( stderr, "Add_Node: out of memory.\n" );
return 0;
}
new->item = item;
new->next = *list;
*list = new;
return 1;
}
/* removes node from the list and frees the memory it uses.
returns nonzero on success, 0 on failure */
int Delete_Node( LIST *list, NODE *node )
{
NODE *temp;
if ( *list == NULL ) /* list is empty */
return 0;
if ( *list == node )
{
*list = node->next;
free( node );
return 1;
}
for ( temp = *list; temp->next != NULL; temp = temp->next )
if ( temp->next == node )
{
temp->next = node->next;
free( node );
return 1;
}
return 0; /* node not found */
}
/* example of usage */
int main( void )
{
LIST the_list = NULL;
NODE *temp;
ITEM customer;
int done = 0;
int answer, count;;
while( !done )
{
clrscr( );
printf( "%3s %-30.30s %-40.40s\n", "ID", "Name",
"Address" );
for ( count = 1, temp = the_list; temp != NULL; count++, temp =
temp->next )
printf( "%3d %-30.30s %-40.40s\n", count,
temp->item.name, temp->item.address );
printf( "1 - Add customer\n2 - Delete customer\n3 - Quit\n" );
printf( "Selection? " );
scanf( "%d", &answer );
while( getchar( ) != '\n' )
;
switch( answer )
{
case 1:
/* using gets here is very bad - typing more characters
than size of string may cause crashes/bad data.
Fixing this is an exercise for the reader. */
printf( "Name: " );
gets( customer.name );
printf( "Address: " );
gets( customer.address );
if ( Add_Node( &the_list, customer ) == 0 )
{
printf( "Unable to add customer!\n" );
getch( );
}
break;
case 2:
printf( "# of customer to delete? " );
scanf( "%d", &answer );
while( getchar( ) != '\n' )
;
for ( count = 1, temp = the_list; temp != NULL; temp =
temp->next, count++ )
if ( count == answer )
break;
if ( temp == NULL )
break;
if ( Delete_Node( &the_list, temp ) == 0 )
{
printf( "Unable to delete customer!\n" );
getch( );
}
break;
case 3:
done = 1;
break;
}
}
return 0;
}
--
---------------------------------------------------------------------
| John M. Aldrich | "A woman is not property, and hus- |
| aka Fighteer I | bands who think otherwise are living |
| mailto:fighteer AT cs DOT com | in a dreamworld." |
| http://www.cs.com/fighteer | - Lazarus Long |
---------------------------------------------------------------------
- Raw text -