[ Pobierz całość w formacie PDF ]

to the next structure. That makes so much sense you'd expect it to be
taught in Sunday School: The last structure in the list, logically, has
nothing following it. It demands a NULL pointer!
Here's another look at that linked list illustration to drive the point
home:
Look at the last structure in the list. What does it end with? 000? Isn't
that a NULL? Absolutely!
Now two things must be done: The loop that creates the linked list must
cap it off with a NULL pointer in the last structure. After that, the loop
that displays the list uses the same NULL to determine when the list is
done. You could ruminate on this for a time if you like, or just peek
ahead to the following source code, the last of the PREZEZ.C series.
Name: PREZEZ5.C
#include
#include
#include
#include
void main()
{
struct pres {
char name[25];
struct pres *next;
};
char *presidents[] = {
"George Washington",
"John Adams",
"Thomas Jefferson",
"James Madison",
"James Monroe",
""
};
struct pres *first;
struct pres *current;
struct pres *new;
int index = 0;
/* Initialize all the pointers */
first = (struct pres *)NULL;
current = (struct pres *)NULL;
new = (struct pres *)NULL;
/* Create the first item in the list */
first = (struct pres *)malloc(sizeof(struct pres));
current = first;
/* Create each item in the list */
while(1)
{
strcpy(current->name,presidents[index]);
index++;
if(!*presidents[index])
{
current->next = (struct pres *)NULL;
break;
}
else
{
new = (struct pres *)malloc(sizeof(struct pres));
current->next = new;
current = new;
}
}
/* Display the results */
current = first;
index = 1;
while(current)
{
printf("Structure %i: ",index++);
printf("%s\n",current->name);
current = current->next;
}
}
Type in the above source code or Shift+click here to download a copy.
Compile and Run. The output will be the same as for PREZEZ4.C (above) but
the method is better, more flexible. Here's what's going on:
First, a while(1) ("while true") loop is used to spin indefinitely. That's
because an if-else structure is used inside the loop to determine when the
last item from the array has been read. (There are other ways to do this
as well.)
Inside the loop, the structure is filled in two steps. First, the
president's name is fetched from the array and stuffed into the structure:
strcpy(current->name,presidents[index]);
This works the same for all the structures, whether they're the first,
middle or last.
Next, the index variable is incremented:
index++;
You need to determine whether or not this is the last structure and the
way to know that is to see if there is another item lurking in the
presidents array. An if statement checks for you:
if(!*presidents[index])
This is a peek ahead. The value of the string there isn't used now; it
will be used the next time the loop repeats (if it repeats). Otherwise, if
tests to see whether anything is there at all. If not (which is the ! part
of the test), then the next pointer in the structure is assigned a NULL
structure-pointer value and the loop breaks:
current->next = (struct pres *)NULL;
break;
Otherwise, malloc() grabs another chunk of memory. The new address is
saved, the current pointer is reset to that address so that the loop can
repeat and the new structure be filled:
new = (struct pres *)malloc(sizeof(struct pres));
current->next = new;
current = new;
To display the results a simpler construction can be used. After all, the
next pointer in each structure variable holds an address for every item in
the list but the last one. In that case, it holds the NULL value. And all
of the C language looping statements interpret NULL as a FALSE.
Before the loop starts, the current pointer is initialized to the address
of the first item in the list, and the index variable is set to one (for
counting purposes):
current = first;
index = 1;
After that, the loop can spin until the value of the next pointer is zero
(index is not used for the loop!):
while(current)
{
printf("Structure %i: ",index++);
printf("%s\n",current->name);
current = current->next;
}
Only the name part of each structure is displayed with a printf. In fact,
it could all go on one line:
printf("Structure %i: %s\n",index+1,current->name);
Then, the current variable is set equal to the address of the next
structure in the linked list:
current = current->next;
When the last item in the list is encountered, the current variable will
be set equal to NULL. That's the condition the while loop interprets as
the end of the loop.
The program may not be utterly elegant, and it's certainly not the only
way to accomplish the task, but it works and is flexible: You can add
the rest of the presidents' names to the array and the sucker will still
work as advertised -- which is the bottom line.
The next lesson shows a different way to add and display elements in a
linked list -- more of a database-like thing than the PREZEZ.C series of
programs.
Bonus C For Dummies Lesson 17-3Lesson 17-3  The Dawn of the Database
Linked lists are really all about databases. The structure itself is like
a record in a database, and the variables in the structure are fields. I
can't see how it could get any more obnoxiously obvious. And if that's all
true, then the linked list is really just another term for a database. So
starting with this Lesson and continuing for a few more Lessons, you're
about to take a bath into the world of databases.
The Ubiquitous Bank Account Program
It seems like the last few times I've gotten questions from readers about
linked lists it had to do with banking programs. You know the type: They
lose track of you and your money  unless you owe them money, in which
case they're on you like white on rice.
The following program is BANK1.C, which should tell you immediately that
it's only the first in what will probably be several more BANK programs.
This program creates a linked list one record at a time. Unlike the
previous linked list examples, this one take a giant step into the area of
databases.
Name: BANK1.C
#include
#include
#include
#include
#include
#include
void addNewAccount(void);
struct account {
int number;
char lastname[15];
char firstname[15];
float balance;
struct account *next;
};
struct account *first,*current,*new;
int anum = 0;
void main()
{
char ch;
/* Initialize all the pointers */
first = (struct account *)NULL;
current = (struct account *)NULL;
new = (struct account *)NULL;
do
{
puts("\nA - Add a new account");
puts("Q - Quit this program\n");
printf("\tYour choice:");
ch = toupper(getch());
switch(ch)
{
case 'A':
puts("Add new account\n");
addNewAccount();
break;
case 'Q':
puts("Quit\n");
default:
break;
}
}
while(ch != 'Q');
}
void addNewAccount(void)
{
char buffer[64];
new = (struct account *)malloc(sizeof(struct account));
/* Check to see if this is the first record [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • angamoss.xlx.pl