Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, 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.

Photo

Linked List Problems


  • Please log in to reply
1 reply to this topic

#1 Titan

Titan

  • Members
  • 14 posts
  • OFFLINE
  •  
  • Local time:06:23 AM

Posted 03 December 2004 - 02:45 AM

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

#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)
}


BC AdBot (Login to Remove)

 


#2 Titan

Titan
  • Topic Starter

  • Members
  • 14 posts
  • OFFLINE
  •  
  • Local time:06:23 AM

Posted 03 December 2004 - 03:03 AM

I know it's not formatted very nicely, the non-regular spacing of the post message box kinda screwed me up :thumbsup:

#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)
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users