do ÂściÂągnięcia - pobieranie - ebook - pdf - download
Podstrony
- Strona Główna
- Angielski Język angielski program nauczania dla liceum kontynuacja
- 2006.02 Qt ISO Maker–moja pierwsza aplikacja w Qt [Programowanie]
- Delphi 31 przydatnych programow [ ZLOTEMYSLI][by www.ebookforum.pl ]
- 00_Program nauki_Technik ekonomista 341 02
- 1914r Teodor Jeske Choiński Program i metoda żydów
- Laurie King Mary Russel 07 The Game
- Burroughs_Patricia_Nowojorski_skandal
- VPN
- Lien_Merete_ _Zapomniany_ogród_01 _Spadek
- Taylor Jennifer Na rownych prawach
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- lo2chrzanow.htw.pl
[ Pobierz całość w formacie PDF ]
Did it work?
If not, keep in mind you can use the NULL byte at the end of the list as a
test for the end of the loop.
How about this. Run the program again, and press L to list all the records
before you entered any records.
Did it crash?
Okay. Give up? The following is my code for the listAll function. This
isn't the best or only way to do, just another way:
void listAll(void)
{
if(first==(struct account *)NULL)
puts("There are no records to print!");
else
{
current=first;
do
{
printf("%5i: %-15s %-15s $%8.2f\n",\
current->number,\
current->lastname,\
current->firstname,\
current->balance);
}
while((current=current->next) != (struct account *)NULL);
}
}
First, an if test is done to ensure that there are records to list. If the
value of the first pointer is still NULL, then the linked list is empty.
The program can go home!
The main bulk of the structure listing routine is a do-while loop. This is
one of the smoothest ways to do things without skipping over the first or
last item in the linked list. The loop is driven by the following:
(current=current->next) != (struct account *)NULL
The current=current->next statement moves the current pointer up through
the linked list. But the != stops it when the result of that operation is
the NULL pointer. This is one way to read in the last structure in the
list (other ways skip the last structure). After all,
current=current->:next does assign NULL to the value of current for the
last structure. When that happens, the do-while loop stops cold.
Inside the loop, a printf statement formats the output. This lines up
everything in nice, neat columns and rows. Here is sample of this
routine's output:
1: Lennon John $ 100.00
2: McCartney Paul $ 100.00
3: Harrison George $ 100.00
4: Starr Ringo $ 1000.00
Don't worry about modifying your program to look like mine. Instead, just
Shift+click here to download the complete source code for BANK2.C.
Compile and run BANK2.C if you like. Compare it with your own source code.
Remember, it's not the best way to do things, just another way.
The backslashes in the printf statement are to separate it onto
different lines. (It is rather long.)
Numbers between the % and the i or s in a printf formatting string tell
printf how many spaces to use when displaying data.
The - (minus sign) in the printf statement left-justifies the output.
The next lesson continues this series with examples of deleting records
in the database. Betcha can't wait!
Bonus C For Dummies Lesson 17-4Lesson 17-4 Decimating the Linked List
Most of us take the word "decimate" to mean "destroy," which is true but
not the original meaning of the word. When the Roman army decimated a foe,
they just killed every tenth guy. Ten = dec, which is where decimate came
from.
Decimation is what you're about to do in your BANK.C program. But you
won't be destroying the list entirely. Instead, you'll just be killing off
random structures in the linked list. This is entirely workable in a
sort of insane, twisted way.
Getting rid of deadbeat records
If you have a database that's just a series of 3x5 cards, then removing
old entries is cinchy: pull out the dead card. No one would know!
Now suppose your database is a series of structures in an array. How can
you delete structure account[7]? If you just zero out the entries, then
you have a blank record in the database. No, you'd probably have to copy
all the structures account[8] and above down a notch. Yeah, that might
work . . .
But arrays are boring. The truth is pointers are much easier to work with.
After all, a linked list is nothing but a string of pointers to various
spots in memory. Consider this illustration (not to scale):
The malloc function can really put the new structure it allocates anywhere
in memory. All you get back is the address in a pointer variable. Even so,
providing you link the structures together, it doesn't matter where in
memory each record lives.
To delete a record, you merely remove its pointer: Take the pointer in the
previous record and have it point at the next record. Lookee:
The pointer in the second structure now points to the fourth structure.
The third structure is out of the list. (And, realistically, in memory the
records may not be sequential anyway.) How does it work? Pointers!
Yes, it's true, the deleted structure is not removed from memory.
Well, you could use the free function to remove the structure from
memory. Mostly, however, programs just re-pack or re-index, which
removes the dead records.
Many programs use this technique. Even DOS/Windows: A delete file is
merely "skipped over" on disk. This is how UNDELETE programs work; they
find the remains of the dead file and then put it back into the list.
The fact that pointers connect these structures will be used in a later
lesson to sort them all.
Adding a delete routine to the BANK.C program
To delete a structure from the BANK.C program you need to know three
things:
Which structure to delete
The address stored in previous structure's *next pointer (which pointers
to the structure you need to delete)
The address of the following structure in memory
So, basically, you take the address stored in the previous structure's
*next pointer and store the address of the next structure in there.
(Review the illustrations earlier in this Lesson to get an idea of how
this works). Only one new variable is needed: a pointer to hold the
address of the previous structure in memory. The following code does it
all:
void deleteAccount(void)
{
char ch;
struct account *previous;
if(first==(struct account *)NULL)
puts("There are no records to delete!");
else
{
current=first;
do
{
printf("%5i: %-15s %-15s $%8.2f\n",\
current->number,\
current->lastname,\
current->firstname,\
current->balance);
printf("DELETE THIS RECORD?");
ch = toupper(getch());
if(ch=='Y')
{
puts("Yes!");
if(current==first)
{
first=current->next;
break;
}
else
{
previous->next = current->next;
break;
}
}
else
{
puts("No!");
previous=current;
}
}
while((current=current->next) != (struct account *)NULL);
}
}
Copy and paste the above code into your BANK2.C program. Remember to
prototype! Remember to add a Delete item to the menu! Save your source
code to disk as BANK3.C. Or, just Shift+Click here to download a copy of
BANK3.C
Compile and Run!
Enter about three or four different records, then try the new Delete
command. Delete a record in the middle, then list 'em all. Then try
deleting the first and last records to make sure that works.
So How Does It Work?
The deleteAccount function is relatively simple, with only a few new
tricks. First, only two variables are required: ch to hold the Y or N key
press response, and *previous to hold the address of the previous
[ Pobierz całość w formacie PDF ]