Linked lists are suitable as a flexible and modular way to implement simple and powerful code with ease.
This library will allow you to leverage the simplicity, power and flexibility of linked lists.
A linked list is a data structure that represents a sequence of nodes (Elements). This library implements a so called "double linked list", which simply means that the list gives each Element pointers to both the next Element and the previous Element. Unlike an array, a linked list does not provide constant time access to a particular “index” within the list. This means that if you’d like to find the K-th element in the list, you will need to iterate through K elements. The benefit of a linked list is that you can add and remove Elements from the list during runtime so this means you can very easily extend your code with new FB's without extra coding since interaction with lists is always done in FOR loops. This means that by design your code doesn't need to be rewritten as demonstrated in the example below 👇
Insert the Library Reference in your program's library manager.
Declare a new FB named FB_ExtendedElement
We need to either 1) extend at least one FB_Element since it has been decorated as ABSTRACT.
or 2) implement interface: LinkedList.IElement.
Any new FB that will be a part of the List should preferably use methods 2 and encapsulation and composition to implement "FB_ExtendedElement". This way FB_ExtendElement will act as a baseclass for your Element without beings explicitly itself of type "FB_ELEMENT". If you use encapsulation and composition it will provide you with much more flexibility but as a penalty it will cost you also more programming since you have to encapsulate/composite all methods and properties. extend
FUNCTION_BLOCKFB_ExtendedElementEXTENDSElementVAR_itfPrev:IElement;_itfNext:IElement;_itfList:IList;_uiCount:UINT:=0;END_VAR// Body(*Empty,weusecomposition*)METHODIsListMemberOf:BOOL// xMemberVAR_INPUTitfList:IList;END_VAR// BodyIsListMemberOf:=(itfList=THIS^._itfList);METHODRemoveElem:IElement// BodyIF_itfList=0THEN// This element is not a member of a listRemoveElem:=0;RETURN;END_IFRemoveElem:=_itfList.RemoveElem(THIS^);PROPERTYIsLinkedElem:BOOL// Get GetIsLinkedElem:=(_itfList<>0);// Set is not implementedPROPERTYList:IList// GetList:=_itfList;// Set_itfList:=List;PROPERTYNextElem:IElement// GetList:=_itfList;// Set_itfList:=List;PROPERTYPrevElem:IElement// GetPrevElem:=_itfPrev;// Set_itfPrev:=PrevElem;METHODReturnInstance:POINTERTOFB_ExtendedElement// BodyReturnInstance:=THIS;METHODCount:UINT;// Body_uiCount:=uiCount+1;PROPERTYGetCount:UINT// GetGetCount:=_uiCount;
Declare an instance of LIST
Declare an instance of FB_ExtendedElement;
VARBuildList:Bool;List:LinkedList.List;Extended1:FB_ExtendedElement;Extended2:FB_ExtendedElement;Extended3:FB_ExtendedElement;Extended4:FB_ExtendedElement;ListSize:UDINT:=0;itfCurElement:IElement;HasItfCurElement:BOOL;END_VAR// BodyIFBuildListTHENList.AppendElement(Extended1);List.AppendElement(Extended2);List.AppendElement(Extended3);List.AppendElement(Extended4);BuildList:=FALSE;END_IFListSize:=List.ListSize;IFListSize<>0THEN// Allthough it seems obvious in this example, always test whether the interface is implemented. HasItfCurElement:=__QUERYINTERFACE(itfCurElement,itfSimpleSparkplug);IFHasItfCurElementTHENitfCurElement:=List.HeadElem;// Point itfCurElement to the listheadEND_IFWHILEitfCurElement<>0DOFORi:=1TO10DO// Count gets tallied upitfCurElement.Count();END_FOR;CurCount:=itfCurElement.GetCount;// Get the next element by using the pointer stored in the element itself (we can also use PrevElem to get the previous element)itfCurElement:=itfCurElement.NextElem;END_WHILEEND_IF
Some usage general information and rules about Elements
Always obtain access to elements via the functionblock instance of List.
Element require more burnerplate-code when compared to array's but are also more flexible.
Array's are always packed together in memory, while Elements usually are scattered throughout the memory.
This is the nature of elements.
By downloading and using our software you abide by the MIT License