Double link list
Joel
joelcnz at gmail.com
Sat Feb 24 09:48:13 UTC 2018
I'm trying some code for practice, but it isn't working properly
- it prints just one number when printing in reverse.
'''
void main() {
import std.stdio, std.algorithm, std.range, std.conv;
struct List(T) {
class Node {
T value;
Node next, prev;
this(T value0) {
value = value0;
}
}
Node head, tail;
auto opOpAssign(string op)(T value) if (op == "~") {
auto newNode = new Node(value);
if (head is null) {
head = tail = newNode;
} else {
tail.next = newNode;
tail.next.prev = tail;
tail = tail.next;
}
return this;
}
auto opOpAssign(string op)(T[] values) if (op == "~") {
values.each!(v => this ~= v);
return this;
}
@property {
bool empty() { return head is null || tail is null; }
ref auto front() { return head.value; }
ref auto back() { return tail.value; }
}
void popFront() { head = head.next; if (head !is null)
head.prev = null; }
void popBack() { tail = tail.prev; if (tail !is null) tail.next
= null; }
auto save() { return this; }
}
auto ints = List!int();
ints ~= [0,1,2,3,4];
ints = ints.dropOne;
writeln(ints);
writeln(ints.retro); // just prints 4
}
'''
More information about the Digitalmars-d-learn
mailing list