无序Lists列表

  • 前面的数据结构,如栈、队列、双端队列,是使用python自带的列表list来实现的,但是并不是所有的编程语言都自带该数据结构,因此有时需要自己手动实现该对象
  • List的主要方法:
    • List():创建一个新的列表
    • add():像列表中添加一个新的元素
    • remove(item):删除列表中的元素,假定该元素位于列表中
    • search(item):搜寻列表中是否含有该元素,返回一个布尔值
    • isEmpty():判单列表是否为空
    • size():返回列表中元素的个数
    • append(item):向列表的尾端添加一个新的元素
    • index(item):返回item元素的索引值
    • insert(pos, item):向制定的位置插入元素
    • pop():删除列表的最后一个元素并返回
    • pop(pos):删除指定位置的元素并返回。

  • 1.1、为了实现列表的无序性,使用链表。链表由节点构成,节点需要存储两个信息:一个是本身的数据,另一个下一个数据的地址。
class Node:
    def __init__(self, initdata):
        self.data = initdata
        self.next = None
    def getData(self):
        return self.data
    def getNext(self):
        return self.next
    def setData(self, newdata):
        self.data = newdata
    def setNext(self,newNext):
        self.next = newNext
# 测试
node = Node(63)
print(node.getData(), node.getNext())
node.setNext(46)
print(node.getData(), node.getNext())
63 None
63 46
  • 1.2、有了节点,将各个节点连接起来得到一个链表。
    • #1:首先创建一个空的列表,表头指向"None",如图1所示
    • #2:判断列表是否为空,只需判段表头是否指向“None”即可
    • #3:添加一个元素的过程要注意顺序,首先将创建一个新的节点,然后将新节点的next指向head指向的节点,最后将原来的head指向断开,指向新的节点。如图2所示
    • #4:查看当前列表的元素个数只要逐个搜索每个节点,直到遇到“None”,如图3所示
    • 注意:add()方法,每次head都指向新加入的节点。
class UnorderedList:
    def __init__(self):    #1
        self.head = None
        
    def isEmpty(self):     #2
        return self.head == None
    
    def add(self, item):   #3
        temp = Node(item)
        temp.setNext(self.head)
        self.head = temp
        
    def size(self):        #4
        current = self.head
        count = 0
        while current != None:
            current = current.getNext()
            count += 1
        return count
    
    def search(self, item):
        current = self.head
        Found = False
        while current != None and not Found:
            if current.getData() != item:
                current = current.getNext()
            else:
                Found = True
        return Found
            
    
    def remove(self,item):
        current = self.head
        previous = None
        found = False
        while not found:
            if current.getData() == item:
                found =True
            else:
                previous = current
                current = current.getNext()
        if previous == None:       # 删除的是第一个节点时,图4
            self.head = current.getNext()
        else:                      #删除其它节点时,图5
            previous.setNext(current.getNext())
            
    def index(self, item):
        count = 0
        current = self.head
        found = False
        while not found and current != None:
            if current.getData() == item:
                found = True
            else:
                current = current.getNext()
                count += 1
        return count
    
    def append(self, item):
        previous = None
        current =  self.head
        temp = Node(item)
        while current != None:
            previous = current
            current = current.getNext()
        previous.setNext(temp)
        
    def insert(self, pos, item):
        temp = Node(item)
        current = self.head
        if pos == 0:
            temp.setNext(self.head)
            self.head = temp
        else:
            for i in range(pos - 1):           
                current = current.getNext()
        
            temp.setNext(current.getNext())
            current.setNext(temp)
alist = UnorderedList()
alist.add(31)
alist.add(77)
alist.add(17)
alist.add(93)
alist.add(26)
alist.add(54)

print(alist.search(93))
print(alist.search(100))
print("54 的索引是: ", alist.index(54))  # 0
print("93 的索引是: ", alist.index(17))  # 3

alist.insert(3,22)
print("22 的索引是: ", alist.index(22))  # 3
print("93 的索引是: ", alist.index(17))  # 4

alist.add(100)
print(alist.search(100))
print(alist.size())
print("26 的索引是: ", alist.index(26))

alist.append(1111)
print("1111 的索引是: ", alist.index(1111))
alist.remove(54)
print(alist.size())
alist.remove(93)
print(alist.size())

alist.insert(0, 999)
print("999 的索引是: ", alist.index(999))
True
False
54 的索引是:  0
93 的索引是:  3
22 的索引是:  3
93 的索引是:  4
True
8
26 的索引是:  2
1111 的索引是:  8
8
7
999 的索引是:  0
list(range(0))
[]

图1
图2
图3
图5
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值