Computer Help and Spyware Removal Computer Help and Spyware Removal Computer Help and Spyware Removal Computer Help Forums Windows Startup Programs Database Spyware and Malware Removal Guides Computer Tutorials Uninstall Database File Database Computer Glossary Computer Resources
 

Welcome Guest ( Log In | Click here to Register a free account now! )



Register a free account to unlock additional features at BleepingComputer.com
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.
Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

 
Reply to this topicStart new topic
> Linked List Problems, Logic error in VC++ - can anyone see it?
Titan
post Dec 3 2004, 02:45 AM
Post #1


New Member
*

Group: Members
Posts: 14
Joined: 31-May 04
Member No.: 634



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)
}
Go to the top of the page
 
+Quote Post
Titan
post 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 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)
}
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 8th October 2008 - 03:01 AM


Advertise   |   About Us   |   Terms of Use   |   Privacy Policy   |   Contact Us   |   Site Map   |   Chat   |   Tutorials   |   Uninstall List
Discussion Forums   |   The Computer Glossary   |   Resources   |   RSS Feeds   |   Startups   |   The File Database   |   Malware Removal Guides

© 2003-2008 All Rights Reserved Bleeping Computer LLC.