Mail Archives: djgpp/1997/06/21/01:39:29
It's not only in DJGPP but any C compiler that you can use linked lists.
Here is an example program, I did not test it so it might contain some
errors:
#include <stdio.h>
#include <stdlib.h>
#incldue <string.h>
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#defien FALSE 0
#endif
//here is the actual linked list structure
typedef struct LIST {
char name[14]; //let's say it will hold name
int age; //and age of a person
struct LIST *next; //this is a pointer to the next node
} LIST;
//this function adds new node to the list. Each node
//contains name and age. You have to pass double pointer because
//you have to modify the pointer to the list if it's null
void AddToList(LIST **list, char *name, int age)
{
LIST *newNode; //this is the new node
//you have to dynamically allocate memory for the node
//that's the whole point behind liked lists, nodes can
//be dynamically created
newNode = (LIST*)malloc(sizeof(struct LIST));
strcpy(newNode->name,name); //copy the name
newNode->age = age; //and age
if (*list == NULL) { //if the list has not been creaed yet
*list = newNode; //the list points to the created node
(*list)->next = NULL; //and the next node is null to mark the end
of the list
}
else { //list was already created
newNode->next = *list; //add new node at the beginning of the list
*list = newNode; //and the list now points to the new beginning
}
}
//this function recursively searches for the name in the list
//it returns true or false, and stores age of that person in age
int LookForName(LIST *list, char *name, int *age)
{
//if you reach null, all the nodes of the list were searched
//and person was not found
if (list == NULL) return FALSE;
else if (strcmp(list->name, name) == 0) { //you found that person
*age = list->age; //coyp age
return TRUE;
}
else return LookForName(list->next, name, age); //try next node
}
main()
{
LIST *list = NULL; //the actual list
char name[14]; //temp name
int choice; //choice of the user
int age; //age
do { //do loop
printf("Enter 1 to add name or 2 to look for name\n");
printf("or enter 0 to ext\n");
scanf("%d", &choice); //get the choice
if (choice == 1) { //add name
printf("Enter name: ");
scanf("%s", name);
printf("Enter age: ");
scanf("%d", &age);
AddToList(list, name, age); //add name to list
}
else if (choice == 2) { //look for name
printf("Etner name: ");
scanf("%s", name);
if (LookForName(name, &age)) { //found person
printf("Person found in list.\n");
printf("his age is: %d\n", age);
}
else {
printf("Person not found.\n");
}
}
else if (choice == 0) break;
} while (choice != 0);
}
this little program keeps asking either to add a person to a list or to
find person in the list. I hope this is helpful. If you have more
questions go ahead and ask, but I would recomend to get a book on advanced
C programming. It doen not have to be for DJGPP, get some ANSI C and that
will work.
Andrew Deren
aderen AT eecs DOT uic DOT edu
http://www.geocities.com/SiliconValley/Vista/1042
Get 3 Games PacHorror, Tetris or Kill The Barney with
full source code.
On Sun, 15 Jun 1997 sar AT maties DOT sun DOT ac DOT za wrote:
> Hi!
>
> 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.
>
> Any help will be appreciated!
>
> Rylan
>
- Raw text -