Singly-linked list/Element definition: Difference between revisions
Content added Content deleted
(→[[C]]: Corrected C example (C needs the struct tag, or alternatively a typedef; also a semicolon was missing; added C++) |
m (→[[C plus plus|C++]]: missing semicolon in C++ code and typo in text) |
||
Line 37: | Line 37: | ||
int data; |
int data; |
||
link(int a_data, link* a_next = 0): next(a_next), data(a_data) {} |
link(int a_data, link* a_next = 0): next(a_next), data(a_data) {} |
||
} |
}; |
||
With this constructor, new nodes can be initialized directly at allocation; e.g. the |
With this constructor, new nodes can be initialized directly at allocation; e.g. the following code creates a complete list with just one statement: |
||
link* small_primes = new link(2, new link(3, new link(5, new link(7)))); |
link* small_primes = new link(2, new link(3, new link(5, new link(7)))); |