Table of contents:
1. Realize single linked list
Second, realize the stack through the linked list
3. Realize queue through linked list
1. Realize the linked list:
class Node():
"""结点类"""
def __init__(self, data):
self.data = data
self.next = None
class LinkList():
"""链表类"""
def __init__(self,node = None):
self.__head = node
def head(self):
"""返回链表头结点"""
return self.__head
def length(self):
"""返回链表的结点数"""
count = 0
cur = self.__head
while cur != None:
count += 1
cur = cur.next
return count
def add(self,item):
"""在链表头添加结点"""
node = Node(item)
node.next = self.__head
self.__head = node
def travel(self):
"""遍历链表中每个结点的值"""
cur = self.__head
while cur !=None:
print(cur.data)
cur = cur.next
def append(self, item):
"""在链表末尾添加结点"""
node = Node(item)
if self.__head==None:
self.__head = node
cur = self.__head
while cur.next != None:
cur = cur.next
cur.next = node
def reverse(self):
"""链表反转"""
cur = self.__head
q = None
while cur!= None:
a = q
q = cur
cur = cur.next
q.next = a
self.__head = q
def insert(self,pos,item):
"""在链表指定位置插入结点"""
if pos <= 0:
self.add(item)
elif pos > self.length()-1:
self.append(item)
else:
node = Node(item)
cur = self.__head
count = 0
while count < pos-1:
count += 1
cur = cur.next
node.next = cur.next
cur.next = node
def insert_linklist(self,pos,linklist):
"""在链表指定位置插入另一个链表"""
head_2 = linklist.head()
cur2 = head_2
while cur2.next != None:
cur2 = cur2.next
cur1 = self.__head
count = 0
if pos <= 0:
cur2.next = self.__head
self.__head = head_2
return
while count < pos - 1 and cur1.next !=None:
count += 1
cur1 = cur1.next
cur2.next = cur1.next
cur1.next = head_2
def remove(self,pos):
"""删除指定位置结点"""
cur = self.__head
if pos <= 0:
cur = cur.next
self.__head = cur
else:
count = 0
while count < pos - 1:
count += 1
cur = cur.next
remove = cur.next
cur.next = remove.next
print("删除的结点位置为:%s\t数据为:%d"%(pos,remove.data))
remove.next =None
return remove
Test run:
# 定义一个链表
ll = LinkList(Node(1))
ll.add(2)
ll.add(3)
ll.append(4)
ll.insert(4,5)
ll.travel()
# 定义另一个链表
ll_ = LinkList(Node(11))
ll_.add(22)
ll_.append(33)
# 将第二个链表按照指定位置插入到第一个链表中
print('*'*100)
print("插入新链表\t")
ll_.travel()
ll.insert_linklist(8,ll_) # 链表中插入链表(22,11,33)
print('*'*100)
ll.travel()
# 对链表进行反转
print('*'*100)
print("反转")
ll.reverse() # 反转
print('*'*100)
ll.travel() # 输出反转后的链表
# 删除链表中指定位置的结点
print("*"*100)
a = ll.remove(6)
print("*"*100)
ll.travel()
operation result:
Second, realize the stack through the linked list:
Stack features: last in, first out , so just add data at the head of the linked list , and then take the head node every time it is output.
class Node():
"""结点类"""
def __init__(self,data):
self.data = data
self.next = None
class stack():
"""堆栈"""
def __init__(self,):
self.top = None
def is_empty(self):
return self.top == None
def push(self, data):
"""添加数据"""
node = Node(data)
# 在头部插入新结点
node.next = self.top
self.top = node
def pop(self):
"""输出一个数据"""
print(self.top.data)
self.top = self.top.next
def pop_all(self):
"""连续输出所有数据"""
while self.top !=None:
self.pop()
Run the test:
stack_A = stack()
select = 1
print("输入新结点选择:1\n堆栈中弹出一个数据:2\n结束输入选择:0")
print('*'*100)
while select != 0:
try:
select = int(input("选择(0/1/2):"))
except:
print("输入错误,请重新输入\n{0}".format('*'*100))
continue
if select == 1:
stack_A.push(input("输入新结点数据:"))
print('*'*100)
elif select == 2:
if stack_A.is_empty():
print("堆栈无数据可输出\n%s"%('*'*100))
continue
print("弹出的数据为:")
stack_A.pop()
print('*'*100)
elif select ==0:
select = 0
break
else:
print("输入错误,请重新输入\n{}".format('*'*100))
print("连续输出:")
stack_A.pop_all()
operation result:
3. Realize the queue through the linked list:
The characteristics of the queue are: first in first out , so you only need to add data at the end of the linked list , and then take out the head node for each output.
Here, two pointers are specified to point to the beginning and the end respectively. The function is to update the end pointer after adding data each time. To add data again, you only need to add after the end pointer, and you don’t need to traverse the entire singly linked list every time you add data.
class Node():
"""结点类"""
def __init__(self,data):
self.data = data
self.next = None
class queue():
def __init__(self):
# 定义分别指向开头与结尾的指针
self.fore = None
self.end = None
def is_empty(self):
return self.fore == None
def in_queue(self,data):
"""队列输入数据"""
node = Node(data)
if self.end == None:
self.fore = node
self.end = node
else:
self.end.next = node
self.end = node
def out_queue(self):
"""队列输出数据"""
if self.is_empty():
print("无数据可输出")
return
print(self.fore.data)
self.fore = self.fore.next
# 如果队列无数据,初始化end
if self.fore == None:
self.end = None
def out_all(self):
"""队列输出全部数据"""
while self.fore != None:
self.out_queue()
Run the test:
q = queue()
select = 1
print("输入新结点选择:1\n队列中弹出一个数据:2\n结束输入选择:0")
print('*'*100)
while select != 0:
try:
select = int(input("选择(0/1/2):"))
except:
print("输入错误,请重新输入\n{0}".format('*'*100))
continue
if select == 1:
q.in_queue(input("输入新结点数据:"))
print('*'*100)
elif select == 2:
if q.is_empty():
print("队列无数据可输出\n%s"%('*'*100))
continue
print("弹出的数据为:")
q.out_queue()
print('*'*100)
elif select ==0:
select = 0
break
else:
print("输入错误,请重新输入\n{}".format('*'*100))
print("连续输出:")
q.out_all()
operation result: