So I've been using C for almost a year now but decided to give Python a chance. Python and many high level languages don't have pointers like C. Without pointers how do you implement dynamic data structures like lists and trees? (I know that Python has these built in but what if I want to use a more specific structure not built in)
Name:
Anonymous2011-08-18 8:54
You can't! Learning Python comes along with forgetting "real" languages.
Try C++ if you want a (sometimes) moar powerfull language.
"Faggot" [sic] languages don't need ponters because their objects are reference (not passed by value etc) values and the languages have GC.
Name:
Anonymous2011-08-18 10:45
In Lisp one implements lists and trees using recursive nesting of conses. It is practically identical to the C way, but with garbage collection.
Python doesn't have lists. What they call lists are in fact dynamic arrays.
>>1
In other languages, pointers are implicit for reference types allocated on the heap. Underneath, it works just like pointers, but without the syntax or need to free memory you've allocated.
this.add = function(data) {
var node = new Node(data);
if (!head) {
head = node;
} else {
current = head;
while (current.next !== null) {
current = current.next;
}
current.next = node;
}
length++;
return this;
};
this.get = function(index) {
if (index < 0 || index > length) {
return null;
}
var current = head;
for (var i = 0; i < index; i++) {
current = current.next;
}
return current.data;
};
this.remove = function(index) {
if (index < 0 || index > length) {
throw new Error("index " + index + " out of bounds");
}
if (index == 0) {
head = head.next;
}
else {
var before = head;
for (var i = 1; i < index; i++) {
before = before.next;
}
before.next = before.next.next;
}