...........................................................................................................................................................
链栈是一种使用链表实现的栈结构。它与顺序栈相比,不需要事先指定栈的大小,可以根据实际需要动态地分配内存空间。链栈由栈顶指针和链表节点组成,栈顶指针指向链表的头节点,每个节点包含数据元素和指向下一个节点的指针。在链栈中,入栈操作相当于在链表头部插入一个新节点,出栈操作相当于删除链表头部的节点。链栈具有灵活性和扩展性,但由于需要额外的指针指向下一个节点,相对于顺序栈,它会占用更多的内存空间。
..........................................................................................................................................................
..........................................................................................................................................................
链栈依旧是遵循先进先出的原则,与链表相比,它的push相当于链表的头插法,进入只能从头进,出去只能从头出
..........................................................................................................................................................
push:
..........................................................................................................................................................
def push(self,data):
"""头插法"""
node = Node(data)
if self.is_empty():
self.head = node
else:
node.next = self.head
self.head = node
..........................................................................................................................................................
判空:
..........................................................................................................................................................
def is_empty(self):
return self.head == None
..........................................................................................................................................................
删除:
..........................................................................................................................................................
def pop(self):
assert self.head!=None
cur=self.head
self.head=cur.next
return cur.data
..........................................................................................................................................................
得到首节点:
..........................................................................................................................................................
def gettop(self):
assert self.head!=None
return self.head.data
得到链栈长度:
def size(self):
if self.is_empty():
return False
else:
count=0
cur=self.head
while cur!=None:
cur=cur.next
count+=1
return count
全部代码:
class Node:
def __init__(self, data):
"""创造结点"""
self.data = data
self.next = None
class LinkStack:
"""与顺序表相比,添加的方法限定为头插法,删除法限定为pop()"""
def __init__(self, node=None):
self.head = node
def is_empty(self):
return self.head == None
def push(self,data):
"""头插法"""
node = Node(data)
if self.is_empty():
self.head = node
else:
node.next = self.head
self.head = node
def pop(self):
assert self.head!=None
cur=self.head
self.head=cur.next
return cur.data
def gettop(self):
assert self.head!=None
return self.head.data
def display(self):
cur = self.head
while cur != None:
print(cur.data, end=",")
cur = cur.next
def size(self):
if self.is_empty():
return False
else:
count=0
cur=self.head
while cur!=None:
cur=cur.next
count+=1
return count
ls=LinkStack()
print(ls.is_empty())
ls.push(0)
ls.push(1)
ls.push(2)
ls.push(3)
print(ls.gettop())
print(ls.pop())
print(ls.gettop())
ls.display()
print(ls.size())
# True
# 3
# 3
# 2
#2,1,0, 3
#
# Process finished with exit code 0
...........................................................................................................................................................
Guff_hys_python数据结构,大数据开发学习,python实训项目-CSDN博客
..........................................................................................................................................................