| 22 | |
| 23 | # 定义一个无序链表 |
| 24 | class UnorderedList: |
| 25 | def __init__(self): |
| 26 | self.head = None |
| 27 | |
| 28 | def isEmpty(self): |
| 29 | return self.head == None |
| 30 | |
| 31 | def add(self, item): |
| 32 | temp = Node(item) |
| 33 | temp.setNext(self.head) |
| 34 | self.head = temp |
| 35 | |
| 36 | def size(self): |
| 37 | current = self.head |
| 38 | count = 0 |
| 39 | while current != None: |
| 40 | count += 1 |
| 41 | current = current.getNext() |
| 42 | return count |
| 43 | |
| 44 | def search(self, item): |
| 45 | current = self.head |
| 46 | found = False |
| 47 | while current != None and not found: |
| 48 | if current.getData() == item: |
| 49 | found = True |
| 50 | else: |
| 51 | current = current.getNext() |
| 52 | return found |
| 53 | |
| 54 | def remove(self, item): |
| 55 | current = self.head |
| 56 | previous = None |
| 57 | found = False |
| 58 | while not found: |
| 59 | if current.getData() == item: |
| 60 | found = True |
| 61 | else: |
| 62 | previous = current |
| 63 | current = current.getNext() |
| 64 | |
| 65 | if previous == None: |
| 66 | self.head = current.getNext() |
| 67 | else: |
| 68 | previous.setNext(current.getNext()) |
| 69 | |
| 70 | myList = UnorderedList() |
| 71 | myList.add(31) |