#!/usr/bin/env python
# -*- coding:utf-8 -*- from chapter3.single_linked_list import LNode,LinkedListUnderflow,LList
import random class LList1(LList):
def __init__(self):
LList.__init__(self)
self._rear = None #表头插入
def prepend(self, elem):
if self._head is None:
self._head = LNode(elem)
self._rear = self._head
else:
self._head = LNode(elem, self._head) #表尾插入
def append(self, elem):
if self._head is None:
self._head = LNode(elem)
self._rear = self._head
else:
self._rear.next = LNode(elem)
self._rear = self._rear.next #表头删除
def pop(self):
if self._head is None:
raise LinkedListUnderflow("in pop")
e = self._head.elem
self._head = self._head.next
return e
#self._rear不变仍然指向最后一个元素 #表尾删除
def pop_last(self):
p = self._head
if p is None:
raise LinkedListUnderflow("in pop_last")
if p.next is None:
self._head = None
while p.next.next:
p = p.next
e = p.next.elem
p.next = None
self._rear = p
return e if __name__=="__main__":
mlist1 = LList1()
mlist1.prepend(98)
mlist1.printall() for i in range(10,20):
mlist1.append(random.randint(1,20)) mlist1.printall() for i in mlist1.filter(lambda y: y%2 == 0):
print(i)