python数据结构与算法(7)

单链表的操作
is_empty() 链表是否为空 length() 链表⻓度 travel() 遍历整个链表
add(item) 链表头部添加元素 append(item) 链表尾部添加元素 insert(pos, item) 指定位置添加元素 remove(item) 删除节点 search(item) 查找节点是否存在
单链表的实现

class   SingleLinkList(object):             """单链表"""               def __init__(self):                             self.__head =   None
                def is_empty(self):                             """判断链表是否为空"""                              return  self.__head ==  None
                def length(self):                               """链表⻓度"""                              #   cur初始时指向头节点                             cur =   self.__head                             count   =   0                               #   尾节点指向None,当未到达尾部时                               while   cur !=  None:                                               count   +=  1                                               #   将cur后移⼀个节点                                              cur =   cur.next                                return  count
                def travel(self):                               """遍历链表"""                              cur =   self.__head                             while   cur !=  None:                                               print   cur.item,                                               cur =   cur.next                                print   ""

头部添加元素
python数据结构与算法(7)


def add(self,   item):                              """头部添加元素"""                                #   先创建⼀个保存item值的节点                             node    =   SingleNode(item)                                #   将新节点的链接域next指向头节点,即_head指向的位置                               node.next   =   self.__head                             #   将链表的头_head指向新节点                             self.__head =   node

尾部添加元素
python数据结构与算法(7)


def append(self,    item):                              """尾部添加元素"""                                node    =   SingleNode(item)                                #   先判断链表是否为空,若是空链表,则将_head指向新节点                                if  self.is_empty():                                                self.__head =   node                                #   若不为空,则找到尾部,将尾节点的next指向新节点                               else:                                               cur =   self.__head                                             while   cur.next    !=  None:                                                               cur =   cur.next                                                cur.next    =   node

指定位置添加元素
python数据结构与算法(7)

def insert(self,    pos,    item):                              """指定位置添加元素"""                              #   若指定位置pos为第⼀个元素之前,则执⾏头部插⼊                                if  pos <=   0:                                              self.add(item)                              #   若指定位置超过链表尾部,则执⾏尾部插⼊                             elif    pos >    (self.length()-1):                                              self.append(item)                               #   找到指定位置                              else:                                               node    =   SingleNode(item)                                                count   =   0                                               #   pre⽤来指向指定位置pos的前⼀个位置pos-1,初始从头节点开 始移动到指定位置                                              pre =   self.__head                                             while   count   <    (pos-1):                                                                count   +=  1                                                               pre =   pre.next                                                #   先将新节点node的next指向插⼊位置的节点                                             node.next   =   pre.next                                                #   将插⼊位置的前⼀个节点的next指向新节点                                               pre.next    =   node

删除节点

def remove(self,item):                              """删除节点"""                              cur =   self.__head                             pre =   None                                while   cur !=  None:                                               #   找到了指定元素
if  cur.item    ==  item:                                                               #   如果第⼀个就是删除的节点                                                                if  not pre:                                                                                #   将头指针指向头节点的后⼀个节点                                                                             self.__head =   cur.next                                                                else:                                                                               #   将删除位置前⼀个节点的next指向删除位置的后⼀个 节点                                                                                pre.next    =   cur.next                                                                break                                               else:                                                               #   继续按链表后移节点                                                               pre =   cur                                                             cur =   cur.next

查找节点是否存在

def search(self,item):                              """链表查找节点是否存在,并返回True或者False"""                             cur =   self.__head                             while   cur !=  None:                                               if  cur.item    ==  item:                                                               return  True                                                cur =   cur.next                                return  False[/size][/font]

[font=微软雅黑][size=3]

测试

if  __name__    ==  "__main__":             ll  =   SingleLinkList()                ll.add(1)               ll.add(2)               ll.append(3)                ll.insert(2,    4)              print   "length:",ll.length()               ll.travel()
    print   ll.search(3)                print   ll.search(5)                ll.remove(1)                print   "length:",ll.length()               ll.travel()

转载于:https://blog.51cto.com/13517854/2323063

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP 7 Data Structures and Algorithms by Mizanur Rahman English | 6 Jun. 2017 | ASIN: B01IF7NLDW | 340 Pages | AZW3 | 2.55 MB Key Features Gain a complete understanding of data structures using a simple approach Analyze algorithms and learn when you should apply each solution Explore the true potential of functional data structures Book Description PHP has always been the the go-to language for web based application development, but there are materials and resources you can refer to to see how it works. Data structures and algorithms help you to code and execute them effectively, cutting down on processing time significantly. If you want to explore data structures and algorithms in a practical way with real-life projects, then this book is for you. The book begins by introducing you to data structures and algorithms and how to solve a problem from beginning to end using them. Once you are well aware of the basics, it covers the core aspects like arrays, listed lists, stacks and queues. It will take you through several methods of finding efficient algorithms and show you which ones you should implement in each scenario. In addition to this, you will explore the possibilities of functional data structures using PHP and go through advanced algorithms and graphs as well as dynamic programming. By the end, you will be confident enough to tackle both basic and advanced data structures, understand how they work, and know when to use them in your day-to-day work What you will learn Gain a better understanding of PHP arrays as a basic data structure and their hidden power Grasp how to analyze algorithms and the Big O Notation Implement linked lists, double linked lists, stack, queues, and priority queues using PHP Work with sorting, searching, and recursive algorithms Make use of greedy, dynamic, and pattern matching algorithms Implement tree, heaps, and graph algorithms Apply PHP functional data structures and built-in data structures and algorithms About the Autho
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值