Help - Search - Members - Calendar
Full Version: Linked List Problems
BleepingComputer.com > Software > Programming
   
Titan
I've been screwing around with linked lists for the last few days, and I get no output when I run this program. Can anyone help me out?

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)
}
Titan
I know it's not formatted very nicely, the non-regular spacing of the post message box kinda screwed me up dry.gif

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)
}
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.