Python论坛  - 讨论区

标题:答复: [python-chinese]Python没有指针,怎么实现树和链表的结构?

2004年08月10日 星期二 14:28

黎达文 ldw at suntektech.com
Tue Aug 10 14:28:51 HKT 2004

这要看实际情况的,如果树的大小本来就是固定的,我们干嘛还要动态存储结构呢? 

-----邮件原件-----
发件人: python-chinese-bounces at lists.python.cn
[mailto:python-chinese-bounces at lists.python.cn] 代表 guochen
发送时间: 2004年8月10日 13:00
收件人: gentoo.cn at 126.com, python-chinese at lists.python.cn
主题: Re: [python-chinese]Python没有指针,怎么实现树和链表的结构?

gentoo.cn,您好!

	怕的就是用数组
完全没办法控制大小

======= 2004-08-10 11:58:00 您在来信中写道:=======

>C++不用指针没法实现树吗?
>同理,Python中使用C++中不用指针的树的算法不就可以了吗
>提示:使用数组的树
>
>Jacob Fan wrote:
>
>>实现树和链表不需要指针,只要引用就可以了.所谓Python,Java一类的语言中没有指
针是指你不能得到一个数据的确切的地址,不能做指针运算等等 不是说你不能引用另外
一个对象.
>>
>>class LinkedListNode:
>>    def __init__(self, nextNode):
>>        self.nextNode = nextNode
>>
>>----- Original Message -----
>>From: "limodou" <chatme at 263.net>
>>To: <python-chinese at lists.python.cn>
>>Sent: Tuesday, August 10, 2004 11:38 AM
>>Subject: Re: [python-chinese] Python没有指针,怎么实现树和链表的结构?
>>
>>
>>  
>>
>>>guochen,您好!
>>>
>>>使用对象啊。对象可以是任意的东西啊。
>>>
>>>a = [1,2,[3,4,[5,6]]]
>>>
>>>这样不就和链表一样吗?树也可以这样表示啊。
>>>
>>>======= 2004-08-10 11:15:39 您在来信中写道:=======
>>>
>>>    
>>>
>>>>python-chinese,您好!
>>>>
>>>>
>>>>
>>>>        致
>>>>礼!
>>>>
>>>>
>>>>        guochen
>>>>        guochen at 1218.com.cn
>>>>          2004-08-10
>>>>
>>>>
>>>>_______________________________________________
>>>>python-chinese list
>>>>python-chinese at lists.python.cn
>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>      
>>>>
>>>= = = = = = = = = = = = = = = = = = = =
>>>
>>>
>>>        致
>>>礼!
>>> 
>>>
>>>        limodou
>>>        chatme at 263.net
>>>          2004-08-10
>>>
>>>
>>>    
>>>
>>
>>
>>----------------------------------------------------------------------
>>----------
>>
>>
>>  
>>
>>>_______________________________________________
>>>python-chinese list
>>>python-chinese at lists.python.cn
>>>http://python.cn/mailman/listinfo/python-chinese
>>>    
>>>
>>>---------------------------------------------------------------------
>>>---
>>>
>>>_______________________________________________
>>>python-chinese list
>>>python-chinese at lists.python.cn
>>>http://python.cn/mailman/listinfo/python-chinese
>>>    
>>>
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        guochen
        guochen at 1218.com.cn
          2004-08-10



_______________________________________________
python-chinese list
python-chinese at lists.python.cn
http://python.cn/mailman/listinfo/python-chinese


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月10日 星期二 14:29

limodou chatme at 263.net
Tue Aug 10 14:29:22 HKT 2004

gentoo.cn,您好!

	问题是讨论这些有意义吗?真想知道的话,建议你去看Python的源代码,一些都明白了。

======= 2004-08-10 12:43:24 您在来信中写道:=======

>我的意思是:是不是可以通过计算一个Object的Struct来猜测这个Object的大小?
>比如c='a', string="abcd"
>那么c的类型是1c, string的类型是4c
>calcsize('c')=1
>calcsize('L')=4
>也就是说一个数值型的对象占用4Bytes内存空间,一个字符型对象占用1Bytes内存
>空间?
>对于string="abcd"这个对象,类型是4c所以占用4Bytes内存空间?
>不知道Python如何考虑对齐的(也许他最小的单位是1Bytes所以都是8bit的倍数,
>这样就对齐了?)
>
>大家都来讨论讨论啊。
>
>
>
>limodou wrote:
>
>>gentoo.cn,您好!
>>
>>	那个不是内存的大小吧。而是对应的struct的大小。因为在python中都是对象,而不仅仅是数据结构。不知道:你想知道内存大小有什么用?
>>
>>======= 2004-08-10 11:05:42 您在来信中写道:=======
>>
>>  
>>
>>>import struct
>>>struct.pack
>>>struct.calcsize ?
>>>
>>>难道使用这个方法吗?
>>>我也想知道有什么方法来计算对象的size
>>>
>>>*calcsize*( 	fmt)
>>>
>>>   Return the size of the struct (and hence of the string)
>>>   corresponding to the given format. 
>>>
>>>Format characters have the following meaning; the conversion between C
>>>and Python values should be obvious given their types:
>>>
>>>*Format* 	*C Type* 	*Python* 	*Notes*
>>>x 	pad byte 	no value 	
>>>c 	char 	string of length 1 	
>>>b 	signed char 	integer 	
>>>B 	unsigned char 	integer 	
>>>h 	short 	integer 	
>>>H 	unsigned short 	integer 	
>>>i 	int 	integer 	
>>>I 	unsigned int 	long 	
>>>l 	long 	integer 	
>>>L 	unsigned long 	long 	
>>>q 	long long 	long 	(1)
>>>Q 	unsigned long long 	long 	(1)
>>>f 	float 	float 	
>>>d 	double 	float 	
>>>s 	char[] 	string 	
>>>p 	char[] 	string 	
>>>P 	void * 	integer 	
>>>
>>>
>>>tocer wrote:
>>>
>>>    
>>>
>>>>比如:
>>>>a=[1,2,3]
>>>>测试 a 在内存中的所占的字节数
>>>>
>>>>------------------------------------------------------------------------
>>>>
>>>>_______________________________________________
>>>>python-chinese list
>>>>python-chinese at lists.python.cn
>>>>http://python.cn/mailman/listinfo/python-chinese
>>>> 
>>>>
>>>>      
>>>>
>>>_______________________________________________
>>>python-chinese list
>>>python-chinese at lists.python.cn
>>>http://python.cn/mailman/listinfo/python-chinese
>>>
>>>    
>>>
>>
>>= = = = = = = = = = = = = = = = = = = =
>>			
>>
>>        致
>>礼!
>> 
>>				 
>>        limodou
>>        chatme at 263.net
>>          2004-08-10
>>
>>  
>>
>>------------------------------------------------------------------------
>>
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>>  
>>
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        limodou
        chatme at 263.net
          2004-08-10


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月10日 星期二 14:34

limodou chatme at 263.net
Tue Aug 10 14:34:20 HKT 2004

guochen,您好!

	你不要被我的示例局限了,每个元素可以非象复杂,象class , tuple都可以放。最简单地:
一个结点可以表示为:(level, index, value, leftnode, rightnode)
leftnode可以是另一个(level, index, value, leftnode, rightnode)
rightnode也可以是(level, index, value, leftnode, rightnode)

这样从根元素起,就是一个树了。还可以用类来实现,用list来实现。方法太多了。

======= 2004-08-10 13:39:00 您在来信中写道:=======

>limodou,您好!
>            但是现在还要实现出上下级别的关系和各个节点之间的索引
>	
>
>======= 2004-08-10 11:38:00 您在来信中写道:=======
>
>>guochen,您好!
>>
>>	使用对象啊。对象可以是任意的东西啊。
>>
>>a = [1,2,[3,4,[5,6]]]
>>
>>这样不就和链表一样吗?树也可以这样表示啊。
>>
>>======= 2004-08-10 11:15:39 您在来信中写道:=======
>>
>>>python-chinese,您好!
>>>
>>>	
>>>
>>>        致
>>>礼!
>>> 				
>>>
>>>        guochen
>>>        guochen at 1218.com.cn
>>>          2004-08-10
>>>
>>>
>>>_______________________________________________
>>>python-chinese list
>>>python-chinese at lists.python.cn
>>>http://python.cn/mailman/listinfo/python-chinese
>>
>>= = = = = = = = = = = = = = = = = = = =
>>			
>>
>>        致
>>礼!
>> 
>>				 
>>        limodou
>>        chatme at 263.net
>>          2004-08-10
>>
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>
>= = = = = = = = = = = = = = = = = = = =
>			
>
>        致
>礼!
> 
>				 
>        guochen
>        guochen at 1218.com.cn
>          2004-08-10
>
>
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        limodou
        chatme at 263.net
          2004-08-10


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月10日 星期二 14:34

guochen guochen at 1218.com.cn
Tue Aug 10 14:34:38 HKT 2004

黎达文,您好!

	废话
要是能预知大小,谁还要用树

======= 2004-08-10 14:28:00 您在来信中写道:=======

>这要看实际情况的,如果树的大小本来就是固定的,我们干嘛还要动态存储结构呢? 
>
>-----邮件原件-----
>发件人: python-chinese-bounces at lists.python.cn
>[mailto:python-chinese-bounces at lists.python.cn] 代表 guochen
>发送时间: 2004年8月10日 13:00
>收件人: gentoo.cn at 126.com, python-chinese at lists.python.cn
>主题: Re: [python-chinese]Python没有指针,怎么实现树和链表的结构?
>
>gentoo.cn,您好!
>
>	怕的就是用数组
>完全没办法控制大小
>
>======= 2004-08-10 11:58:00 您在来信中写道:=======
>
>>C++不用指针没法实现树吗?
>>同理,Python中使用C++中不用指针的树的算法不就可以了吗
>>提示:使用数组的树
>>
>>Jacob Fan wrote:
>>
>>>实现树和链表不需要指针,只要引用就可以了.所谓Python,Java一类的语言中没有指
>针是指你不能得到一个数据的确切的地址,不能做指针运算等等 不是说你不能引用另外
>一个对象.
>>>
>>>class LinkedListNode:
>>>    def __init__(self, nextNode):
>>>        self.nextNode = nextNode
>>>
>>>----- Original Message -----
>>>From: "limodou" <chatme at 263.net>
>>>To: <python-chinese at lists.python.cn>
>>>Sent: Tuesday, August 10, 2004 11:38 AM
>>>Subject: Re: [python-chinese] Python没有指针,怎么实现树和链表的结构?
>>>
>>>
>>>  
>>>
>>>>guochen,您好!
>>>>
>>>>使用对象啊。对象可以是任意的东西啊。
>>>>
>>>>a = [1,2,[3,4,[5,6]]]
>>>>
>>>>这样不就和链表一样吗?树也可以这样表示啊。
>>>>
>>>>======= 2004-08-10 11:15:39 您在来信中写道:=======
>>>>
>>>>    
>>>>
>>>>>python-chinese,您好!
>>>>>
>>>>>
>>>>>
>>>>>        致
>>>>>礼!
>>>>>
>>>>>
>>>>>        guochen
>>>>>        guochen at 1218.com.cn
>>>>>          2004-08-10
>>>>>
>>>>>
>>>>>_______________________________________________
>>>>>python-chinese list
>>>>>python-chinese at lists.python.cn
>>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>>      
>>>>>
>>>>= = = = = = = = = = = = = = = = = = = =
>>>>
>>>>
>>>>        致
>>>>礼!
>>>> 
>>>>
>>>>        limodou
>>>>        chatme at 263.net
>>>>          2004-08-10
>>>>
>>>>
>>>>    
>>>>
>>>
>>>
>>>----------------------------------------------------------------------
>>>----------
>>>
>>>
>>>  
>>>
>>>>_______________________________________________
>>>>python-chinese list
>>>>python-chinese at lists.python.cn
>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>    
>>>>
>>>>---------------------------------------------------------------------
>>>>---
>>>>
>>>>_______________________________________________
>>>>python-chinese list
>>>>python-chinese at lists.python.cn
>>>>http://python.cn/mailman/listinfo/python-chinese
>>>>    
>>>>
>>
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>
>= = = = = = = = = = = = = = = = = = = =
>			
>
>        致
>礼!
> 
>				 
>        guochen
>        guochen at 1218.com.cn
>          2004-08-10
>
>
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        guochen
        guochen at 1218.com.cn
          2004-08-10





[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号