Pointers in other languages
1
Name:
Anonymous
2011-08-18 8:49
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)
2
Name:
Anonymous
2011-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.
3
Name:
Anonymous
2011-08-18 10:12
"Faggot" [sic] languages don't need ponters because their objects are reference (not passed by value etc) values and the languages have GC.
4
Name:
Anonymous
2011-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.
5
Name:
Anonymous
2011-08-18 11:45
Write it in C, the functions take PyObject * parameters.
6
Name:
Anonymous
2011-08-18 15:32
Python dynamic arrays are more cache-efficient than cons trees or lists. Prove me wrong.
7
Name:
Anonymous
2011-08-18 15:52
>>6
too bad its reference is being stored in a hash table
8
Name:
Anonymous
2011-08-18 16:08
9
Name:
Anonymous
2011-08-18 16:24
>>8
you
wouldn't buy FREE SOFTWARE
10
Name:
Anonymous
2011-08-18 20:30
>>9
That's like saying you wouldn't
buy FREE BEER
I buy beer all the time, and in addition to money it has cost me my friend, family, marriage, career and dignity.
11
Name:
Anonymous
2011-08-18 21:05
<- Look at my doubles.
12
Name:
Anonymous
2011-08-18 23:11
>>1
You can implement linked lists using closures. Closures can be used for lots of schtuff.
13
Name:
Anonymous
2011-08-19 0:03
>>12
clojures can be used for lots of stuff, too
14
Name:
Anonymous
2011-08-19 1:14
>>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.
15
Name:
Anonymous
2011-08-19 2:39
function LinkedList() {
var Node = function(data) {
this.data = data;
this.next = null;
};
var length = 0;
var head;
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;
}
length--;
return this;
};
}
16
Name:
Anonymous
2011-08-19 8:16
>>15
What language is this?
17
Name:
Anonymous
2011-08-19 8:19
18
Name:
Anonymous
2011-08-19 8:27
>>16
Look's like Javascript.
19
Name:
Anonymous
2011-08-19 8:46
20
Name:
Anonymous
2011-08-19 9:00
21
Name:
Anonymous
2011-08-19 9:13
class LinkedList:
class Node:
def __init__(self, elem):
self.elem = elem
self.next = None
class Iterator:
def __init__(self, node):
self.current = node
def next(self):
if self.current.next == None:
raise StopIteration
self.current = self.current.next
return self.current.elem
def __init__(self):
self.dummy = LinkedList.Node(None)
self.size = 0
def add(self, e):
node = self.dummy
while node.next != None:
node = node.next
if node.elem == e:
return False
node.next = LinkedList.Node(e)
self.size += 1
return True
def addAll(self, c):
changed = False
for e in c:
if self.add(e):
changed = True
return changed
def clear(self):
self.dummy.next = None
def __contains__(self, o):
for e in self:
if (o == e):
return True
return False
def containsAll(self, c):
for o in c:
if not o in self:
return False
return True
def __len__(self):
return self.size
def __iter__(self):
return LinkedList.Iterator(self.dummy)
def isEmpty(self):
return self.dummy.next == None
def remove(self, o):
node = self.dummy
while node.next != None:
if node.next.elem == o:
node.next = node.next.next
self.size -= 1
return True
node = node.next
return False
def removeAll(self, c):
changed = False
for o in c:
if self.remove(o):
changed = True
return changed
22
Name:
Anonymous
2011-08-19 9:14
POINTER MY ANUS
23
Name:
Anonymous
2011-08-19 9:14
of course that's not Perl, come on
24
Name:
Anonymous
2011-08-19 9:15
of course that's not LISP, come on
25
Name:
Anonymous
2011-08-19 9:15
>>21
Of course that is
THE FORCED INDENTATION OF CODE THREAD OVER come on.
26
Name:
Anonymous
2011-08-19 9:17
If you want to specialize some built-in data structure in Python you inherit from it.
27
Name:
Anonymous
2011-08-19 9:17
28
Name:
Anonymous
2011-08-19 9:18
ooc.util.LinkedList
By the way every ooc object is compatible for use with Python.
29
Name:
Anonymous
2011-08-19 9:28
30
Name:
Anonymous
2011-08-19 9:33
31
Name:
Anonymous
2011-08-19 9:39
Anus
Get haxed.
32
Name:
Anonymous
2011-08-19 9:40
>>29
#include <ooc/io/io.h>
#include <ooc/util/LinkedStack.h>
int main(void) {
var stack = new(LinkedStack());
Stack$push(stack, "bro?");
Stack$push(stack, "made ");
Stack$push(stack, "u ");
Stack$push(stack, "Why ");
foreach(char * str, stack) {
print(str);
} foreach_end;
println("");
del(stack);
return 0;
}
33
Name:
Anonymous
2011-08-19 9:45
>>29
from ooc.util import LinkedList
myList = LinkedList()
myList += ["Are", "you", "angry?"]
for s in myList:
print s,
34
Name:
Anonymous
2011-08-19 9:48
print s[b],[/b]
Disgusting.
35
Name:
Anonymous
2011-08-19 9:48
36
Name:
Anonymous
2011-08-19 9:50
>>34,35
If you find your own post disgusting please refrain from posting it, at least don't call it shit in a separate post.
37
Name:
Anonymous
2011-08-19 9:53
38
Name:
Anonymous
2011-08-19 9:54
>>37
That is extremely difficult, your anus is probably very tight, did you actually mean ``
SEPARATE MY BUTTCHEEKS ''?
39
Name:
Anonymous
2011-08-19 10:38
>>38
That is extremely difficult, your anus is probably very tight
Not after years of training that allows him to hide a can of condensed milk there!
40
Name:
Anonymous
2011-08-19 11:00
>>32
ooc
Pig disgusting.
>>33
from ooc.util import LinkedList
Nice language, shit import.
41
Name:
Anonymous
2011-08-19 11:43
>>39
Back to
Russian imageboards .
42
Name:
Anonymous
2011-08-19 11:52
43
Name:
Anonymous
2011-08-19 15:38
>>32
int main(void) {
var
new(), del()
return 0;
foreach_end
Okay, what language is this?
44
Name:
Anonymous
2011-08-19 15:40
>>43
BBCode
Multiquote
Fail
45
Name:
Anonymous
2011-08-19 16:11
>>44
BBCodeMultiquoteSuccess
46
Name:
Anonymous
2011-08-19 16:20
47
Name:
Anonymous
2011-08-19 16:29
48
Name:
Anonymous
2011-08-19 16:30
49
Name:
Anonymous
2011-08-19 16:44
>>48
So there was a male once with an unhaxable anus who then got an anus with surgery?
50
Name:
Anonymous
2011-08-19 16:58
>>4
Python doesn't have lists. What they call lists are in fact dynamic arrays.
I like the part where you made shit up.
Look at the source. They are double linked lists.
51
Name:
Anonymous
2011-08-19 17:29
52
Name:
Anonymous
2011-08-19 17:38
53
Name:
Anonymous
2011-08-19 17:51
>>50
In CPython they're dynamic arrays.
54
Name:
Anonymous
2011-08-19 18:41
>>52
U MENA
F I B O N A C C I B U T T S O R T E D