Register | Login
Forum Index > Samples > Sample: Pointer LinkedList Class
Author Message
Pages: 1
Admin
Site Admin

avatar

(send private message)

Posts: 923
Topics: 54

Location:
OverHertz Studio
[70] Sample: Pointer LinkedList Class - posted: 2011-09-20 01:51:27
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.

Code:
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 smile

a quick sample of using the class:

Code:
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. smile

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Pages: 1
create new reply


Quick reply:

Message:



Currently Active Users:
There are currently 1 user(s) online. 0 member(s) and 1 guest(s)
Most users ever online was 533, May 5, 2016, 7:19 am.


Statistics:
Threads: 223 | Posts: 1835 | Members: 50 | Active Members: 50
Welcome to our newest member, NobodyAtWork
const Copyright = '2011-2017 © OverHertz Ltd. All rights reserved.';
Web development by OverHertz Ltd