Advertisement

Linked List...

Started by December 13, 2000 10:28 AM
7 comments, last by CProgrammer00015 24 years, 1 month ago
I've been sick of writting a new list of functions to deal with my linked lists that I create, it got REALLY annoying doing it for every DIFFERANT structure I created so I wrote a quick header file to solve this problem. For some reason I called the "engine" IONIZER, don't ask why Tell me what you think-
      
#ifndef _IONIZER_H_
#define _IONIZER_H_

typedef unsigned int ADDRESS_SIZE;	

/*
#include <memory.h>                  
*/


#include <stdlib.h>

unsigned int gNextOffset;	
unsigned int gPrevOffset;		
unsigned int gIonSize;			

void  InitializeIonizer (unsigned int IonSize, 
                         unsigned int NextOffset, 
                         unsigned int PrevOffset);
void* CreateStream (unsigned int IonNumber);		
void  DestroyStream(void* Head, unsigned int StreamLength);	

							 
void InitializeIonizer (unsigned int IonSize, 
                        unsigned int NextOffset, 
                        unsigned int PrevOffset)
{
	gNextOffset = NextOffset;
	gPrevOffset = PrevOffset;
	gIonSize    = IonSize;
}

void* CreateStream (unsigned int IonNumber)
{
	ADDRESS_SIZE *Edit,
		     *Temp;

	void* Ion = NULL,
	    * Return;

	Ion    = malloc(gIonSize);
	Return = Ion;

	Edit = (ADDRESS_SIZE*)(Ion)+gPrevOffset;
       *Edit = NULL;

	Edit = (ADDRESS_SIZE*)(Ion)+gNextOffset;
       *Edit = (ADDRESS_SIZE)malloc(gIonSize); 

        Temp = (ADDRESS_SIZE*)(*Edit)+gPrevOffset;
       *Temp = (ADDRESS_SIZE)Ion;
    
	for(unsigned int index = 1; index < IonNumber; index ++)
	{
	        Ion = (void*)*Edit;

		Edit = (ADDRESS_SIZE*)(Ion)+gNextOffset;
	       *Edit = (ADDRESS_SIZE)malloc(gIonSize); 

	        Temp = (ADDRESS_SIZE*)(*Edit)+gPrevOffset;
	       *Temp = (ADDRESS_SIZE)Ion;
	}

	Temp = (ADDRESS_SIZE*)(*Edit)+gNextOffset;
       *Temp = NULL;

	return(Return);
}

void DestroyStream (void* Head, unsigned int StreamLength)
{
	ADDRESS_SIZE* Edit;

	void* Temp = Head;

	for(unsigned int index = 0; index < (StreamLength-1);
            index ++)
	{
		Edit = (ADDRESS_SIZE*)(Head)+gNextOffset;
		Head = (void*)*Edit;
		free(Temp);
		Temp = Head;
	}

	free(Head);
}

#endif
      
Edited by - CProgrammer00015 on 12/13/00 11:12:49 AM
it works?

i can''t tell...
it would take me a couple of minutes to digest the code w/ the variable naming convention, and i''m working right now

what''s a stream? a node maybe?

i''ve never known it to be a good idea to have a variable name that you tell people "don''t ask me why i call it that".. ;p

ever read Code Complete by Steve McConnell?
i DEFINITELY recommend it, looking at ur code.

how do u create a ll w/ them?

hrmm... back to work.

-- Succinct(Don't listen to me)
Advertisement
hmm, looking at it from a differant point of view it does look kinda bad.... Yeah a stream is the lenght of the linked list...but the code does work!

Thanks for the reply.
To create the linked list heres all you need:

  struct LinkedList{     LinkedList  *Next;     LinkedList  *Prev;     int info;}*Head;int main(void){     InitializeIonizer(sizeof(LinkedList), 0, 4);     Head = CreateStream(5);     DestroyStream(Head, 5);      return(0);}  


The first function call initializes the engine, you pass it how large the structure is then how many bytes ahead from the beginning of the structure in memory (Next is the first byte so 0 and in a 32 bit sys, the address is 4bytes so Prev is 4 bytes ahead of Next)
Then you create the List by passing the function the size.
Then destroy it by passing it the beginning of the LL and the number of nodes.

One more thing, if you find any bugs please tell me.

CProgrammer49@juno.com



Edited by - CProgrammer00015 on December 13, 2000 12:06:00 PM

Edited by - CProgrammer00015 on December 13, 2000 12:14:04 PM
I recommend using template classes for your linked lists. That way you can have lists of any type you want, plus you just include all your list management functions as methods. Constructors/Destructors can handle memory allocation/deallocation.

First you template a list element type, than a list containing that element type.

I didn't try to read through the code you posted (sorry, too lazy) so I will apologize now in case this doesn't help at all.

This code is about 95% done. I just need to add an optimization or two and some commenting, but if you are familiar with linked lists, using this code should be straightforward. Just put the two classes into a header file.

To use them, you first typedef your element type.
  typedef ListElement(int) intElement; 

I can't use greater than less than symbols in a post, so I used parentheses here.

Then you can declare a list like this:
  DL_List(intElement); 


If you use user-defined types make sure that you have overloaded the equals operator.

    template <class ElementType> class ListElement{public:	ListElement<ElementType> *previous, *next;	ElementType nodeData;	ListElement<ElementType>(const ElementType &node)	{		previous = NULL;		next = NULL;		nodeData = node;	}	~ListElement<ElementType>()	{		previous = NULL;		next = NULL;	}};template <class ListType> class DL_List{protected:	ListType *head, *tail, *current;public:	int listSize;	DL_List<ListType>()	{		head = NULL;		tail = NULL;		current = NULL;		listSize = 0;	}	~DL_List<ListType>()	{		Destroy();	}	void Destroy()	{		if (listSize > 0)	        {			current = head;			while (current->next)			{				current = current->next;				delete current->previous;			}			delete current;			listSize = 0;		}		head = NULL;		tail = NULL;		current = NULL;	}	void AddtoHead(ListType *newElement)	{		if (listSize > 0)		{			head->previous = newElement;			newElement->next = head;			head = newElement;		}		else		{			head = newElement;			tail = newElement;			current = newElement;		}		listSize++;	}			void AddtoTail(ListType *newElement)	{		if (listSize > 0)		{			tail->next = newElement;			newElement->previous = tail;			tail = newElement;		}		else		{			head = newElement;			tail = newElement;			current = newElement;		}		listSize++;	}	void deleteNode(int index)	{		if (index >= listSize)			return;		// optimize later		int i = 0;		current = head;		while (i < index)		{			current = current->next;			i++;		}		if (current->previous)		  current->previous->next = current->next;		if (current->next)   		  current->next->previous = current->previous;					if (index == 0)			head = current->next;		else if (index == (listSize-1))			tail = current->previous;		delete current;		listSize--;	}				ListType* operator [] (int index)	{		if (index >= listSize)			return NULL;		int i = 0;		current = head;		while (i < index)		{			current = current->next;			i++;		}		return current;	}			void operator = (const DL_List<ListType> &nl1)	{		if (listSize > 0)			Destroy();		if (nl1.listSize == 0)			return;		head = new ListType(nl1.head->nodeData);		head->previous = NULL;		head->next = NULL;		current = head;		listSize++;		ListType *prev = nl1.head->next;		for (int i = 1; i < nl1.listSize; i++)		{  		  current->next = new ListType(prev->nodeData);		  current->next->previous = current;		  current = current->next;		  prev = prev->next;		  listSize++;		}		tail = current;		prev = NULL;	}		};    


Edited by - GHoST on December 13, 2000 12:45:58 PM
"That's a very nice hat."-Korben Dallas
Advertisement
THats pretty cool, I''ve never used templates much before so yeah I could do it that way to. Thanks

This is a General and Game Programming question, so it is going into the General and Game Programming Discussion, if you don''t mind.


''Smile, things could get worse.''
So I smiled, and they did.


sharewaregames.20m.com

Yes, use templates, this is a perfect example of how they can be used. While you are at it, use STL, they already have all this written for you

This topic is closed to new replies.

Advertisement