Loading…

Linked List

An MQL array already gives fast indexed access and a built-in resize, so a linked list only earns its place for a specific workload: repeated insertion or removal in the middle of a collection. An array pays O(n) for that, shifting every element past the change point down or up one slot. A linked list pays O(1) once the node is found, because the operation is just moving a couple of pointers. CDoubleLinkedList is doubly linked and tracks both head and tail, so PushFront, PushBack, PopFront and PopBack are all O(1) with no traversal. GetAt, InsertAt and RemoveAt walk from whichever end is closer to the requested index rather than always starting at the head, which halves the average traversal distance for free. The part worth being explicit about is memory. Neither MQL4 nor MQL5 objects created with new are garbage collected - forgetting a matching delete leaks for the life of the terminal process on either platform. Every node this class creates is deleted on exactly one path: PopFront, PopBack and RemoveAt each delete precisely the node they detach, and Clear() (called by the destructor as well as available directly) walks the whole chain freeing every remaining node. The demo's churn pass exists specifically to exercise this: it runs hundreds of push/pop cycles and checks Count() returns to exactly 0, which is the observable symptom a leaked delete would eventually produce as the list silently stopped shrinking. InsertAt(index, value) treats index == Count() as append rather than rejecting it, which is what lets it double as PushBack at the boundary without a special case in caller code that is building a list incrementally by index. Notes and limitations - Values only, double precision - there is no generic payload type because neither MQL4 nor MQL5 has templates. Store an encoded double (a ticket number, for instance) if the payload needs to represent something else. - GetAt/InsertAt/RemoveAt are O(n) in the worst case despite the nearer-end optimisation; for pure indexed access at scale, a plain array is faster. This library is for insert/remove-heavy workloads, not read-heavy ones. - Not thread-safe, which matches both platforms' single-threaded-per-chart model. - Every node is heap-allocated individually (`new` per push). For very short-lived, very high-frequency use this carries more overhead than a pooled or array-backed structure; CRingBuffer is the better fit for a fixed-size rolling window instead.

Script · MT4 · v1.0 · Free

About Linked List

Linked List is a script for MetaTrader 4. It has 1 setting, such as Churn cycles. MQL source code included.

Files in the download: LinkedList.mqh, LinkedList.txt, LinkedListDemo.mq4.

Settings

SettingDefaultWhat it does
Churn cycles500Push/pop cycles for the memory-churn stress pass

How to install

  1. Download the ZIP from your PolyMaria library and unzip it.
  2. In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
  3. Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
  4. Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
  5. Drag the script onto a chart to run it once.

Download the PDF guide

Tags: MT4 Script · MQL4 Source Code

MQL4 & MQL5 scripts · PolyMaria