# -*- coding: utf-8 -*-
# @Time : 2020/10/24 3:31 PM
class Node:
def __init__(self, input_data):
self.data = input_data
self.next = None
class my_list:
def __init__(self, node=None):
self.head = node
def is_empty(self):
return self.head == None
def length(self):
sum = 0
now = self.head
while now != None:
now = now.next
sum += 1
return sum
def append(self, data):
node = Node(data)
if self.is_empty():
self.head = node
else:
now = self.head
while now.next != None:
now = now.next
now.next = node
#插入头
def add(self, data):
node = Node(data)
if self.is_empty():
self.head = node
else:
node.next = self.head
self.head = node
def pop(self):
pre = None
##判断是否为头节点
if self.is_empty():
return None
else:
now = self.head
while now.next != None:
pre = now
now = now.next
pre.next = None
return now
def insert(self, index, data):
node = Node(data)
if index <= 0:
self.add(data)
elif index > self.length() - 1:
self.append(data)
else:
now = self.head
sum = 0
while now != None:
if index - 1 == sum:
break
now = now.next
node.next = now.next
now.next = node
def remove(self, data):
now = self.head
pre = None
while now != None:
if now.data == data:
if now == self.head:
self.head = now.next
else:
pre.next = now.next
break
else:
pre = now
now = now.next
if __name__ == '__main__':
l = my_list()
l.add(0)
l.append(1)
l.append(2)
l.append(3)
# l.pop()
l.remove(7)
print(l.length())
print(l.is_empty())