Skip to main content
gamedev.net
🔒 Locked

Node list usage

Started by TheShadow 1 reply 250+ views
Original Post
TheShadow
TheShadow
I made this factorial number calculator
  
#include <iostream>
using std::cout;
using std::endl;

int maxnodes = 5;
int i;
int sum = 1;

struct listNode 
{    
	int data; 
	struct listNode *nextPtr;
};    

void main()
{

	listNode node[19];
	for( i = 1; i <= maxnodes; i++)
	{
		node[i].data = i;
		if(i == maxnodes)
		{
			node[i].nextPtr = 0;
		}
		else
			node[i].nextPtr = &node[i+1];
	
	}

	
	listNode *iterator = &node[1];
	while (iterator != 0)
	{
		
		sum = sum * iterator->data ;
	
		iterator = iterator->nextPtr;
	}  
	cout << sum << endl;
}
  
But what other usages can i give to lists. With this simple techniques i already know by making this small program above.
Mecha Engineer (Making Real Humanoid Suits)
ToohrVyk
ToohrVyk

  
class BaseObject {

static BaseObject * s_listBegin;
BaseObject * m_next;

public:

void Add( void );

void DoSomething( void );
static void DoIt( void );

};

void BaseObject * BaseObject::s_listBegin = NULL;

BaseObject::Add( void ) {
m_next = s_listBegin;
s_listBegin = this;
}

void BaseObject::DoIt( void ) {

BaseObject * Buffer = s_listBegin;

while(Buffer) {
Buffer->DoSomething;
Buffer = Buffer->m_next;
}

}



Off top of my head...

ToohrVyk
-------------
Extatica - a free 3d game engine
Available soon!
Click here to learn more
CraZeE
CraZeE
well, as i mentioned in an earlier post, lists are useful where arrays are at a loss. arrays require u to know the size that you want to allocate. a list is very useful in a queue, for instance, where items may be added or removed arbitrarily. by having a linked list, u add new stuff to the end of the list and remove stuff at the front. however, this wont work with ur method of using an array of listnodes. check out dynamic memory allocation/deallocation for this.
To learn, we share... Give some to take some -

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.