python链栈_栈的链式存储结构

...........................................................................................................................................................

链栈是一种使用链表实现的栈结构。它与顺序栈相比,不需要事先指定栈的大小,可以根据实际需要动态地分配内存空间。链栈由栈顶指针和链表节点组成,栈顶指针指向链表的头节点,每个节点包含数据元素和指向下一个节点的指针。在链栈中,入栈操作相当于在链表头部插入一个新节点,出栈操作相当于删除链表头部的节点。链栈具有灵活性和扩展性,但由于需要额外的指针指向下一个节点,相对于顺序栈,它会占用更多的内存空间。

.......................................................................................................................................................... 

.......................................................................................................................................................... 

链栈依旧是遵循先进先出的原则,与链表相比,它的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博客

 ..........................................................................................................................................................

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Guff_hys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值