From: "John M. Aldrich" Newsgroups: comp.os.msdos.djgpp Subject: Re: Linked lists (please don't kill me!) Date: Sun, 15 Jun 1997 14:25:53 +0000 Organization: Two pounds of chaos and a pinch of salt Lines: 163 Message-ID: <33A3FB71.2E1A@cs.com> References: <33A3D1BF DOT 6BCC AT maties DOT sun DOT ac DOT za> Reply-To: fighteer AT cs DOT com NNTP-Posting-Host: ppp110.cs.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk 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 #include #include 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 | ---------------------------------------------------------------------