RE: [Dev-C++] array of strings -- LINKED LIST EXAMPLE
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Reid T. <Rei...@at...> - 2003-03-31 01:26:11
|
From Practical C Programming, 3rd Edition By Steve Oualline 3rd Edition August 1997=20 1-56592-306-5, Order Number: 3065 451 pages, $34.95 US, $51.95 CA, =A324.95 UK http://www.oreilly.com/catalog/pcp3/ http://examples.oreilly.com/pcp3/ I have 1st edition, I highly recommend this book.=20 /******************************************************** * Define, use and print out an ordered linked list * * * * Usage: * * list * * * * Note: Demonstration of an ordered linked list * ********************************************************/ #include <stdio.h> #include <stdlib.h> /******************************************************** * Item for our linked list * ********************************************************/ struct item { int value; /* Value of the item */ struct item *next_ptr; /* Pointer to the next item */ }; /******************************************************** * enter -- enter a value in the linked list. * * * * Parameters * * first_ptr -- pointer th the head of the list * * value -- value to add to the list * * * * Restrictions: The first entry must be the lowest. * * The list must have one item already * ********************************************************/ void enter(struct item *first_ptr, const int value) { struct item *before_ptr; /* Item before this one */ struct item *after_ptr; /* Item after this one */ struct item *new_item_ptr; /* Item to add */ /* Create new item to add to the list */ before_ptr =3D first_ptr; /* Start at the beginning */ after_ptr =3D before_ptr->next_ptr; =20 while (1) { if (after_ptr =3D=3D NULL) break; if (after_ptr->value >=3D value) break; /* Advance the pointers */ after_ptr =3D after_ptr->next_ptr; before_ptr =3D before_ptr->next_ptr; } new_item_ptr =3D malloc(sizeof(struct item)); new_item_ptr->value =3D value; /* Set value of item */ before_ptr->next_ptr =3D new_item_ptr; new_item_ptr->next_ptr =3D after_ptr; } /******************************************************** * print -- print the linked list * * * * Parameter * * first_ptr -- pointer to the first item on the * * list * ********************************************************/ void print(struct item *first_ptr) { struct item *cur_ptr; /* Pointer to the current item */ for (cur_ptr =3D first_ptr; cur_ptr !=3D NULL; cur_ptr =3D = cur_ptr->next_ptr) printf("%d ", cur_ptr->value); printf("\n"); } int main() { /* The linked list */ struct item *head_ptr =3D NULL; head_ptr =3D malloc(sizeof(struct item)); head_ptr->value =3D 0; head_ptr->next_ptr =3D NULL; enter(head_ptr, 5); enter(head_ptr, 4); enter(head_ptr, 8); enter(head_ptr, 9); enter(head_ptr, 1); enter(head_ptr, 2); print(head_ptr); return (0); } -- thanks, reid -----Original Message----- From: Khalid El-Kary [mailto:kha...@ho...]=20 Sent: Sunday, March 30, 2003 7:31 PM To: Dev...@li... Subject: RE: [Dev-C++] array of strings where can i find information about a linked lists, i don't know about = them=20 *shy* thanx for the suggestion :-) Regards, Khalid AL-Kary >why not return them to a linked list rather than an array? > >-- >thanks, >reid _________________________________________________________________ Add photos to your e-mail with MSN 8. Get 2 months FREE*.=20 http://join.msn.com/?page=3Dfeatures/featuredemail ------------------------------------------------------- This SF.net email is sponsored by: The Definitive IT and Networking Event. Be There! NetWorld+Interop Las Vegas 2003 -- Register today! http://ads.sourceforge.net/cgi-bin/redirect.pl?keyn0001en _______________________________________________ Dev-cpp-users mailing list Dev...@li... TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |