【发布时间】:2019-07-02 04:53:56
【问题描述】:
我正在使用客户端/服务器进行简单的聊天。客户端在 VB 和 python中的服务器。
我想让我的服务器存储我的消息,虽然我最聪明的是构建一个链表(我是 python 新手,但 C# 更高级)。
我尝试存储的内容包括:
- 收件人(这里称为dest)
- 消息(称为 msg)
我不知道有多少,如果我有一条新消息给已经存储了消息的人,我会覆盖
我在课堂上试过
class tabmessage:
def __init__(self, dest=None,msg=None, next=None):
self.dest = dest
self.msg = msg
self.next = None
这就是电话
#I create the top of the chain on the beginning
messages = tabmessage(dest='Control',msg='Message Integrity')
...然后在稍后的函数中
#Setting the top of the chain
(d,m,s) = (messages.dest,messages.msg,messages.next)
#Looking for a similar dest in chain and getting at the end at the same time
while True:
if (d == tempdest):
m = (tempmsg+".")[:-1]
print("Overwrite of msg for" + d + " : " + m);
return
if (s is None):
break
(d,m,s)=(s.dest,s.msg,s.next)
#If I did not found it i try to add it to the chain
s = tabmessage(dest=(tempdest+".")[:-1],msg=(tempmsg+".")[:-1])
print("Trying to add : " + s.dest + " : " + s. msg)
最后的打印看起来不错:
尝试添加:用户:这是我的消息
但如果我这样做:
print("Trying to add : " + messages.next.dest + " : " + messages.next. msg)
发生错误(NoneType 没有 dest 元素...),所以顶部仍然是单独的。
或者如果有更聪明的方法可以在 python 中做到这一点?
【问题讨论】:
-
为什么要使用链表?我不清楚您要完成什么,以及为什么需要滚动您自己的链表,但
m = tempmsg不会影响任何事情。它只是将tempmsg分配给局部变量m,然后函数返回。 -
我需要与他们的收件人一起存储一个邮件列表(它是 dest)。那我不知道有多少。在 C 或 C# 中,我会有一个使用链表。我是 Python 的新手,所以我也这么认为。
-
我将 m = tempmsg 修改为 m = (tempmsg + ".")[:-1] 在这里新建一个字符串
-
为什么不直接使用内置的
list? -
再一次,你只是分配给一个不会改变任何东西的局部变量
m
标签: python linked-list