Linked-Lists

Finally a chance to talk about the linked-lists. You have probably heard or learnt about them in school while studying C or maybe you've read about them on the internet or a magazine.

Here is a great explanation from the great wikipedia.com:

“In computer science, a linked list is one of the fundamental data structures used in computer programming. It consists of a sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes. A linked list is a self-referential datatype because it contains a pointer or link to another data of the same type. Linked lists permit insertion and removal of nodes at any point in the list in constant time, but do not allow random access.”

In PPL linked-lists variables are extremely powerful and versatile. In each node you can have a different type of variable, structures or arrays. This opens up unlimited data storage in memory that is simply unmatched.

To create a list variable, there many ways, here is how you declare a list variable and how you add nodes to it:

List(l$);
Add(l$, 1, 2, 3, 4, 5);
Add(l$, “A”, “B”, “C”, “D”);


This new linked-list variable now contains 9 nodes with different values and types. Let's see how you can move through the nodes like other languages would allow you to:

First(l$);
while (1 == 1)
ShowMessage(l$);
if (Next(l$) == false)
break;
end;
end;

Here is how we iterate through the list in reverse order:

Last(l$);
while (1 == 1)
ShowMessage(l$);
if (Prev(l$) == false)
break;
end;
end;

The First() function moves the list internal pointer to the first node, Next() moves to the next node returning true if succeeded or false is past the end of the node list. The Last() function moves the internal list pointer to the last node in the list and the Prev() function moves to the previous node.

In PPL you can use the ForEach() statement to iterate through a list, an array, a structure or a matrix type variable:

ForEach(l$)
ShowMessage(l$);
end;

ForEachRev(l$)
ShowMessage(l$);
end;

If you need to store the list node value into another variable, you can place a second variable as a target in the ForEach() statement:

ForEach(l$, v$)
ShowMessage(v$);
end;

Let's see how PPL can access nodes at random order, PPL can access list's nodes just like regular arrays:

ShowMessage(l$[0]); // display 1
ShowMessage(l$[5]); // display A

How do we go to a specific node position? Simple, by using the Goto() function. How do we know what node is the current one? Use the Lpos() function. How many nodes are in the list? Use the Count() function.

Goto(l$, 0); // Like First()
ShowMessage(LPos(l$));
Goto(l$, Count(l$)-1); // Like Last()
ShowMessage(LPos(l$));

We can also move nodes around using the Lmove() function:

Lmove(l$, 3, 1); // This moves node 3 to node 1.

You can also insert nodes using the Ins() function:

Ins(l$, 0, 0); // Insert value 0 at node 0.

To delete a node from the list, you can use the Del() function, if you want to empty the whole list, just use Empty().

First(l$); // Move to first node.
Del(l$); // Deletes first node.
Goto(l$, 5); // Goto 5th node.
Del(l$); // Deletes 5th node.
Empty(l$); // Empty the whole list of all of its nodes.

In the next tutorial we will see how you can store different variable types like arrays and structures inside a list node.