Ok, I was looking for some programming examples online to help me out, and i stumbled accoss this one. And just for a disclaimer, I am not going to use this for my homework assignment. I just wanted to show you guys as a reference to some of my questions. Anyway, here goes...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum {
MAXLEN=255
};
typedef struct list {
char name[MAXLEN];
char address[MAXLEN];
char phone[MAXLEN];
struct list *next;
} ADDRESS;
void add_to_list (void); /* Add a name to our address book */
void delete_from_list (void);/* Delete a name from our book */
void print_name (void); /* Print an entry in the address book */
void list_names (void); /* List all the names in the book */
void free_list (void); /* Free all the linked list */
ADDRESS *hol= NULL; /* Head of our linked list of addresses -
global variable*/
int main()
{
char input[MAXLEN]; /* Input from the user */
while (1) {
printf ("(A)dd/(D)elete/(P)rint/(L)ist/(Q)uit\n");
fgets (input, MAXLEN,stdin);
switch (input[0]) {
case ('a'):
case ('A'):
add_to_list();
break;
case ('d'):
case ('D'):
delete_from_list();
break;
case ('p'):
case ('P'):
print_name();
break;
case ('l'):
case ('L'):
list_names();
break;
case ('q'):
case ('Q'):
free_list();
return (0);
default:
printf ("Unknown command\n");
}
}
return 0;
}
void add_to_list (void)
/* Add a new name to our address book */
{
ADDRESS *new_name;
new_name= malloc (sizeof (ADDRESS));
if (new_name == NULL) {
printf ("Out of memory!\n");
exit (-1);
}
/* Get input for the new item */
printf ("Name> ");
fgets (new_name->name, MAXLEN, stdin);
printf ("Address> ");
fgets (new_name->address, MAXLEN, stdin);
printf ("Tel> ");
fgets (new_name->phone, MAXLEN, stdin);
/* Chain the new item into the list */
new_name->next= hol;
hol= new_name;
}
void delete_from_list (void)
/* Remove a name from the address book */
{
ADDRESS *del_ptr; /* Pointer to find name to delete */
ADDRESS *prev_ptr; /* Pointer to name BEFORE this name */
char del_name[MAXLEN]; /* Name to delete */
printf ("Name> ");
fgets (del_name, MAXLEN, stdin);
/* The first item in the list being deleted is a special case */
if (hol == NULL) {
printf ("No list to delete from\n");
return;
}
/* If the first name is the correct one then move the head of list on to the next head of list */
if (strcmp(hol->name, del_name) == 0) {
del_ptr= hol;
hol= hol->next;
free(del_ptr);
return;
}
/* Otherwise loop round the list looking for the name */
prev_ptr= hol;
while (prev_ptr->next != NULL) {
/* We've found the name so delete it */
if (strcmp(prev_ptr->next->name,del_name) == 0) {
del_ptr= prev_ptr->next;
prev_ptr->next= del_ptr->next;
free(del_ptr);
return;
}
prev_ptr= prev_ptr->next;
}
printf ("Name not found!\n");
}
void print_name (void)
/* Print a name from the list */
{
char name[MAXLEN]; /* Name to look for */
ADDRESS *search_ptr; /* Pointer to search list for name */
printf ("Name> ");
fgets (name, MAXLEN, stdin);
search_ptr= hol;
while (search_ptr != NULL) {
if (strcmp (search_ptr->name, name) == 0) {
printf ("Address: %s", search_ptr->address);
printf ("Tel: %s", search_ptr->phone);
return;
}
search_ptr= search_ptr->next; /* Move to next item */
}
printf ("No such name\n");
}
void list_names (void)
/* List all names in the book */
{
ADDRESS *tmp_ptr; /* Traverses list */
printf ("All names in address book:\n");
tmp_ptr= hol;
while (tmp_ptr != NULL) {
printf ("%s",tmp_ptr->name);
tmp_ptr= tmp_ptr->next;
}
}
void free_list (void)
/* Free memory for list */
{
ADDRESS *del_ptr; /* Pointer to part to delete */
while (hol != NULL) {
del_ptr= hol; /* Store the head of list */
hol= hol->next; /* Move on to the next bit */
free(del_ptr); /* Free the memory for the bit we just left */
}
}
I don't see any refernece to NODES and what does this code
Code:
ADDRESS *hol= NULL; /* Head of our linked list of addresses -
mean?
This example confused me more than anything I think... Any help is appreciated.
Thanks,
~P