python链表的创建_python实现的链表

python实现的链表,包括插入、查找、删除操作

#!/usr/bin/python

class linknode():

def __init__(self,k,n=None):

self.key=k;

self.next=n;

def createlist(): #创建链表

n=raw_input("enter the num of nodes");

n=int(n);

if n<1 :

return ;

else :

a=raw_input("enter the key");

head=linknode(k=a);

if n is 1:

return head;

else :

p=head;

for i in range(1,n):

a=raw_input("enter a key");

t=linknode(k=a);

p.next=t;

p=t;

return head;

def printlist(head): #打印链表

p=head;

while p!= None:

print p.key;

p=p.next;

def listlen(head):    #计算链表长度

c=0;

p=head;

while p!= None:

c=c+1;

p=p.next;

return c;

def insert(head,n): #插入元素

if n<1 or n>listlen(head) :

return ;

p=head;

if n is 1:

a=raw_input("enter a key");

t=linknode(k=a);

t.next=head;

head=t;

else :

for i in range(1,n-1):

p=p.next;

a=raw_input("enter a key");

t=linknode(k=a);

t.next=p.next;

p.next=t;

return head;

def dellist(head,n): #删除

if n<1 or n>listlen(head):

return head;

elif n is 1:

head=head.next;

else:

p=head;

for i in range(1,n-1):

p=p.next;

q=p.next;

p.next=q.next;

return head;

def findlist(head,n): # 查找

p=head;

if p is None:

return ;

while(p!=None):

if p.key is repr(n):

print "find it";

return 1;

else:

p=p.next;

if p is None:

print "not found";

原文:http://blog.csdn.net/u011608357/article/details/28997089

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值