Форум » C/C++ для начинающих (C/C++ for beginners) » structure variables c programming » Ответить

structure variables c programming

lordcideon: I want to modify this program to use a linked list o PayRecord structure variables instead of an array of those structures: #include <stdio.h> #define NUMRECS 5 struct PayRecord /* construct a global structure type */ { int id; char name[20]; double rate; }; int main() { int i; struct PayRecord employee[NUMRECS] = {{32479, "Abrams, B.", 6.72}, {33623, "Bohm, P.", 7.54}, {34145, "Donaldson, S.", 5.56}, {35987, "Ernst, T.", 5.43}, {36203, "Gwodz, K.", 8.72} }; struct PayRecord *recptr = employee; /* Pointer to array elements */ int idnum; /* User entered employee id value */ double rate; /* User entered pay rate */ int found = 0; /* Flag when employee record found */ for (i = 0; i < NUMRECS; i++, recptr++) printf("%d %-20s %4.2f\n", recptr->id,recptr->name,recptr->rate); /* Get employee id and new rate */ printf("\nEnter employee number followed by new pay rate: "); scanf("%d %lf", &idnum, &rate); /* Search for employee id in array */ for (i = 0, recptr = employee; i < NUMRECS && !found; i++) if (recptr->id == idnum) found = 1; /* Set flag when found */ else recptr++; /* Otherwise increment pointer */ if (!found) /* No record found */ printf("\nNo such employee found!\n\n"); else /* Found the employee */ recptr->rate = rate; /* Update the rate */ /* Output the array again */ for (i = 0, recptr = employee; i < NUMRECS; i++, recptr++) printf("%d %-20s %4.2f\n", recptr->id,recptr->name,recptr->rate); return 0; } Kind of like this: #include <stdio.h> int main() { struct TeleType /* Structure with link pointer */ { char name[30]; char phoneNum[15]; struct TeleType *nextaddr; }; /* A series of structure variables, or records, are declared and */ /* initialized, except for the nextaddr field: */ struct TeleType t1 = {"Acme, Sam","(555) 898 2392"}; struct TeleType t2 = {"Dolan, Edith","(555) 682 3104"}; struct TeleType t3 = {"Lanfrank, John","(555) 718 4581"}; /* Now the nexaddr field in each record is updated so that each record */ /* has a pointer to the next record. The last record has NULL in the */ /* nextaddr field to close off the list. */ t1.nextaddr = &t2; /* store t2's address in t1.nextaddr */ t2.nextaddr = &t3; /* store t3's address in t2.nextaddr */ t3.nextaddr = NULL; /* store the NULL address in t3.nextaddr */ struct TeleType *outptr = &t1; /* Point into list */ while (outptr != NULL) /* display till end of linked list */ { printf("%-30s %-20s\n", outptr->name, outptr->phoneNum); outptr = outptr->nextaddr; /* get next address */ } return 0; } Any help would be great :)

Ответов - 1

Сыроежка: If you need the same functionality as with struct TeleType then simply add one more member to your original structure that will be a pointer to a next element in the list. Or you could define one more structure like [pre2] struct Node { struct Node *next; struct PayRecord record; }; [/pre2] As for me then it is a bad idea to combine local variables in a list because if you need to add a new element in the list you need to declare a new local variable with unique name. I think that you need a list where each element of which is allocated dynamically.



полная версия страницы