http://GameProgrammer.Com

Programming

GP Mailing List
     Thread Index
     Date Index

ATXGPSIG List
     Thread Index
     Date Index

Google
>

Home

Wise2Food



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Linked Lists again



Thanx a lot to everyone who has helped me, Ron, that website was great

Please remember that I am a beginner, I taught myself using the Borland C++
3.0 help files, and now I am using DJGPP, so no help files. Does anyone
know what i have to do to get extensive help files or HOWTO's

back to the linked lists, Mike, I don't understand the use of template or
any of your header file.

I have attached an updated version of my linked lists program. There is a
problem in the AddNode function, but I can't find it. Anyone who can help,
thanks

Chris, sorry, you lost me totally with that file you sent me. I don't know
what I am doing wrong, but it won't compile under DJGPP.

Another thing, when in practice would you use a linked list.

thanks to all of you

http://www.oxo42.8m.com
John Oxley
#include <stdio.h>
#include <conio.h>

struct nodeType
{
	int	HouseNum;
	nodeType *NEXT;
};

struct nodeList
{
	nodeType *HEAD;
	nodeType *CURRENT;
	int	NodeNumber;
};

nodeList *list;

int	InitialiseList(void)
{
 	list=new nodeList;	// Create the list
	list->HEAD=NULL;	// Head points to NULL
	list->CURRENT=NULL;	// Current points to NULL
	list->NodeNumber=0;	// There are no nodes yet

	return 0;
}

int	AddNode(int	HouseNumber)
{
 	bool endLoop=false;
    bool addToHead=false;
    bool addToTail=false;
 	nodeType *pTemp=new	nodeType;	// Create a temp variable
	nodeType *pTemp2=new nodeType;

	pTemp->HouseNum=HouseNumber;	// Assign the passed value
	pTemp->NEXT=NULL;
	list->CURRENT=list->HEAD;
	if(list->NodeNumber==0)
   	{
	 	list->HEAD=pTemp;
		list->CURRENT=list->HEAD;
		list->CURRENT->NEXT=NULL;
	}
	else
	{
    	// Search for the correct place
        while(!endLoop)
        {
        	endLoop=true;

            // Check for an add to head
            if(list->CURRENT->HouseNum>pTemp->HouseNum)
            {
            	addToHead=true;
            }

            // Check to see if we are at the end of the list
            if(list->CURRENT)
            {
	        	// Compare to see if we are at the right place
    	        if(list->CURRENT->HouseNum<pTemp->HouseNum)
        	    {
                	pTemp2=list->CURRENT;
	        		list->CURRENT=list->CURRENT->NEXT;	// Peek ahead
                	endLoop=false;						// Do the loop again
	            }
			}
            else
            {
            	addToTail=true;
            }
	    }

        // Add to the head of the list
        if(addToHead)
        {
        	pTemp2=list->HEAD;
            list->HEAD=pTemp;
            list->HEAD->NEXT=pTemp2;
        }
        else if(addToTail)   // Add to the tail of the list
        {
        	list->CURRENT=pTemp;
            list->CURRENT->NEXT=NULL;
        	pTemp2->NEXT=list->CURRENT;
        }
        else	// Add to the middle of the list
        {
        	pTemp->NEXT=list->CURRENT->NEXT;
            list->CURRENT=pTemp;
        }

        /*
	 	// Search for the correct place
		while(!endLoop)
		{
			if(!endIf)
			{
                if(!list->CURRENT)
                {
					endIf=true;
                    addToTail=true;
                }
            	if(list->CURRENT->HouseNum<pTemp->HouseNum)
				 	list->CURRENT=list->CURRENT->NEXT;
                else
                	EndIf=true;
			}
			else
			{
			 	pTemp2=list->CURRENT;
				list->CURRENT=pTemp;
				if(list->CURRENT)
					list->CURRENT->NEXT=pTemp2;
				EndLoop=true;
				if(!list->CURRENT)
				{
				 	list->CURRENT->NEXT=NULL;
					list->TAIL=list->CURRENT;
				}
			}
		}    */
	}
	list->NodeNumber++;

	return 0;
}

int	DisplayList(void)
{
 	nodeType *pTemp=new	nodeType;
	pTemp=list->HEAD;
	while(pTemp)
	{
	 	printf("%d\n",pTemp->HouseNum);
		pTemp=pTemp->NEXT;
	}

	return 0;
}

int	main(void)
{
	// Clear the screen
	clrscr();

	InitialiseList();	// Initialise the list

	AddNode(5);			// Add a node
	AddNode(6);			// Add a node
	AddNode(7);			// Add a node
    AddNode(3);


	DisplayList();		// Display the list
	getch();


	/*
    // Create a new node
    node=new nodeType;
    node->HouseNum=5;
    node->NEXT=NULL;

    // Update the list;
    list->HEAD=node;
    list->CURRENT=node;
    list->TAIL=node;
    list->NodeNumber=1;

    // Create another new node
    node=new nodeType;
    node->HouseNum=6;
    node->NEXT=NULL;

    // Update the end of the list
	list->CURRENT=list->CURRENT->NEXT;
    list->TAIL=node;
    list->NodeNumber=2;

    // Add another node
    list->CURRENT->NEXT=new nodeType;
    list->CURRENT=list->CURRENT->NEXT;
    list->CURRENT->HouseNum=7;
    list->NodeNumber=3;

    // Update the end of the list
    list->CURRENT->NEXT=NULL;
    list->TAIL=list->CURRENT;

    printf("The list has been created with %d entries\n",list->NodeNumber);
    getch();

    // Display the list
    list->CURRENT=list->HEAD;
    while(list->CURRENT)
    {
        printf("%d\n",list->CURRENT->HouseNum);
    	list->CURRENT=list->CURRENT->NEXT;
   	}
	getch();
    */


	return 0;
}