class stack:
"""栈"""
def __init__(self):
self.item = []
def push(self, it):
return self.item.append(it)
def pop(self):
return self.item.pop()
def pip(self):
return self.item == []
def return_top(self):
return self.item[len(self.item)-1]
def get_long(self):
return len(self.item)
class Node:
def __init__(self, head):
self.head = head
self.next = None
class Linketlist:
def __init__(self, head=None):
self.head = head
"""判断是否为空"""
def isEmpty(self):
return self.head == None
"""判断链表长度"""
def length(self):
if self.head == None:
return 0
else:
pip = self.head
count = 1
while pip != None:
count += 1
pip = self.next
return count
"""遍历链表的值"""
def look_list(self):
if self.isEmpty():
print("链表里没有值!")
else:
cur = self.head
value = []
while cur != None:
value.append(cur)
cur = self.next
print(value)
"""头部添加节点"""
def add(self, value):
node = Node(value)
node.next = self.head
self.head = node
"""尾部添加节点"""
def append(self, value):
node = Node(value)
if self.isEmpty():
self.head = node
else:
cur = self.head
while cur != None:
cur = self.next
self.next = node
"""插入节点"""
def insert(self, pos, value):
if pos <= 0:
self.add(value)
elif pos > (self.length() - 1):
self.append(value)
else:
node = Node(value)
cur = self.head
index = 0
while index < (pos - 1):
index += 1
cur = cur.next
node.next = cur.next
cur.next = node
"""删除等于某个值的全部节点"""
def remove(self, value):
pre = None
cur = self.head
while cur != None:
if cur.head == value:
if pre == None:
self.head = self.head.next
cur = self.head
continue
else:
pre._next = cur.next
cur = cur.next
continue
pre = cur
cur = self.next