Welcome Guest ( Log In | Click here to Register a free account now! )
Welcome to Bleeping Computer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.![]() ![]() |
Dec 3 2004, 02:45 AM
Post
#1
|
|
|
New Member ![]() Group: Members Posts: 14 Joined: 31-May 04 Member No.: 634 |
Compiler: Microsoft VC++ 6.0 Introductory Edition CODE #include <iostream>
#include <windows.h> #define NUM_ENTRIES 10 using namespace std; struct ENTRY { int value; //a numerical value ENTRY *next; //pointer to the next entry ENTRY *last; //pointer to the last entry }; typedef ENTRY *ENTRY_PTR; void InitEntry(ENTRY_PTR Entry) //to initialize an ENTRY { Entry->value = 0; Entry->next = NULL; Entry->last = NULL; //set the value and pointers to zero/null } int BackList(ENTRY_PTR &Entry) //to move back in the list { if(!Entry->last) //make sure the pointer isn't 0 return 0; //return value for conditional testing else { Entry = Entry->last; //shift the pointer to the current ENTRY back one return 1; } } int NextList(ENTRY_PTR &Entry) //to move forward in the list { if(!Entry->next) //make sure the pointer isn't 0 return 0; //return value for conditional testing else { Entry = Entry->next; //shift the pointer to the current entry up one return 1; } } void main() { int i; ENTRY_PTR Current_Entry; //points to the current entry srand(GetTickCount()); //seed random numbers Current_Entry = new ENTRY; //create a new ENTRY for Current_Entry to //point to for(i=0; i<NUM_ENTRIES-1; i++) //loop to NUM_ENTRIES -1, you don't want { //to define a *next for i=9 InitEntry(Current_Entry); //initialize the current entry Current_Entry->value = rand()%10; //set a value Current_Entry->next = new ENTRY; //create a new ENTRY for //the list Current_Entry->next->last = Current_Entry; //set the prev on the next Current_Entry = Current_Entry->next; //entry to the current one } Current_Entry->value = rand()%10 //for the last entry that would be missed while(BackList(Current_Entry)); //keep going through the list while BackList returns a one //(ie while there is a valid pointer to the last ENTRY) while(NextList(Current_Entry)) printf("%d", Current_Entry->value); //keep going through the list and printing the value while NextList //returns a one (ie while there is a valid pointer to the next ENTRY) } |
|
|
|
Dec 3 2004, 03:03 AM
Post
#2
|
|
|
New Member ![]() Group: Members Posts: 14 Joined: 31-May 04 Member No.: 634 |
I know it's not formatted very nicely, the non-regular spacing of the post message box kinda screwed me up
CODE #include <iostream>
#include <windows.h> #define NUM_ENTRIES 10 using namespace std; struct ENTRY { int value; //a numerical value ENTRY *next; //pointer to the next entry ENTRY *last; //pointer to the last entry }; typedef ENTRY *ENTRY_PTR; void InitEntry(ENTRY_PTR Entry) //to initialize an ENTRY { Entry->value = 0; Entry->next = 0; Entry->last = 0; //set the value and pointers to zero } int BackList(ENTRY_PTR &Entry) //to move back in the list { if(!Entry->last) //make sure the pointer isn't 0 return 0; //return value for conditional testing else { Entry = Entry->last; //shift the pointer to the current ENTRY back one return 1; } } int NextList(ENTRY_PTR &Entry) //to move forward in the list { if(!Entry->next) //make sure the pointer isn't 0 return 0; //return value for conditional testing else { Entry = Entry->next; //shift the pointer to the current entry up one return 1; } } void main() { int i; ENTRY_PTR Current_Entry; //points to the current entry srand(GetTickCount()); //seed random numbers Current_Entry = new ENTRY; //create a new ENTRY for Current_Entry to point to for(i=0; i<NUM_ENTRIES-1; i++) //loop to NUM_ENTRIES -1, you don't want { //to define a *next for i=9 InitEntry(Current_Entry); //initialize the current entry Current_Entry->value = rand()%10; //set a value Current_Entry->next = new ENTRY; //create a new ENTRY for the list Current_Entry->next->last = Current_Entry; //set the *last on the next Current_Entry = Current_Entry->next; //entry to the current entry } Current_Entry->value = rand()%10 //for the last entry that would be missed while(BackList(Current_Entry)); //keep going through the list while BackList returns a one //(ie while there is a valid pointer to the last ENTRY) while(NextList(Current_Entry)) printf("%d", Current_Entry->value); //keep going through the list and printing the value while NextList //returns a one (ie while there is a valid pointer to the next ENTRY) } |
|
|
|
![]() ![]() |
| Lo-Fi Version | Time is now: 8th October 2008 - 03:01 AM |