using System; using System.Diagnostics; namespace SpaceInvaders { public class SLinkMan : List { //------------------------------------------------------------------- // FIELDS //------------------------------------------------------------------- public SLinkIterator poIterator; public SLink pHead; public SLink pTail; //------------------------------------------------------------------- // CONSTRUCTION //------------------------------------------------------------------- public SLinkMan() : base() { //LTN: owned by slink manager this.poIterator = new SLinkIterator(); Debug.Assert(this.poIterator != null); this.pHead = null; this.pTail = null; } //------------------------------------------------------------------- // PUBLIC METHODS //------------------------------------------------------------------- public override void AddToFront(Node pNode) { Debug.Assert(pNode != null); SLink pLink = (SLink)pNode; if (pHead == null) { //empty list this.pHead = this.pTail = pLink; pLink.pNext = null; } else { //add to front pLink.pNext = pHead; pHead = pLink; } } public override Node RemoveFromFront() { Debug.Assert(pHead != null); SLink pLink = pHead; if(pLink.pNext == null) { //only node on list this.pHead = this.pTail = null; } else { //update head ptr this.pHead = this.pHead.pNext; } pLink.Reset(); return pLink; } public override void Remove(Node pNode) { Debug.Assert(pHead != null); Debug.Assert(pNode != null); SLink pLink = (SLink)pNode; //case 1: only node on list if (pHead == pLink) { //pHead = pTail = null; pHead = pLink.pNext; } //case 2/3: middle or last node else { //middle or last node SLink pCurr = this.pHead; SLink pPrev = null; //iterate and find while (pCurr != pLink) { pPrev = pCurr; pCurr = pCurr.pNext; } //unattach it if (this.pTail == pCurr) { this.pTail = pPrev; pPrev.pNext = null; } else { pPrev.pNext = pCurr.pNext; pCurr.pNext = null; } } pLink.Reset(); } public override Iterator GetIterator() { poIterator.Reset(this.pHead); return poIterator; } } }