A Unidirectional Linked List with Structures for the Tape
One of the interesting capabilities offered by allocating structures from the heap is that you can use a pointer in the structure to link structures together into a list (called a linked list). For instance, you can create an indefinitely long list of aTapeElement structures by adding a member variable to aTapeElement that is a pointer to the next aTapeElement:
struct aTapeElement { char Operator; float Operand; aTapeElement *NextElement; } ;
Then you can link them together, like this:
1: aTapeElement Tape; 2: aTapeElement SecondElement = new aTapeElement; 3: Tape.NextElement = SecondElement; 4: aTapeElement ThirdElement = new aTapeElement; 5: Tape.NextElement->NextElement = ThirdElement; ...
Get SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.