class Person { public: char *name; // some real data char *address; // some more real data Person *next; // the "hand" Person(void); // the default constructor Person(char *nam, char *adr) // the other constructor void print(void); // a useful method etc; }; |
Person *world; // to hold the linked list's beginning Person *original[1000]; // the original array of them; int i; world=original[0]; for (i=0; i<999; i+=1) original[i]->next=original[i+1]; original[999]->next=NULL; |
Person *world; // to hold the linked list's beginning Person original[1000]; // the original array of them; int i; world=&original[0]; for (i=0; i<999; i+=1) original[i].next=&original[i+1]; original[999].next=NULL; |
Person *ptr=world; while (ptr!=NULL) { ptr->print(); ptr=ptr->next; } |
int num=0; int total=0; Person *ptr=world; while (ptr!=NULL) { total+=ptr->weight; num+=1; ptr=ptr->next; } printf("Average weight=%d\n", total/num); |
Person *ptr=world; while (ptr!=NULL && strcmp(ptr->name,"Enry Iggins")!=0) { ptr=ptr->next; } if (ptr==NULL) printf("Couldn't find Enry Iggins anywhere\n"); else { printf("We have found Enry Iggins:\n"); ptr->print(); } |
FILE *fil=fopen(......); Person *world=NULL; Person *temp; char line[500]; while (1) { if (fgets(line,499,fil)==NULL) break; temp=new Person(line); temp->next=world; world=temp; } fclose(fil); |
ptr=array[1000000]; |
ptr=world; for (int i=0; i<1000000; i+=1) ptr=ptr->next; |
class PersonLink { public: Person *thisone; PersonLink *next; |
class GoldfishLink { public: Goldfish *thisone; GoldfishLink *next; |
FILE *fil=fopen(......); PersonLink *world=NULL; PersonLink *temp; Person *p; char line[500]; while (1) { if (fgets(line,499,fil)==NULL) break; p=new Person(line); temp=new PersonLink(); temp->thisone=p; temp->next=world; world=temp; } fclose(fil); |
int num=0; int total=0; PersonLink *ptr=world; while (ptr!=NULL) { total+=ptr->weight; num+=1; ptr->print(); ptr=ptr->next; } printf("Average weight=%d\n", total/num); |
PersonLink *WList=NULL; PersonLink *temp; PersonLink *ptr=world; while (ptr!=NULL) { if (ptr->thisone->name[0]=='W') { temp=new PersonLink(); temp->thisone=ptr->thisone; temp->next=WList; WList=temp; } ptr=ptr->next; } |