![]() ![]() | |
Download the Expansion Pack!!
typedef struct List
{ long Data;
List* Next;
List()
{Next=NULL;
Data=0;
}
};
typedef List* ListPtr;
|

SLList:: SLList()
{ Head = new List;
Tail=Head;
CurrentPtr = Head;
}
|
This brings up an interesting subject also. How will we
use our linked list? This is what makes the linked list so powerful. We could use it as a priority list where
the oldest objects get a higher precedence until deleted from the list. We could also use it as a
master listing of items that need to be kept track of at one time, deleting object when they need to be,
without using any precedence scheme. Here's some code that will add a node onto the end of the list,
and then move the end pointer so that it really does point at the end.
void SLList::AddANode()
{Tail->Next = new List;
Tail=Tail->Next;
}
|
ListPtr SLList::Previous(long index)
{ ListPtr temp=Head;
for(long count=0;count<index-1;count++)
{temp=temp->Next;
}
return temp;
}
ListPtr SLList::Previous(ListPtr index)
{ListPtr temp=Head;
if(index==Head) //special case, index IS the head :)
{ return Head;
}
while(temp->Next != index)
{ temp=temp->Next;
}
return temp;
} |
void SLList::Advance()
{ if(CurrentPtr->Next != NULL)
{ CurrentPtr=CurrentPtr->Next;
}
}
void SLList::Rewind()
{ if(CurrentPtr != Head)
{ CurrentPtr=Previous(CurrentPtr);
}
}
|
void SLList::DeleteANode(ListPtr corpse) //<-- i thought it was funny :)
{ ListPtr temp;
if(corpse == Head) //case 1 corpse = Head
{temp=Head;
Head=Head->Next;
delete temp;
}
else if(corpse == Tail) //case 2 corpse is at the end
{ temp = Tail;
Tail=Previous(Tail);
Tail->Next=NULL;
delete temp;
}
else //case 3 corpse is in middle somewhere
{temp=Previous(corpse);
temp->Next=corpse->Next;
delete corpse;
}
CurrentPtr=Head; //Reset the class tempptr
} |
SLList:: ~SLList()
{ ListPtr temp = Head;
CurrentPtr = Head;
while(CurrentPtr != NULL)
{CurrentPtr = CurrentPtr->Next;
delete temp;
temp=CurrentPtr;
}
} |