Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Samples

Sample: Pointer LinkedList Class

A community discussion post from the original code:zIRON forum.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

Sample: Pointer LinkedList Class

Hello,

I've just put a sample class together which can be used to store pointers or numbers, well whatever you like really, it is not efficient, it is merely a sample.

block ZPtrListItem {
	pointer Data;
	
	pointer Next;
}

class ZPtrList {
	pointer First;
	pointer Last;
	
	
	procedure Deinit() {
		uses esi edi;		
		esi = this.First as ZPtrListItem;
	
		while (esi <> nil) {
			edi = esi.Next;
			GlobalFree(esi);
			esi = edi;			
		}
		this.First = nil;
		this.Last = nil;
	}
	
	function AddPointer(pointer inPtr) {
		uses ecx;
		
		eax = GlobalAlloc(GPTR, sizeof ZPtrListItem);
		eax as ZPtrListItem;
		eax.Data = inPtr;
		if (this.First == nil) {
			this.First = eax;
			this.Last = eax;
		} else {	
			ecx = this.Last as ZPtrListItem;
			ecx.Next = eax;
			this.Last = eax;
		}
	}
	
	function GetDataByIndex(int32 index) {
		if (this.First == nil) {
			return 0;
		}
		
		uses ecx edx;
		
		ecx = xor;
		eax = index;
		edx = this.First as ZPtrListItem;		
		while (eax != ecx) {
			if ([edx+4] == nil) {
				return 0;
			}
			edx = edx.Next;
			ecx++;
		}
		eax = edx.Data;
	}
	
	procedure DeleteIndex(int32 index) {
		uses ecx edx;
		ecx = xor;
		eax = index;
		eax--;
		edx = this.First as ZPtrListItem;		
		while (eax != ecx) {			
			edx = edx.Next;
			ecx++;
		}
		ecx = edx.Next as ZPtrListItem;
		eax = ecx.Next;
		edx.Next = eax;
		GlobalFree(ecx);
	}
}

this is the class and related block, which you can place in a file such as ptrList.zir (i may place this in classes.zir and add to release, i will gradually optimise it or someone else can).

Things that can be done:

  1. Better memory usage, such as growing the list in quants.
  2. Counter variable, keeping track of added item count.
  3. More useful functions :)

a quick sample of using the class:

ZPtrList myIntList;
myIntList = new ZPtrList;
edi = myIntList as ZPtrList;


for (ecx = 1 to 1000) {
	edi.AddPointer(ecx);
}

edi.DeleteIndex(996);

for (ecx = 0 to 998) {
	eax = edi.GetDataByIndex(ecx);
	printd(eax);
}

edi.Deinit();

delete myIntList;

Post your thoughts. :)