python数据结构栈

一、链式栈

1、首先要定义栈的结点。

class Node:
    def __init__(self,data):
        self.data=data
        self.next=None

2、然后,构造栈(stack)。

class stack:
    def __init__(self):
        self.head=Node(None)

3、构造栈的各种方法。

isempty方法判断栈是否为空

    def isempty(self):
    #初始化栈的head为空如果没有插入节点那么它应该一直是空
        if self.head.next==None:
            return True
        else:
            return False

push方法插入新的结点

因为最先输出的一定是head的next元素,所以只有把每一个新的元素放到head的后面,才能实现栈先入后出的性质。

    def push(self,data):
        new_node=Node(data)
        #首先把新的结点定义出来
        if self.isempty():
            #如果栈是空的,可以直接插入
            self.head.next=new_node
        else:
            #如果栈不是空的,就按照链表头插的方法插入
            new_node.next=self.head.next
            self.head.next=new_node
        print('插入成功')

pop方法出栈

该方法最后会返回出栈的元素。

    def pop(self):
        if self.isempty():
            #首先判断栈是不是空的
            print('栈已经为空') 
            return False
        else:
            #删除元素直接让head指向它的next的next
            p=self.head.next
            self.head.next=p.next
            return p.data

除此之外,我们还可以定义一些新的方法。

    #例如输出栈内所有内容
    def print_all(self):
        p=self.head.next
        while p:
            print(p.data)
            p=p.next
    #清空栈
    def clear(self):
        self.head.next=None

以下是完整代码大家可以直接使用

class Node:
    def __init__(self,data):
        self.data=data
        self.next=None

class stack:
    def __init__(self):
        self.head=Node(None)
    
    def isempty(self):
        if self.head.next==None:
            return True
        else:
            return False

    def push(self,data):
        new_node=Node(data)
        
        if self.isempty():
            self.head.next=new_node
        else:
            new_node.next=self.head.next
            self.head.next=new_node
        print('插入成功')

    def clear(self):
        self.head.next=None

    def pop(self):
        if self.isempty():
            print('栈已经为空') 
            return False
        else:
            p=self.head.next
            if p.next==None:
                self.clear()
            else:
                self.head.next=p.next
                return p.data
    
    def print_all(self):
        p=self.head.next
        while p:
            print(p.data)
            p=p.next

def main():
    a=stack()
    a.push(1)
    a.push(2)
    a.push(3)
    a.push(4)
    a.push(5) 
    a.pop()
    a.pop()
    a.print_all()

if __name__=='__main__':
    main()

二、顺序栈

为了实现栈的功能,我们除了用链式的栈,同样可以用list类来模拟栈的功能

class stack(list):
    def __init__(self):
        super().__init__()
    def push(self,data):
        self.insert(0,data)
    def pop_1(self):
        self.pop(0)
    def print_all(self):
        print(self)

a=stack()
a.push(1)
a.push(2)
a.push(3)
a.push(4)
a.push(5)
a.push(6)
a.pop_1()
a.pop_1()
a.print_all()

总之,栈是一种数据结构无论是哪一种存储方式,只要可以实现栈的基本功能即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值