Compiler: Microsoft VC++ 6.0 Introductory Edition
#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)
}



Back to top







